I’m not sure if I’m trying to stick a square peg into a round hole, but I’m trying to use signals to coordinate five or six child processes with their parent. I have the parent setup to handle those signals
signal(1, SigControllerCarWaiting1);
signal(2, SigControllerCarWaiting2);
signal(3, SigControllerCarWaiting3);
signal(4, SigControllerCarWaiting4);
signal(5, SigControllerCarWaiting5);
signal(6, SigControllerCarEnter1);
signal(7, SigControllerCarEnter2);
signal(8, SigControllerCarEnter3);
signal(9, SigControllerCarEnter4);
signal(10, SigControllerCarEnter5);
And it mostly works. When I run my program, many times it will complete as expected. But some of the time, it will fail to override the default behavior and my parent process will output something like ‘Hangup’ and then terminate….leaving a bunch of children still executing.
I’ve tried re-establishing the signal in the handler like…
void SigControllerCarWaiting5() { signal(5, SigControllerCarWaiting5); SigControllerCarWaiting(4); }
void SigControllerCarWaiting4() { signal(4, SigControllerCarWaiting4); SigControllerCarWaiting(3); }
void SigControllerCarWaiting3() { signal(3, SigControllerCarWaiting3); SigControllerCarWaiting(2); }
void SigControllerCarWaiting2() { signal(2, SigControllerCarWaiting2); SigControllerCarWaiting(1); }
void SigControllerCarWaiting1() { signal(1, SigControllerCarWaiting1); SigControllerCarWaiting(0); }
But it doesn’t seem to have an impact. Sometimes it runs great; other times it will fail with the signal text and kill the parent. I’m very new to all of this and thought someone could point me in the correct direction. Is there something other than ‘signal(ID, Function) I should use to capture multiple incoming signals?
If I run it with only one child process it never seems to fail. So I’m assuming it has to do with a race-condition of sorts between the children….but I haven’t been able to make any progress in tracking it down.
EDIT
After further investigation, I found sites that recommended using sigaction instead of signal…but I still seem to have the same problem. Sometimes, the signal doesn’t get handled by me, and causes my process to end prematurely.
Live and learn…
When using signal; if two children sent the same signal at the same time, the first would be processed by my handler but the second would be executed before the handler had re-established itself as the handler, causing the default behavior to happen.
Changing to sigaction does prevent this. However, it still ‘failed’ on certain signals (specifically 9 or SIGKILL because the system will not allow you to override the SIGKILL behavior).
So, my solution is to use sigaction and not use SIGKILL for my own custom communication.
Beyond that; it seems like, as others have suggested, pipes or semaphores would be better tools to use in this situation.