Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: multi-command sytem call

by tilly (Archbishop)
on Aug 25, 2004 at 02:57 UTC ( [id://385565]=note: print w/replies, xml ) Need Help??


in reply to multi-command sytem call

The system command returns the status of the command that you executed - in this case whether you successfully backgrounded the two processes.

Which you do.

What you need is to background the jobs, reap them, and then collect the return value when you reap them.

I'd do that by calling open twice, then calling waitpid twice and storing $?. (Yeah, it is a lot more complicated - but you are trying to keep straight a much more complicated situation.)

Replies are listed 'Best First'.
Re^2: multi-command sytem call
by water (Deacon) on Aug 25, 2004 at 03:02 UTC
    Many thanks, tilly.... Sadly, your advice is over my head at this point in my perl learning curve -- I'm not yet fluent in processes, children, pids, etc. Any pointers towards sample code to get me started?
      edan provided sample code at Re^3: multi-command sytem call. You can also get an overview at perlipc.

      The theory, quickly, goes like this.

      Every process on the system gets a Process IDentifier (aka pid), which is an integer. For all operations, the pid identifies the process. If you launch a process with open, you're told that pid. When it exits, your process gets a CHLD signal. If you've not been set up to automatically ignore CHLD signals, you can then wait or waitpid to "reap" the child. When you call wait or waitpid, Perl tells you the pid of the process that you reaped and then puts its exit status into $?. Between the time that a process attempts to exit and when its parent reaps it, that process is a zombie - dead but not gone until it manages to tell someone its fate.

      Yeah, it sounds complex. But if you sit down and try to write down what needs to happen, it generally works out to be reasonably straightforward.

      The reason why you need to be aware of all of this complexity is that you're trying to launch two processes at once, then find out what happened to them later. In particular you want to know that one refused to run because the other was running. For that you need to use the API for dealing with multiple subprocesses and keeping them straight.

Re^2: multi-command sytem call
by blm (Hermit) on Aug 25, 2004 at 03:30 UTC

    What would you call waitpid on? How would you find the process id of the child (perl prog-rpla.pl or perl prog-rpla.pl) if you use open(PROC1, "|perl prog-rpla.pl &") as I think you are indicating water should do?

      my $pid1 = open( my $fh1, '|-'); if ( $pid1 == 0 ) { print "child1 sez: $$ forked, now exec()ing false!!\n"; exec (qw/false/) or die "Can't exec false $!"; } my $pid2 = open( my $fh2, '|-'); if ( $pid2 == 0 ) { print "child2 sez: $$ forked, now exec()ing true!!\n"; exec (qw/true/) or die "Can't exec false $!"; } sleep 1; # just so the output makes more sense print "Hi, I'm the parent, and I'm going to wait for pid1=($pid1) and +pid2=($pid2)!\n"; my $dead_pid; $dead_pid = waitpid $pid1, 0; print "parent sez: pid $dead_pid exited with status $?\n"; $dead_pid = waitpid $pid2, 0; print "parent sez: pid $dead_pid exited with status $?\n";

      Output is:

      child1 sez: 19628 forked, now exec()ing false!! child2 sez: 19629 forked, now exec()ing true!! Hi, I'm the parent, and I'm going to wait for pid1=(19628) and pid2=(1 +9629)! parent sez: pid 19628 exited with status 256 parent sez: pid 19629 exited with status 0
      --
      edan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-20 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found