在父进程出口处杀死子进程 [英] killing child processes at parent process exit

查看:165
本文介绍了在父进程出口处杀死子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C和编程非常陌生,需要一些帮助.在linux(cygwin)上的c中,我需要在退出时删除所有子进程.我看过其他类似的问题,但无法正常工作.我尝试过-

I'm very new to c and programming and need some help. In c on linux(cygwin) I am required to remove all child processes at exit. I have looked at the other similar questions but can't get it to work. I've tried-

atexit(killzombies); //in parent process

void killzombies(void)
{
    printf("works");
    kill(0, SIGTERM);
    printf("works");
    if (waitpid(-1, SIGCHLD, WNOHANG) < 0)
         printf("works");
}

由于某种原因,作品"甚至无法打印.我按Ctrl + C退出.

for some reason, "works" doesn't even print ever. I press ctrl + c to exit.

我也尝试过-

prctl(PR_SET_PDEATHSIG, SIGHUP); //in child process
signal(SIGHUP, killMe);

void killMe()
{
    printf("works");
    exit(1);
}

但是因为我正在使用cygwin,所以当我#include <sys/prctl.h>时,cygwin说它找不到文件或目录,并且我不知道要安装什么软件包. 另外,如果我的prctl()函数正常工作,那会杀死所有僵尸吗?

but because I'm using cygwin, when I #include <sys/prctl.h>, cygwin says it can't find the file or directory and I don't know what package to install for it. Also, if my prctl() function were to work, would that kill all the zombies?

我的程序是一个客户端服务器,我的服务器forks()处理每个客户端.我想在服务器关闭时不留下任何僵尸.

My program is a client server and my server forks() to handle each client. I'm suppose to leave no remaining zombies when the server shuts down.

推荐答案

来自atexit(3)的Linux文档:

From the Linux documentation of atexit(3):

如果在以下情况下不调用

使用atexit()(和on_exit(3))注册的功能: 一个过程由于信号的传递而异常终止.

Functions registered using atexit() (and on_exit(3)) are not called if a process terminates abnormally because of the delivery of a signal.

如果您希望在应用程序收到SIGINT或SIGTERM时进行清理,则需要安装适当的信号处理程序并在其中进行工作.

If you want to cleanup when your application receives a SIGINT or SIGTERM, you'll need to install the appropriate signal handlers and do your work there.

这篇关于在父进程出口处杀死子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆