1.upto(4) do
fork do
exec("sleep 3 && echo \"hello $HOSTNAME #{Process.pid}\n\"")
end
end
Process.waitall
But trap isnt always called on all the children.
Example output:
hello nate.cpo 4136
Child pid 4136: terminated
hello nate.cpo 4139
Child pid 4139: terminated
hello nate.cpo 4137
hello nate.cpo 4138
Child pid 4138: terminated
Notice that 4137 never called the trap callback even though
Process.waitall was called.
Hugh Sasse wrote:
···
On Tue, 21 Nov 2006, Nate Murray wrote:
> Hey All, I got this code straight from the PickAxe:
>
> But it doesnt word!
> The CLD trap'ped call never executes.
>
>
> Another question... If I perform the exec 4 times the trap call back
> should be called four times, right?
>
> So this code:
>
> trap("CLD") {
> pid = Process.wait
> puts "Child pid #{pid}: terminated"
# exit
> }
>
> 1.upto(4) do
> exec("echo \"hello $HOSTNAME #{Process.pid}\n\" ; sleep 5") if
> fork.nil?
> end
sleep 25
>
> Should call trap("CLD") four times, right?
>
> Thanks for your help in advance.
that should do the job. You didn't wait around for things to happen.
Hugh
>
>
>
1.upto(4) do
fork do
exec("sleep 3 && echo \"hello $HOSTNAME #{Process.pid}\n\"")
end
end
Process.waitall
------------------------------------------------------------------------
Waits for all children, returning an array of _pid_/_status_ pairs
(where _status_ is a +Process::Status+ object).
[...]
=end
# So while I'm not sure what's happening to the missing signal
# you should be able to see that all the processes have finished.
But trap isnt always called on all the children.
Example output:
[...]
Notice that 4137 never called the trap callback even though
Process.waitall was called.
The processes that are trapped call the callback and never end up in
Process.waitall. And those in Process.waitall never send the SIGCLD.
I think I need to do more research on this...
Hugh Sasse wrote:
···
On Tue, 21 Nov 2006, Nate Murray wrote:
> Yeah, but how do I wait for an unknown amount of time? It would seem
> that this would do it:
>
> trap("CLD") {
> pid = Process.wait
> puts "Child pid #{pid}: terminated"
> }
>
> 1.upto(4) do
> fork do
> exec("sleep 3 && echo \"hello $HOSTNAME #{Process.pid}\n\"")
> end
> end
>
> Process.waitall
puts Process.waitall
=begin ri_output
------------------------------------------------------- Process::waitall
Process.waitall => [ [pid1,status1], ...]
------------------------------------------------------------------------
Waits for all children, returning an array of _pid_/_status_ pairs
(where _status_ is a +Process::Status+ object).
[...]
=end
# So while I'm not sure what's happening to the missing signal
# you should be able to see that all the processes have finished.
>
> But trap isnt always called on all the children.
>
> Example output:
>
[...]
> Notice that 4137 never called the trap callback even though
> Process.waitall was called.
>
Hugh
This is so strange. I feel that I am really missing something here.
Here is my updated code, but the output is odd:
require 'pp'
trap("CLD") {
pid = Process.wait
I think this causes a conflict with Process.waitall below. I don't understand why you're doing this and a waitall. One thing seems clear: any child process whose exit is caught here won't be caught by the waitall below.
puts "Child pid #{pid}: terminated"
}
1.upto(4) do
fork do
exec("sleep 3 && echo \"hello $HOSTNAME #{Process.pid}\n\"")
end
end
pp Process.waitall
The following code and output may give you some ideas about what is happening.
Apologies, that doesn't change anything.. /me should really test first
-rr-
···
On 11/20/06, Rak Rok <rakrok@gmail.com> wrote:
What you want is SIGCHLD. s/CLD/CHLD/
-rr-
On 11/20/06, Nate Murray <jashmenn@gmail.com> wrote:
>
> This is so strange. I feel that I am really missing something here.
>
> Here is my updated code, but the output is odd:
>
> require 'pp'
>
> trap("CLD") {
> pid = Process.wait
>