Shibani
Shibani

Reputation: 148

To handle interrupts:

In the following program:

Ctrl+z and ctrl+c both are interrupts.
The code is supposed to handle any interrupt.


Then why does only one of them(ctrl+c) work?

Code:

#include <signal.h>
#include<stdio.h>
void handler(int sig)
{
    printf("Caught SIGINT\n");
    exit(0);
}

int main() 
{
    printf("\nYou can press ctrl+c to test this program\n");
    if (signal(SIGINT, handler) == SIG_ERR) 
    perror("signal error");

    pause(); /* wait for the receipt of a signal */

    exit(0);
}

Input by the user: has to be an interrupt Output must be: Caught sigint

Upvotes: 1

Views: 164

Answers (2)

Karoly Horvath
Karoly Horvath

Reputation: 96258

Ctrl-Z sends SIGTSTP.

Things to read:

Upvotes: 4

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

Because Ctrl-Z causes a SIGTSTP, not a SIGINT.

Upvotes: 7

Related Questions