Why does the signal manager only work for my first ctrl + c?

advertisements

Here is my code:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

static void sigint_handler(int signo){
    printf("%s\n", "hello world!");
    for(;;){
        pause();
    }
}

int main()
{
    signal(SIGINT, sigint_handler);
    for(;;){
        pause();
    }
    return 0;
}

And in action:

Korays-MacBook-Pro:~ koraytugay$ gcc tugay.c
Korays-MacBook-Pro:~ koraytugay$ ./a.out
^Chello world!
^C^C^C^C^C^C

Why don't I see the hello world message after I hit CTRL + C for the second, third, forth time..?

My understanding is the process should catch CTRL C again, but obviously it is wrong. But why?


On BSD, when a signal handler is invoked, the signal disposition is not reset, and further instances of the signal are blocked from being delivered while the handler is executing.

And since mac osx is partly based on BSD, once the code is run on a mac osx, once second exception is called, it will be pending and wait for the handler of the first exception to exit. But since you will never exit, you have a deadlock.

Read my full explanation here