Question on exec subprocesses

Folks,

Can someone shed some light on how I can accomplish something similar
to this:

exec(“foo action”)
exec(“foobar action”)
Process.wait

I want to wait on the second process, yet in ruby 184, all processes
are waited on (until completion). How can I effectively fork off these
process simultaneously and wait for the last one to complete or what
would be really nice, is to wait for both of these separate processes to
finish.

TIA!
Phy

On 9/28/06, Phy P. [email protected] wrote:

Folks,

Can someone shed some light on how I can accomplish something similar to this:

exec(“foo action”)
exec(“foobar action”)
Process.wait

I want to wait on the second process, yet in ruby 184, all processes are waited on (until completion). How can I effectively fork off these process simultaneously and wait for the last one to complete or what would be really nice, is to wait for both of these separate processes to finish.

Does:
fork {exec “foo action”}
fork {exec “foobar action”}
Process.waitall

do what you want?

On 9/28/06, Phy P. [email protected] wrote:

Folks,

Can someone shed some light on how I can accomplish something similar to this:

exec(“foo action”)
exec(“foobar action”)
Process.wait

I want to wait on the second process, yet in ruby 184, all processes are waited on (until completion). How can I effectively fork off these process simultaneously and wait for the last one to complete or what would be really nice, is to wait for both of these separate processes to finish.

First of all, exec replaces the current process with an external
command.
Also Process.wait (if you got to it, which you wouldn’t after exec)
wait’s for any child process to terminate.

Now assuming that “foo action” and “foobar action” are ruby
expressions and not external commands I thing you want something like
this:

child_pid1 = fork { foo action }
child_pid2 = fork {foobar action}

child_pid1 and child_pid2 will be the process id’s of the respective
children.

Now you can do a variety of things as far as waiting for the processes
to end including:

# wait for any child to exit and return it's process id
child_pid =  Process.wait

# wait for all children to exit and return an array of the form:
#   [[pid1, status1], ...]
status_arrays = Process.wait_all

# wait for any child to exit and return an array containing the 

child
# processes pid and status
status_array = Process.wait2

 # wait for a specific child process
  waitpid(child_pid1)


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Thanks, this works like a charm!

----- Original Message ----
From: Wilson B. [email protected]
To: ruby-talk ML [email protected]
Sent: Thursday, September 28, 2006 4:45:44 PM
Subject: Re: Question on exec subprocesses

On 9/28/06, Phy P. [email protected] wrote:

Folks,

Can someone shed some light on how I can accomplish something similar to this:

exec(“foo action”)
exec(“foobar action”)
Process.wait

I want to wait on the second process, yet in ruby 184, all processes are waited on (until completion). How can I effectively fork off these process simultaneously and wait for the last one to complete or what would be really nice, is to wait for both of these separate processes to finish.

Does:
fork {exec “foo action”}
fork {exec “foobar action”}
Process.waitall

do what you want?

Well, I guess I should give more detail, the process I want to fork are
external commands. Following a post from another help individual, I
assume I shoul do something like this:

pida = fork { exec “command a;command b” }
pidb = fork { exec “command a;command b” }
Process.waitall

Something along these lines?

TIA,
Phy

----- Original Message ----
From: Rick DeNatale [email protected]
To: ruby-talk ML [email protected]
Sent: Thursday, September 28, 2006 4:51:29 PM
Subject: Re: Question on exec subprocesses

On 9/28/06, Phy P. [email protected] wrote:

Folks,

Can someone shed some light on how I can accomplish something similar to this:

exec(“foo action”)
exec(“foobar action”)
Process.wait

I want to wait on the second process, yet in ruby 184, all processes are waited on (until completion). How can I effectively fork off these process simultaneously and wait for the last one to complete or what would be really nice, is to wait for both of these separate processes to finish.

First of all, exec replaces the current process with an external
command.
Also Process.wait (if you got to it, which you wouldn’t after exec)
wait’s for any child process to terminate.

Now assuming that “foo action” and “foobar action” are ruby
expressions and not external commands I thing you want something like
this:

child_pid1 = fork { foo action }
child_pid2 = fork {foobar action}

child_pid1 and child_pid2 will be the process id’s of the respective
children.

Now you can do a variety of things as far as waiting for the processes
to end including:

# wait for any child to exit and return it's process id
child_pid =  Process.wait

# wait for all children to exit and return an array of the form:
#   [[pid1, status1], ...]
status_arrays = Process.wait_all

# wait for any child to exit and return an array containing the 

child
# processes pid and status
status_array = Process.wait2

 # wait for a specific child process
  waitpid(child_pid1)


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/