a simple program to illustrate that child process does not shareresources with parent process

Discussion in 'C Programming' started by sree.harsha.sn@gmail.com, Nov 7, 2008.

  1. Guest

    /*here is the program */
    #include<stdio.h>
    #include<sched.h>
    #include<sys/types.h>
    #include<fcntl.h>
    int main(){
    pid_t chpid;
    int fd,variable,status;
    char ch;
    variable=9;
    if(fd=open("hello.c",O_RDONLY)==-1) {
    printf("unable to open hello.c\n");
    return 1;
    }

    chpid=fork();
    if(chpid!=0) {
    wait(&status);
    }

    else {
    variable=42;
    close(fd);
    printf("the child has changed the value of variable to %d
    \n",variable);
    printf("the child has also closed the file\n");
    return 0;
    }

    printf("the value of variable now is %d\n",variable);

    if(read(fd,&ch,1)==-1) {
    printf("READ Failed\n");
    return 1;
    }
    else {
    printf("Read from file %s",&ch);
    }
    return 0;
    }
    /*

    */
    the output is as follows

    the child has changed the value of variable to 42
    the child has also closed the file
    the value of variable now is 9

    The first three lines are as expected. but after the program goes into
    the read function second time it does not come out again Why?
     
    , Nov 7, 2008
    #1
    1. Advertising

  2. Chris Dollin Guest

    Re: a simple program to illustrate that child process does not share resources with parent process

    wrote:

    > /*here is the program */
    > #include<stdio.h>
    > #include<sched.h>
    > #include<sys/types.h>
    > #include<fcntl.h>


    (fx:snip)

    (a) comp.unix.programmer

    (b) FIX YOUR LAYOUT.

    --
    "We are on the brink of a new era, if only --" /The Beiderbeck Affair/

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, Nov 7, 2008
    #2
    1. Advertising

  3. In article <>,
    <> wrote:

    > char ch;

    ....
    > if(read(fd,&ch,1)==-1) {

    ....
    > printf("Read from file %s",&ch);


    The argument for %s needs to be a null-terminated string.

    > if(fd=open("hello.c",O_RDONLY)==-1) {


    == binds more tightly than =. Turning up the warning level on your
    compiler would probably have caught this.

    -- Richard
    --
    Please remember to mention me / in tapes you leave behind.
     
    Richard Tobin, Nov 7, 2008
    #3
  4. Re: a simple program to illustrate that child process does notshare resources with parent process

    On Fri, 7 Nov 2008 05:03:10 -0800 (PST),
    <> wrote:

    While almost all of your program is Unix specific, rather than standard
    C, your error is a language error:

    > if(fd=open("hello.c",O_RDONLY)==-1) {


    This is the same as

    if (fd = (open("hello.c",O_RDONLY) == -1)) {

    which is not what you meant. You meant

    if ((fd = open("hello.c",O_RDONLY)) == -1) {

    Most likely you can get your compiler to warn you about this. If you use
    gcc, make sure to include -W and -Wall as command line options.

    You also forgot to include some headers that your compiler can warn you
    about. If you need mroe help with this program, I suggest you post to
    comp.unix.programmer in the future. There are more people there who can
    help you with the Unix specific bits.

    Martien
    --
    |
    Martien Verbruggen | The Second Law of Thermodenial: In any
    | closed mind the quantity of ignorance
    | remains constant or increases.
     
    Martien Verbruggen, Nov 7, 2008
    #4
  5. Guest

    thanks a lot... i was looking at the second read call in the
    program... forgot all about precedences of operators.

    Martien Verbruggen wrote:
    > On Fri, 7 Nov 2008 05:03:10 -0800 (PST),
    > <> wrote:
    >
    > While almost all of your program is Unix specific, rather than standard
    > C, your error is a language error:
    >
    > > if(fd=open("hello.c",O_RDONLY)==-1) {

    >
    > This is the same as
    >
    > if (fd = (open("hello.c",O_RDONLY) == -1)) {
    >
    > which is not what you meant. You meant
    >
    > if ((fd = open("hello.c",O_RDONLY)) == -1) {
    >
    > Most likely you can get your compiler to warn you about this. If you use
    > gcc, make sure to include -W and -Wall as command line options.
    >
    > You also forgot to include some headers that your compiler can warn you
    > about. If you need mroe help with this program, I suggest you post to
    > comp.unix.programmer in the future. There are more people there who can
    > help you with the Unix specific bits.
    >
    > Martien
    > --
    > |
    > Martien Verbruggen | The Second Law of Thermodenial: In any
    > | closed mind the quantity of ignorance
    > | remains constant or increases.
     
    , Nov 9, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Troy
    Replies:
    1
    Views:
    366
    S. Justin Gengo
    Aug 26, 2003
  2. Noel Dolan
    Replies:
    0
    Views:
    276
    Noel Dolan
    Jul 18, 2004
  3. Gauri
    Replies:
    3
    Views:
    196
  4. Z W
    Replies:
    0
    Views:
    197
  5. Bitswapper
    Replies:
    5
    Views:
    184
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page