Thread: Fork and wait. need output explanation

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    36

    Fork and wait. need output explanation

    Code:
    main() {
    int status, i;
    for(i=1;i<=2;++i)
    {
    if(fork())
    {
    wait(&status)
    printf("this is the parent\n");
    }
    else printf("this is the child\n");
    }
    printf("leaving the system\n"); exit(1);
    }
    Output:
    child
    child
    leaving system
    parent
    leaving system
    parent
    child
    leaving system
    parent
    leaving system

    I have an elementary understanding of the fork() and wait() function, and I cannot walk myself through this output

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You may want to assign the output of the fork() to status. As it stands, the status variable is pointing to some arbitrary memory location. Also check the return value of fork(). Do you know what fork() is really doing here?

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    That output is not from that program.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by slingerland3g View Post
    You may want to assign the output of the fork() to status. As it stands, the status variable is pointing to some arbitrary memory location. Also check the return value of fork(). Do you know what fork() is really doing here?

    It is making a duplicate of the program, but as the child.

    This is code my teacher gave me, and asked me for the output. So I ran it to get the output, but I dont understand it.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by Dino View Post
    That output is not from that program.
    compiled in GCC

    I just checked my post vs the output, its correct.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by ollie88r View Post
    compiled in GCC

    I just checked my post vs the output, its correct.
    Not if you used that code. The output should be:


    this is the child
    this is the child
    leaving the system
    this is the parent
    leaving the system
    this is the parent
    this is the child
    leaving the system
    this is the parent
    leaving the system

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I think what Dino is pointed out is that nowhere in the source code above, are there statements which produce the given output. For example, the code says "this is the parent" whereas your output says "parent", which is not the same thing. Are there other (important) details your leaving out?

    NB: I havent really read anything else in this thread besides your last comment, so I dont know if this helps at all. I was just pointing out what Dino said (and is correct about).

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by Kennedy View Post
    Not if you used that code. The output should be:


    this is the child
    this is the child
    leaving the system
    this is the parent
    leaving the system
    this is the parent
    this is the child
    leaving the system
    this is the parent
    leaving the system

    well ya. sorry i was shorthanding it a little for ease of typing sakes.

    i just dont understand the order of the output

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Also your 'if/else' statements do not quite jive with your use of wait(). If this compiles using gcc I will be surprised. Out of curiosity what include files are you using?

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    There's a ; missing from the wait() call -- then one has to add in all the #includes there, but it does compile and produce (sort of) the output he wanted with gcc on an x86.

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by Kennedy View Post
    There's a ; missing from the wait() call -- then one has to add in all the #includes there, but it does compile and produce (sort of) the output he wanted with gcc on an x86.
    true, that was the direction I was going with.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by slingerland3g View Post
    true, that was the direction I was going with.
    Sorry I missed that typo.

    I just dont get the order of the outputs

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    Interesting problem

    Looking away from the fact that in your output it says "child" instead of "this is the child", it does have some interesting things in this code and you should really study it.

    Fork and wait are quiet easy. Fork spawns one new process. The process which forks gets the new process' (called child) program id (pid) from fork(). The child get 0 in return. Wait suspends the process to wait for a state change in one of its children. The status-variable is filled with information (if not NULL when wait is called).

    With some modifications to your program (for instance for loops are usually i =0; i < x; ++i, not i=1;i<=x;++i but it is only a matter of taste) it is easier to read what is happening:

    Code:
    Loop number: 0
    parent (pid=25591) is going to sleep, waiting for a child (perhaps pid=25592)
    this is the child (pid=25592), my parent is 25591
    Loop number: 1
    parent (pid=25592) is going to sleep, waiting for a child (perhaps pid=25593)
    this is the child (pid=25593), my parent is 25592
    pid=25593 leaving the system
    this is the parent (pid=25592) of child (pid=25593) which just died
    pid=25592 leaving the system
    this is the parent (pid=25591) of child (pid=25592) which just died
    Loop number: 1
    parent (pid=25591) is going to sleep, waiting for a child (perhaps pid=25594)
    this is the child (pid=25594), my parent is 25591
    pid=25594 leaving the system
    this is the parent (pid=25591) of child (pid=25594) which just died
    pid=25591 leaving the system
    I really recommend you to read man-pages for fork,wait,getpid,getppid. You will find them on the net if you haven't installed them. Read them very carefully since they contain much information on not so much text. For instance, they will explain that fork basicly copies the hole processes, so except a few things (such as pid) they are identical copies which means that variables, pointers, file pointer etc are not shared but are the same (be careful with file pointers because they are not shared, but points to a shared area). This is why you see that the loop runs 3 times and not 2.

    Now I have probably done too much job for you but it was an interesting program, so I couldn't stop. Make sure to understand it.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    main() {
            int status, i, pid;
            for(i=0;i<2;++i)
            {
                    printf("Loop number: %d\n",i);
                    if((pid=fork()))
                    {
                            printf("parent (pid=%d) is going to sleep, waiting for a child (perhaps pid=%d)\n", getpid(), pid);
                            wait(&status);
                            printf("this is the parent (pid=%d) of child (pid=%d) which just died\n", getpid(),pid);
                    }
                    else printf("this is the child (pid=%d), my parent is %d\n", getpid(), getppid());
            }
            printf("pid=%d leaving the system\n",getpid());
            exit(1);
    }

  14. #14
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Add some logic check, ie check the return of your fork(), as this may be more telling.

    Code:
    #include<stdio.h>
    #include<limits.h>
    #include<wait.h>
    main ()
    {
      int status, i;
      int pid;
      for (i = 1; i <= 2; ++i)
        {
          if (pid = fork ())
            {
              wait (&status);
              printf ("this is the parent - PID = %d\n", pid);
    
            }
          else
            printf ("this is the child - PID = %d\n", pid);
        }
      printf ("leaving the system\n");
      exit (1);
    }

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by per_anders View Post
    Looking away from the fact that in your output it says "child" instead of "this is the child", it does have some interesting things in this code and you should really study it.

    Fork and wait are quiet easy. Fork spawns one new process. The process which forks gets the new process' (called child) program id (pid) from fork(). The child get 0 in return. Wait suspends the process to wait for a state change in one of its children. The status-variable is filled with information (if not NULL when wait is called).

    With some modifications to your program (for instance for loops are usually i =0; i < x; ++i, not i=1;i<=x;++i but it is only a matter of taste) it is easier to read what is happening:

    Code:
    Loop number: 0
    parent (pid=25591) is going to sleep, waiting for a child (perhaps pid=25592)
    this is the child (pid=25592), my parent is 25591
    Loop number: 1
    parent (pid=25592) is going to sleep, waiting for a child (perhaps pid=25593)
    this is the child (pid=25593), my parent is 25592
    pid=25593 leaving the system
    this is the parent (pid=25592) of child (pid=25593) which just died
    pid=25592 leaving the system
    this is the parent (pid=25591) of child (pid=25592) which just died
    Loop number: 1
    parent (pid=25591) is going to sleep, waiting for a child (perhaps pid=25594)
    this is the child (pid=25594), my parent is 25591
    pid=25594 leaving the system
    this is the parent (pid=25591) of child (pid=25594) which just died
    pid=25591 leaving the system
    I really recommend you to read man-pages for fork,wait,getpid,getppid. You will find them on the net if you haven't installed them. Read them very carefully since they contain much information on not so much text. For instance, they will explain that fork basicly copies the hole processes, so except a few things (such as pid) they are identical copies which means that variables, pointers, file pointer etc are not shared but are the same (be careful with file pointers because they are not shared, but points to a shared area). This is why you see that the loop runs 3 times and not 2.

    Now I have probably done too much job for you but it was an interesting program, so I couldn't stop. Make sure to understand it.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    main() {
            int status, i, pid;
            for(i=0;i<2;++i)
            {
                    printf("Loop number: %d\n",i);
                    if((pid=fork()))
                    {
                            printf("parent (pid=%d) is going to sleep, waiting for a child (perhaps pid=%d)\n", getpid(), pid);
                            wait(&status);
                            printf("this is the parent (pid=%d) of child (pid=%d) which just died\n", getpid(),pid);
                    }
                    else printf("this is the child (pid=%d), my parent is %d\n", getpid(), getppid());
            }
            printf("pid=%d leaving the system\n",getpid());
            exit(1);
    }
    Wow. most helpful post ever.

    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  2. Signals, fork(), wait() and exec()
    By DJTurboToJo in forum C Programming
    Replies: 7
    Last Post: 03-18-2008, 09:14 AM
  3. plz help!!!
    By x135 in forum Linux Programming
    Replies: 2
    Last Post: 03-26-2006, 06:38 AM
  4. Where are fork and wait?
    By xErath in forum C Programming
    Replies: 5
    Last Post: 11-21-2004, 12:43 AM
  5. Procesees, fork(), wait() and exec()
    By Stewdent in forum Linux Programming
    Replies: 1
    Last Post: 02-20-2003, 11:34 AM