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

代码:

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

用户输入:必须是中断 输出必须是:捕获信号

4

2 回答 2

7

因为 Ctrl-Z 导致 a SIGTSTP,而不是 a SIGINT

于 2013-03-02T14:07:37.200 回答
4

Ctrl-Z 发送SIGTSTP.

阅读内容:

于 2013-03-02T14:09:17.970 回答