signal handler for SIGCHLD | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

signal handler for SIGCHLD

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-29-2005
jens jens is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 30 June 2005, 9:08 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 06-30-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 28 July 2014, 9:39 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
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  
Old 06-30-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 October 2014, 3:06 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 Posts
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:
SA_NOCLDWAIT --If set, and sig equals SIGCHLD, child processes of the calling processes shall not be transformed into zombie processes when they terminate. If the calling process subsequently waits for its children, and the process has no unwaited-for children that were transformed into zombie processes, it shall block until all of its children terminate, and wait(), waitid(), and waitpid() shall fail and set errno to [ECHILD]. Otherwise, terminating child processes shall be transformed into zombie processes, unless SIGCHLD is set to SIG_IGN.
That's from the Posix sigaction man page. So properly ignoring the signal may be an option as well.
    #4  
Old 07-02-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 28 July 2014, 9:39 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
Quote:
Originally Posted by Perderabo
So properly ignoring the signal may be an option as well.
I dont really think that ignoring the children is an option for jens:
Quote:
Originally Posted by jens
(cant afford to loose information about childs)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 11:59 AM.