From: Morton Goldberg Date: 2006-11-21T13:38:17+09:00 Subject: Re: fork, trap and exec. Pickaxe typo? On Nov 20, 2006, at 4:25 PM, Nate Murray 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 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. DEBUG = [] trap("CLD") { pid = Process.wait DEBUG << "Child pid #{pid} terminated" } begin 4.times do if fork.nil? exec(%{echo "hello from child #{Process.pid}"}) else sleep 1 end end ensure DEBUG.each { |s| puts s } end RubyMate r5712 running Ruby v1.8.2 (/usr/bin/ruby) hello from child 1743 hello from child 1744 hello from child 1745 hello from child 1746 Child pid 1743 terminated Child pid 1744 terminated Child pid 1745 terminated Child pid 1746 terminated Regards, Morton