2

I am new to signals in Unix, the purpose of this exercice is to disable SIGINT for stopping the program and use SIGQUIT instead, and it is successfully ignoring SIGINT when i press ctrl-c but the printf isnt showing until i press the Ctrl-/ which is for the SIGQUIT, and all the messages appear at once.

Example: if i press ctrl-c 4 times then ctrl-/: Only after pressing ctrl-\ i get 4 messages saying i have pressed Ctrl-c then the quitting you sent the SIGQUIT signal.

If i press ctrl-c only i get nothing but at least it is being ignored

struct sigaction action;

void hand(int sig)
{
    switch(sig){
    case SIGINT:
        printf("You have presed ctrl-c");
        break;
    case SIGQUIT:
        printf("Quittin you sent a SIGQUIT signal");
        exit(SIGQUIT);
        break;
    default:
        printf("WRONG SIGNAL");
        exit(-1);
        break;
    }
}

int main()
{       
    action.sa_handler=hand;
    printf("Ctrl-C disabled, press Ctrl-/ to quit...\n");
    sigaction(SIGINT,&action,NULL);
    sigaction(SIGQUIT,&action,NULL);
    while(1){
        sleep(1);
    }
}
dbush
  • 205,898
  • 23
  • 218
  • 273
Elio
  • 73
  • 9

2 Answers2

6

Two issues here. First, printf is line buffered by default and your aren't printing a newline, so the output buffer may not be flushed when you expect. Second, the printf function is not async-signal-safe, meaning it's undefined behavior to call it in a signal handler.

Change your signal handler to set a flag instead of printing, then check that flag in your loop and print (with \n at the end of the string) if it is set.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

In addition to @dbush answer, you can use write in the signal handler, it is async-signal-safe and no buffering delays its output.

Example:

static char const msg[] = "You have presed ctrl-c\n";
write(STDOUT_FILENO, msg, sizeof msg - 1);
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271