使用信号杀死 C 中的进程
我的程序中的主进程又分叉了 3 个进程,进程 ID 为 pid1、pid2、pid3。 pid1和pid2进程处于无限循环中。我想要的是当 pid3 进程结束时,包括主进程在内的所有进程都将终止。截至目前,我正在使用 : ,
wait(pid3);
kill(0, SIGKILL);
它按照我所说的执行上述所有操作,但它在终端上打印 Killed 。我不希望信号显示已杀死,实际上什么也没有,而是优雅地退出程序。我怎样才能做到这一点?
The main process in my program forks 3 more process with say process ids as pid1, pid2, pid3. Pid1 and pid2 processes are in infinite loop. What I want is when pid3 process is over all the process including the main are terminated. As of now, I am using :
wait(pid3);
kill(0, SIGKILL);
which do all above as i said, but it prints Killed on the terminal. I don't want the signal to display killed and infact nothing, but gracefully exits from the program. How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请改用
SIGTERM
,并安装一个信号处理程序,在接收到SIGTERM
时执行干净退出(例如exit(0)
)。Use
SIGTERM
instead, and install a signal handler that performs a clean exit (e.g.exit(0)
) on receivingSIGTERM
.另一种方法是在进程之间设置一个
pipe()
,并让 pid1 和 pid2 进程在管道上看到文件结尾时退出。An alternate method is to set up a
pipe()
between the processes, and have the pid1 and pid2 processes exit when they see end-of-file on their pipe.