C语言 忽略子进程中的 SIGINT 信号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12953350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:08:45  来源:igfitidea点击:

Ignore SIGINT signal in child process

csignals

提问by SpyrosR

I am writing a simple program in which parent and child process are alternatively printing into a file. I have managed to do this using user defined signals. Now I want to handle the SIGINTsignal. Once ctrl-cis received the parent must send termination signal to child,the child should then should terminate and finally the parent should terminate.

我正在编写一个简单的程序,其中父进程和子进程交替打印到文件中。我已经设法使用用户定义的信号来做到这一点。现在我想处理SIGINT信号。一旦ctrl-c收到,父级必须向子级发送终止信号,子级应该终止,最后父级终止。

My question is, in order to make this work properly I must catch the SIGINTsignal ONLY from parent and IGNORE it from child. Is it right? If yes any hints on doing this?

我的问题是,为了使这项工作正常进行,我必须SIGINT仅从父母那里捕捉信号,而从孩子那里忽略它。这样对吗?如果是的话,有什么关于这样做的提示吗?

回答by hmjd

Call:

称呼:

signal(SIGINT, SIG_IGN);

from the child process which will make the child process ignore the SIGINTsignal. From man signal:

来自子进程这将使子进程忽略SIGINT信号。从人信号

If the disposition is set to SIG_IGN, then the signal is ignored.

如果处置设置为 SIG_IGN,则忽略该信号。