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
CEP: What about the action? iBot Complex Event Processing RSS News 0 02-15-2008 08:20 AM
tracking user action xitrum UNIX for Advanced & Expert Users 7 03-07-2007 01:48 PM
action command esham Shell Programming and Scripting 5 07-30-2005 01:04 AM
action based on rsync status dangral UNIX for Dummies Questions & Answers 2 04-29-2005 09:25 AM
any idea to repeat a action in VI myelvis UNIX for Dummies Questions & Answers 6 11-26-2003 03:21 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-27-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Signal Default Action

Code:
#include <signal.h>

void handler(int sig) {
  int i;
  for( i=0; i<=999999; i++ ) {
  }
  printf("Received signal: %d\n", sig);
}

int main() {
   signal(SIGINT, handler);
   while (1) {
   }
   return 0;
}
When SIGINT is delivered for the first time, shouldn't the handler code be executed and its action default to SIG_DFL of SIGINT so that the next time when SIGINT is delivered it should logically terminate

Its not happening so,

each time a SIGINT is delivered, handler is executing without any problems.

Any thoughts on this ?
Reply With Quote
Forum Sponsor
  #2  
Old 11-27-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Surprisingly

it works as expected in solaris and hp - that is terminate when the second SIGINT is delivered

but not in Linux ( RHEL3 )

Could there be any difference in the signal standards ?


Code:
uname -a
Linux 2.4.21-37a8
Reply With Quote
  #3  
Old 11-27-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,663
Resetting the handler is dumb. That is the original behavior, but during the window between the automatic reset and the reinstallation of the handler, a signal can be lost. So BSD changed signal(). On HP-UX, you can get either behavior by linking with different libraries. Anyway, both behaviors were in use when Posix came along. So, according to the Posix standard:
Quote:
When a signal occurs, and func points to a function, it is implementation-defined whether the equivalent of a:
signal(sig, SIG_DFL);


is executed or the implementation prevents some implementation-defined set of signals (at least including sig) from occurring until the current signal handling has completed. (If the value of sig is SIGILL, the implementation may alternatively define that no action is taken.)
And this is why it also says:
Quote:
The sigaction() function provides a more comprehensive and reliable mechanism for controlling signals; new applications should use sigaction() rather than signal().
Reply With Quote
  #4  
Old 11-27-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
The sigaction() function provides a more comprehensive and reliable mechanism for controlling signals; new applications should use sigaction() rather than signal().
I perfectly agree with the above and have been using that only.

Am just curious to know about the behavior of signal(); and why its behavior is different in different systems.

Is it due to the reason that different libraries are in use ? If so, how to identify them ?

Quote:
from occurring until the current signal handling has completed
Am confused about this too, there is no guarantee with the older semantics that the signal of same type will not be delivered while the current function block for the signal is being executed.

In that case, there is no reason for it to wait till the function block is executed.

In short, can it be said signal() behavior is undefined - or am I being rude in saying that ?
Reply With Quote
  #5  
Old 11-27-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,663
Quote:
Originally Posted by matrixmadhan View Post
In short, can it be said signal() behavior is undefined - or am I being rude in saying that ?
"Undefined" is a little harsh. It's not like signal() is allowed to whistle dixie or anything. I would use the term "unpredictable". A standard signal() has several attributes each of which must be one of two possible behaviors. I thought that I explained "why":

Quote:
Resetting the handler is dumb. That is the original behavior, but during the window between the automatic reset and the reinstallation of the handler, a signal can be lost. So BSD changed signal().
Reply With Quote
  #6  
Old 11-27-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by Perderabo View Post
I would use the term "unpredictable".
I would use the term "platform dependent".

Use sigaction() and you will get rewards in this life as well as the next from the Great God Posix.

In Visual C++ you have to reinstall the signal handler in order to catch it again.
Reply With Quote
  #7  
Old 11-27-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
I would use the term "platform dependent".
I think ' platform dependent ' should be more appropriate.

As I could see the difference when executing the same code in HP, Solaris, RHEL3. - Different behavior

Porter, is this what you meant ?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:26 PM.


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

Content Relevant URLs by vBSEO 3.2.0