The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting exit status of child in trap handler rimon Shell Programming and Scripting 4 06-16-2008 10:05 PM
Runaway SIGALRM signal handler stewartw High Level Programming 8 04-10-2008 09:13 PM
signal handler problems blowtorch High Level Programming 4 08-26-2007 05:14 AM
signal handler for SIGCHLD jens High Level Programming 3 07-02-2005 09:05 AM
shell script signal handler jalburger Shell Programming and Scripting 2 12-04-2002 01:10 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-2008
Registered User
 

Join Date: May 2008
Posts: 3
Stumble this Post!
Question Usage of exit() inside a signal handler

Is it ok to use exit() inside a signal handler?
I catch SIGUSR1 in a signal handler and I try to close a file and then exit. The result is inconsistent. Sometimes the process exit and sometimes it returns to the original state before the signal handler was invoked.

Perhaps exit is not legal in a handler function?

T.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-03-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 541
Stumble this Post!
Exception handler

Show what your SIGUSR1 signal handler function looks like.
Reply With Quote
  #3 (permalink)  
Old 07-03-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
Stumble this Post!
If your signal handling function is declared in main(), in the parent, before any fork and the children don't deviate from this handling then the the entire process group will react according to the described signal handler.
Reply With Quote
  #4 (permalink)  
Old 07-06-2008
Registered User
 

Join Date: May 2008
Posts: 3
Stumble this Post!
Quote:
Originally Posted by shamrock View Post
Show what your SIGUSR1 signal handler function looks like.
This is the signal handler function:

static void handle_sigusr1()
{
if (DebugFile!=NULL) {
fwrite(DebugBuff.buff_ptr, DebugBuff.buff_len, 1, DebugFile);
fprintf(DebugFile,"\n Shutting down process........\n");
fclose(DebugFile);
}

exit(0);
}

Tuvia
Reply With Quote
  #5 (permalink)  
Old 07-06-2008
Registered User
 

Join Date: May 2008
Posts: 3
Stumble this Post!
Quote:
Originally Posted by ramen_noodle View Post
If your signal handling function is declared in main(), in the parent, before any fork and the children don't deviate from this handling then the the entire process group will react according to the described signal handler.
The signal handler is declared after the fork. This is one of the first things that the child processes do after they are invoked. Should this be done before the fork?

Thanks, Tuvia
Reply With Quote
  #6 (permalink)  
Old 07-07-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 541
Stumble this Post!
Declare the USR1 signal handler in both child and parent. This explains why some times the process exits and sometimes it seems that nothing has happened. Usually exit is the default disposition of most signals so putting it inside your handler is not good signal handling practice. Normally signal handlers trap a signal and when they return the process execution picks up from where it left off that is the process does not terminate.
Reply With Quote
  #7 (permalink)  
Old 07-17-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,849
Stumble this Post!
You should not call stdC library functions like fwrite in a signal handler. They are not atomic, so they may not complete in the event of multiple signals. Try write().
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0