1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Rep Power

    fork() && wait()

    hi there

    okay, this is my program:

    #include <stdio.h>
    #include <unistd.h>

    int pid;

    itni (int n){
    unsigned long int z;
    if ((fork()) != 0)

    if (n==1){
    for (z=0;z<5000000;z++);

    printf ("-->%d\n", n);

    main (){
    printf ("Begin of Main()\n");
    itni (1);
    itni (2);
    waitpid(0, NULL, 0); // !!
    printf ("End of Main()\n");

    > ./fork
    Begin of Main()
    End of Main() # parent exited?
    -->1 # itni(1) output

    and the question is:

    how I can make waitpid() to wait for both processes till
    they end their jobs?

    I need to make this program work like:

    begin of main()
    end of main

    > gcc -v
    Using builtin specs.
    gcc version 2.95.4 20020320 [FreeBSD]

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Flint, MI
    Rep Power
    First, know that keeping track of this stuff is a real trick, because the interactions between processes are a lot more complex than you would think.

    The first problem is that your first argument to waitpid, 0, means to wait for any child process to exit. That means that when the first process exits, the wait is done and your program will exit.

    You need to keep track of all the processes that you spawn and not exit until all of the children have completed. You're also getting the error message "parent exited?" because of this. If you want the parent to exit before the children do, you should close all of the file descriptors shared in common in the child, as well as put the child into it's own process group.

    As hairy as all of this sounds, once you get through it all you'll understand a lot more about how your operating system works.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Rep Power

    Try something like this...

        while (wait(&retVal) != -1){//wait for all other processes to die
            if (retVal){//Clean exit should return zero
                boolQuit = true;
        if (boolQuit){
            cerr << "Got non-zero return value from wait()ing on stored procedures!  Exiting!\n";
    I use this in code that launches a bunch of child processes that I want to wait until they terminate before the main process continues.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo