Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

need help on wait();

by Ras (Acolyte)
on Feb 28, 2002 at 20:11 UTC ( [id://148364]=perlquestion: print w/replies, xml ) Need Help??

Ras has asked for the wisdom of the Perl Monks concerning the following question:

Is there any way to make wait(); in perl work like the
wait() in unix. Meaning if i have mutiple processes
kicked off in the back ground, how can i make wait();
wait for all of the processes to finish? thanks

Replies are listed 'Best First'.
Re: need help on wait();
by derby (Abbot) on Feb 28, 2002 at 20:28 UTC
    Ras,
    perldoc -f waitpid

    -derby

Re: need help on wait();
by steves (Curate) on Mar 01, 2002 at 09:42 UTC

    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) {}
      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")
Re: need help on wait();
by bluto (Curate) on Mar 01, 2002 at 00:16 UTC
    It works like wait() in unix -- it waits for a single process. What have you tried that didn't work?

    If you need some ideas, see perlipc or "perldoc perlipc".

    bluto

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://148364]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-24 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found