I'm am currently pursuing a study about signals in Unix and I have a
question involving the use of the functions "sigaction()"and /or
"signal()" for catching and handling signals.
Consider the following sample program:
/* signal_int.c */
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
/* Simple Signal handling Function */
void foo( int sig )
{
printf("I got signal %d\n", sig);
}
int main()
{
struct sigaction newaction;
newaction.sa_handler = foo;
if ( sigaction( SIGINT, &newaction, NULL ) == -1)
perror("Could not install the new signal handler");
while (1) {
printf("Hello Ball!! \n");
sleep(1);
}
}
When the above program is run it begins printing "Hello Ball!!" to
stdout. If we hit Ctrl-C to generate the SIGINT signal the program
catches this signal and executes the signal handler void(foo(int)),
and then the program just continues on printing "hello ball" to
stdout. Now, here's the part that I am a bit puzzled over. According
to what I read in my text and what I interpret from the man pages, at
this point the signal handler should be automatically restored to
default which implies that an additional Cntl-C generating a SIGINT
would cause the program to terminate, which is the default signal
handling behavior. However when I run this program under Linux such is
not the case, the default behavior is not restored and the program
continues to print "hello ball" regardless of however many times we
hit Cntl-C.
I am curious as to why this is the case? Is it that the default signal
handling is not restored automatically in all flavors of Unix,Linux
included? I am puzzled because the textbook and man pages seem to
imply that the program should take on its default signal handling
action automatically when it receives a second SIGINT.
The same situation occurs when using "signal()" rather than
"sigaction()".
In any event I found that by by using the flag:
newaction.sa_flags = SA_ONESHOT;
in the above program causes the default behavior to be restored and
the program will terminate when it receives a second SIGINT...
Any hints, suggestions, etc. always appreciated.
Thanks,
/John <[EMAIL PROTECTED]>
--
email: [EMAIL PROTECTED]
Local mailserver <landreau.ruffe.edu> , remote <ns.computer.net>
Do you think World War 3 is a Coney Island of the mind?