当前位置: 动力学知识库 > 问答 > 编程问答 >

c - Catch all signals that would stop a program

问题描述:

I've got a program to catch any terminating signals. Currently all it does is catch Ctrl + C. I want to be able to catch Ctrl + Z and Ctrl + \ in addition to Ctrl + C. Here's part of my code.

if (signal(SIGINT, SIG_IGN) == SIG_ERR)

fprintf(stderr,"Error: Can't catch SIGINT\n");

if (signal(SIGTERM, SIG_IGN) == SIG_ERR)

fprintf(stderr,"Error: Can't catch SIGTERM\n");

// do stuff

What other signals to I need to implement to catch Ctrl + Z and Ctrl + \? And what raises SIGTERM? Thanks in advance.

网友答案:

Ctrl+Z raises SIGTSTP. Ctrl+\ raises SIGQUIT.

I don't know of any key combination that raises SIGTERM, but of course you can send it using kill -TERM <pid> replacing <pid> with the process ID. (Or just kill <pid>; SIGTERM is the default signal for kill)

Note that you can't catch SIGSTOP nor SIGKILL.

分享给朋友:
您可能感兴趣的文章:
随机阅读: