holotko wrote:

> 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);
>    }
> }

Note: you're not initialising the sa_mask or sa_flags fields of
newaction, so the results will be undefined.

> 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.

The signal handler is only removed if you specify SA_ONESHOT in the
sa_flags field, which you aren't doing.

> 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?

Correct. You have to specify that you want this behaviour using
SA_ONESHOT. Normally you don't want the default signal disposition to
be restored.

> 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.

Which manpages and textbook are you reading? Everything which I've
seen documents sigaction() correctly.

> The same situation occurs when using "signal()" rather than
> "sigaction()".

signal() is a different matter. There are two distinct sets of
semantics. The SysV version of signal() is equivalent to using
sigaction() with an sa_flags field of SA_ONESHOT | SA_NODEFER. The BSD
version is equivalent to using sigaction() with an sa_flags field of
SA_RESTART.

> In any event I found that by by using the flag:
> 
>         newaction.sa_flags = SA_ONESHOT;

Which is what you should be doing.

> in the above program causes the default behavior to be restored and
> the program will terminate when it receives a second SIGINT...

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to