|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
signal handler for SIGCHLD
Hi,
I have an c++ application which uses the function fork and execvp(). The parent does not wait until the child ends. The parents just creates children and let them do their stuff. You can see the parent program as a batch-manager. I have added a SIGCHLD handler to the program: void zombie_handler(int iSignal) { signal(SIGCHLD,zombie_handler); //reset handler to catch SIGCHLD for next time; int status; pid_t pid; pid = wait(&status); //After wait, child is definitely freed. printf("pid = %d , status = %d\n", pid, status); } int main(int argc,char* argv[]) { signal(SIGCHLD,zombie_handler); ... while (condition) { fork(); //do child-actions & do not wait for child to finish. We can't afford to wait... } } - How can i avoid signal races in this case? I don't want another SIGCHLD-signal while I'm busy with processing a SIGCHLD-signal !! I don't want to ignore the signal neither when I'm processing a SIGCHLD-signal (cant afford to loose information about childs) - In the signal-handler I have added the wait() function. The time that wait will need will be none, because the child has already been terminated but is not freed yet? Am I correct? Can anyone help me with this? Best regards, Jens |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
jens,
Welcome to the UNIX forums! To answer your questions: 1. sigaction and sigprocmask are just the syscalls that you need. sigaction will replace your signal syscall. Check the man pages for details. 2. Yes, since the child has already exited, the wait syscall will just reap the child and go on. Moderators, Could this be moved to the c programming forums? |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
I would like to add that signals in unix are generally not queued despite some documentation to the contrary. So if 3 children exit while the parent is not running, it is not probable that the parent will receive 3 SIGCHLD signals. So the handler should be willing to loop to reap zero or more children.
I see that Posix seems to be blessing most of the System V SIGCHLD semantics: Quote:
|
|
#4
|
||||
|
||||
|
Quote:
Quote:
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Perl Signal Handler | SFNYC | Shell Programming and Scripting | 1 | 06-11-2010 01:21 PM |
| Signal Handler Hangs | sree_ec | Programming | 6 | 03-18-2010 11:48 PM |
| SIGCHLD interrupts its own handler | jrichemont | Programming | 3 | 03-18-2010 04:05 PM |
| signal handler problems | blowtorch | Programming | 4 | 08-26-2007 08:14 AM |
| catch SIGCHLD signal in parent process | ranjan | UNIX for Advanced & Expert Users | 2 | 06-10-2005 12:52 AM |
|
|