in reply to need help on wait();

The confusion here is that on UNIX, wait the system call usually behaves differently than wait the shell function. The system call waits for any child process to die, returning the PID of the last dead child or -1 if there are no more children to wait for. The shell function normally waits for all children unless given a PID, in which case it waits for just that child.

At the system call level, most UNIX systems also have a waitpid system call that waits for a given child.

Perl follows the UNIX system call model and has both a wait and waitpid function. To wait for all children, you do this:

while (wait() != -1) {}

Replies are listed 'Best First'.
Re: Re: need help on wait();
by Ras (Acolyte) on Mar 01, 2002 at 15:04 UTC
    This is what i'm basically doning and it's
    not working. What am i doing wrong?

    system("sleep 2 &");
    system("sleep 5 &");
    system("sleep 20 &");
    system("sleep 25 &");
    system("sleep 30 &");
    system("sleep 35 &");

    while (wait() != -1) {}

      system() already wait()s for the child process. To get around this you are spawning a shell and telling the shell to spawn a child and not wait for it. So system() waits for the shell to finish and you are left with a grandchild process which gets adopted by "init" (PID 1) when its parent exits and you have zero children.

      If you want to have "sleep 10" be a child that you can wait for, then you'll have to use fork (on platforms that have fork) or:     system(1,"sleep 10"); (on platforms that support that) or something even more platform-specific like use Win32::Process.

              - tye (but my friends call me "Tye")