I’m looking for a reliable way of implementing a callback on a forked process, once it has finished.
I tried using trap (see the code below), but it appears to fail from time to time.
trap("CLD") {
pid = Process.wait
# do stuff
}
pid = fork {
# do stuff
}
While I did found (via google) possible explanations why this may be happening, I’m having a hard time figuring out a possible solution.
The only solution I see so far is to open a pipe between processes (parent – read end, child – write end). Then put the parent processes thread on blocking read and trap “broken pipe” or “pipe closed” exceptions.
Any of these exceptions will obviously mean that child process is dead.
UPDATE: If I’m not wrong normally closed pipe will result into EOF result of blocking read, and broken pipe (if child process crashed) will result into
Errno::EPIPEexception.