I feel really bad for leaving you all high and dry yesterday… really bad… okay, you’re right, I don’t really feel all that bad.
I have a doosey for you all today though, no doubt about it! One of the most common trends in computers these days is multi-processing. Multiple cores means one program can do more work by spawning extra threads or child processes. I’ve already done a bit of stuff with threading, so here’s a bunch of stuff with forking (tee-hee).
What to Demo?
What am I wanting to demo here?
- How to use
fork()
to spawn a child process. - How to use
exec()
to load a new script into that child process. - How to detect when that child process is done.
- How to send signals to the child process.
- A short list of available signals, and which ones you can’t touch
Here’s the flow of the test program:
- For each signal in the list
fork()
a child processexec()
thewatch_perl_die.pl
script- Bind to the
SIGCHLD
signal to a signal handler made on the fly - Send the signal from the list to the child process using
kill()
- Verify that the child did or didn’t drop its
head.dat
file when it received the signal - Clean up and start again
- Verify that we tested all of the signals in the list
The Code
Here’s the top-level script which does the control:
Here’s the child process’ script:
Signals Tried
We tried several different signals, most of which we were able to trap. Two of the signals were, SIGSTOP
and SIGKILL
, were not able to be trapped. This is a normal part of how signals work. Here’s a link to man 7 signal
.
SIGINT
– Usable, we were able to trap itSIGUSR1
– Usable, we were able to trap itSIGALRM
– Usable, we were able to trap itSIGTERM
– Usable, we were able to trap itSIGSEGV
– Usable, we were able to trap itSIGABRT
– Usable, we were able to trap itSIGPIPE
– Usable, we were able to trap itSIGCHLD
– Usable, we were able to trap itSIGKILL
– Unusable, we were not able to trap itSIGSTOP
– Unusable, we were not able to trap it
Notes on Process Control
You’ll notice how after I perform the slaughter (kill()
) I do two things to verify that this was properly done:
- I verify that the
$bloody
variable is now set by my ownSIGCHLD
handler - I verify that the
head.dat
file was or was not dropped, depending on whether or not the signal was trappable
The reason for this is that there are two signal handlers in play: my dynamic SIGCHLD
handler, and the child program’s dynamically defined handler.
The Conclusion
I had a lot of fun putting this one together for ya, it was a blast. I hope that you find reading this code as interesting as I found writing it.
Please also note that I developed this entire program using koding.com. If you think you’d like to use koding.com, check it out. If you click here and sign up, it gives me more disk space to write code with, which enables me to do more of this.
Much thanks!