# How to recognize which child process has just ended?

**URL:** <https://www.ruby-forum.com/t/how-to-recognize-which-child-process-has-just-ended/101743>\
**Category:** Ruby\
**Created:** [August 7, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/how-to-recognize-which-child-process-has-just-ended/101743 "2006-08-07T19:26:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wookie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e36b37/32.png) [@wookie](https://www.ruby-forum.com/u/wookie)\
**Post date:** [August 7, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/how-to-recognize-which-child-process-has-just-ended/101743/1 "2006-08-07T19:26:29Z")

</div>

Hi!

The piece of software I’m working on now needs to fork child processes  
and execute different programs (which terminate themselves). Also, some  
child processes need to be killed/stopped/resumed and action needs to be  
taken when a child is terminated. When the parent receives & traps the  
SIGCLD signal, how does it know which child it is regarding and whether  
it has been ended, terminated, stopped or resumed?

In brief, I need the information, which in C signal handling is  
accessible via the siginfo\_t structure.

Thanks.

---

<div class="post-metadata">

**Author:** ![wookie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e36b37/32.png) [@wookie](https://www.ruby-forum.com/u/wookie)\
**Post date:** [August 7, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/how-to-recognize-which-child-process-has-just-ended/101743/2 "2006-08-07T20:02:37Z")

</div>

Lukasz M. wrote:

> Hi!
> 
> The piece of software I’m working on now needs to fork child processes  
> and execute different programs (which terminate themselves). Also, some  
> child processes need to be killed/stopped/resumed and action needs to be  
> taken when a child is terminated. When the parent receives & traps the  
> SIGCLD signal, how does it know which child it is regarding and whether  
> it has been ended, terminated, stopped or resumed?
> 
> In brief, I need the information, which in C signal handling is  
> accessible via the siginfo\_t structure.
> 
> Thanks.

Look at Process#wait2 and Process#waitpid2.

---

<div class="post-metadata">

**Author:** ![wookie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e36b37/32.png) [@wookie](https://www.ruby-forum.com/u/wookie)\
**Post date:** [August 7, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/how-to-recognize-which-child-process-has-just-ended/101743/3 "2006-08-07T20:49:08Z")

</div>

On Tue, 8 Aug 2006, Francis C. wrote:

> > In brief, I need the information, which in C signal handling is  
> > accessible via the siginfo\_t structure.
> > 
> > Thanks.
> 
> Look at Process#wait2 and Process#waitpid2.

i don’t think that’s sufficient - one would still need a sig handler,  
something along the lines of:

harp:~ \> cat a.rb  
require ‘thread’

q = Queue.new

trap(‘SIGCHLD’){ q.push :go }

reaper = Thread.new do  
Thread.abort\_on\_exception = true

```
 loop do
   q.pop
   pid, status = Process.wait2
   p [pid, status]
 end

```

end

fork{ exit 42 }  
sleep 1

fork{ exit 2 }

puts ‘…’  
sleep

harp:~ \> ruby a.rb  
[26939, #\<Process::Status: pid=26939,exited(42)\>]  
[26940, #\<Process::Status: pid=26940,exited(2)\>]  
…

regards.

-a

---

<div class="post-metadata">

**Author:** ![wookie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e36b37/32.png) [@wookie](https://www.ruby-forum.com/u/wookie)\
**Post date:** [August 7, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/how-to-recognize-which-child-process-has-just-ended/101743/4 "2006-08-07T23:49:22Z")

</div>

On Tue, 8 Aug 2006, Francis C. wrote:

> Use a SIGCHLD handler if you need to know instantly  
> when a child process finishes, but as always observe the standard  
> guidelines for coding signal handlers.

sorry i wasn’t more explicit - it’s this that i interpreted the OP had  
wanted  
since he asked to know **when** the child exited not to do something  
whenever  
it happened to. i may have been mistaken in my interpretation though…

in any case we are in total agreement.

-a

---

<div class="post-metadata">

**Author:** ![wookie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e36b37/32.png) [@wookie](https://www.ruby-forum.com/u/wookie)\
**Post date:** [August 10, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/how-to-recognize-which-child-process-has-just-ended/101743/5 "2006-08-10T21:50:43Z")

</div>

Thank you both.

As a matter of fact I do need to use a signal handler (the application  
has an ncurses interface, so most of the time it’s locked on getch calls  
and action needs to be taken on child death), but the answer to my  
problem is indeed the Process#wait2 method, which I somehow managed to  
overlook.

---

<div class="post-metadata">

**Author:** ![wookie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e36b37/32.png) [@wookie](https://www.ruby-forum.com/u/wookie)\
**Post date:** [August 7, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/how-to-recognize-which-child-process-has-just-ended/101743/6 "2006-08-07T23:32:05Z")

</div>

unknown wrote:

> i don’t think that’s sufficient - one would still need a sig handler,  
> something along the lines of:

That’s a surprise, what made you think so? Is that true on Windows,  
perhaps? On Unix no sig handler is needed. SIGCHLD is ignored by  
default. The following works:

#----------------------

fork {exit 42}  
fork {exit 2}

Process.wait2  
Process.wait2

#----------------------

You can also call #wait and its variants with the flag Process::WNOHANG  
if you want to poll. Use a SIGCHLD handler if you need to know instantly  
when a child process finishes, but as always observe the standard  
guidelines for coding signal handlers.
