2008/3/3, Oliver Peng <oliver.peng / skywave.com>:
> Robert Klemme wrote:

>  > You are doing an unhealthy mix: Process.waitall and wait block to wait
>  > for an event but SIGCLD is supposed to be called when that event
>  > happens.
>  >
>  > So either, use Process.waitall in main or change the trap code to do
>  > something like manipulating a child process counter so you can detect
>  > when the last is gone.
>
> Sorry. I am not sure what do you mean. In my code, it trap the CLD
>  signal to run wait method to clean the zombie sub-process, which is same
>  as the sample code in book 'Programming ruby'.

There is no trace of Process.waitall in the signal handler sample code.

>  When a child process dies, a signal, SIGCHLD (or SIGCLD) is sent to its
>  parent process. So when I run Process.wait, I always should get the
>  response at once. And this code can work. The only problem is sometimes
>  SystemStackError was raised.
>
>  Can you please use sample code to show me where is the problem?

The problem is in waitall.  The sample code otherwise works ok.  Try this

trap "CLD" do
  puts "#{Process.wait} exited"
  # alternative
  p Process.wait2
end

5.times do |i|
  pid = fork do
    puts "proc #{i}"
  end and puts "#{pid} created"
end

puts "all children created"

sleep 10

Here's another approach

require 'pp'

5.times do |i|
  pid = fork do
    puts "proc #{i}"
  end and puts "#{pid} created"
end

puts "all children created"

pp Process.waitall


Kind regards

robert

-- 
use.inject do |as, often| as.you_can - without end