![]() |
unix signal processing
The following is a program I'm trying to get working. My problem isn't the code itself. I can't seem to understand signal handling no matter what I read or try. Will someone please help me understand how to handle signals?
The following program spawns three processes which are passed as arguments. The program is suppose to initially spawn each process and then restart each of the processes (up to 4 times) when the SIGCHLD signal is received. Please note that the p1xxx variables aren't being used at the moment and I'm aware of this fact. I originally had this program working using the waitpid() and the WNOHANG option (no signal handling) and I was using the p1xxx variables as counters. :
#include <iostream>These are the functions :
void sig_handler( int signo ) {Thank you for your time. |
Its late so you get no explanation but this. You set up the signal handler properly you just needed to actual check on the child processes in and start back up the right one. I use some global data so the signal handler would know what pids to check each time. You also need to sleep the parent so it can wait for the children. (sorry for mixed tabs and spaces, but I use spaces ;))
:
#include <iostream>Oh yes, and there might be way to do this without global data. See the "wait" function in this page of the gnu c manual. |
Thanks for your reply. That code you posted is making my head hurt so I'll have to stare at it for awhile. The actual code you have in the sig_handler() looks similar to the code I had before this program used signals. I'm going to play with this for awhile longer. I'll post back if I have any more questions.
Once I understand this I'll probably try to change it so it doesn't use globals. I have a feeling that my professor will reject it if I use globals. |
Using the GCC code I linked to you should be able to removed the need to store the pids globally but you somehow have to know what command to run when a certain child quits. The signal handler can't be passed any other information so it will be quite a trick for it to figure out what command it should run to "restart" a process.
|
It seems to be working just fine with the globals. There were some bugs but the logic of the code is sound. Why can't the signal handler be passed anything more than int signo? Is that just the way it's implemented?
|
| All times are GMT -5. The time now is 1:36 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC