Signal Default Action


 
Thread Tools Search this Thread
Top Forums Programming Signal Default Action
# 1  
Old 11-28-2007
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 ?
# 2  
Old 11-28-2007
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

# 3  
Old 11-28-2007
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().
# 4  
Old 11-28-2007
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 ?
# 5  
Old 11-28-2007
Quote:
Originally Posted by matrixmadhan
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().
# 6  
Old 11-28-2007
Quote:
Originally Posted by Perderabo
I would use the term "unpredictable".
I would use the term "platform dependent". Smilie

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.
# 7  
Old 11-28-2007
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 ?
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

multiple action!

lets explain it easy by showing the initial file and desired file: I've a file such this that contains: initial_file: 31/12/2011 23:46:08 38.6762 43.689 14.16 Ml 3.1 ... (1 Reply)
Discussion started by: oreka18
1 Replies

2. AIX

Received signal #11, SIGSEGV [default] on AIX 6.1

Hello, One of our customer is getting segmentation fault when he runs his shell script which invokes our executable on AIX 6.1. On AIX 5.3, there were no issues. Here is the truss output. 811242: __loadx(0x0A040000, 0xF0D3A26C, 0x00000000, 0x00000009, 0x00000000) = 0xF026E884... (0 Replies)
Discussion started by: erra_krishna
0 Replies

3. Shell Programming and Scripting

Need help with find its action

I am writing a shell script that takes at least 2 arguments. The first is an octal representation of file permissions, the second is a command that is executed on all the files found with that permission. #!/bin/sh find . -perm $1 -exec $2 $3 $4 {} \; invoked: ./script.sh 543 ls -la what... (3 Replies)
Discussion started by: computethis
3 Replies

4. Shell Programming and Scripting

same action in then as in else: explanation?

in /etc/init.d/networking of an ubuntu computer, I found this code: if ifdown -a --exclude=lo; then log_action_end_msg $? else log_action_end_msg $? fi Shouldn't it be replace by ifdown -a --exclude=lo ... (0 Replies)
Discussion started by: raphinou
0 Replies

5. Shell Programming and Scripting

action command

Hi.. When i refered the script /etc/rc.sysinit... i found the "action commands" like But this is not working in my shells.. the following error is coming... Please anybody help Thanks in advance esham (5 Replies)
Discussion started by: esham
5 Replies

6. UNIX for Dummies Questions & Answers

any idea to repeat a action in VI

Any idea to repeat an action to all the lines in vi... suppose i want to delete the first word from all the lines in VI .. how would i do it ? in general i am also looking for a way to apply a action to all the lines in VI . (6 Replies)
Discussion started by: myelvis
6 Replies
Login or Register to Ask a Question