简体   繁体   English

在父进程出口处杀死子进程

[英]killing child processes at parent process exit

I'm very new to c and programming and need some help. 我对C和编程非常陌生,需要一些帮助。 In c on linux(cygwin) I am required to remove all child processes at exit. 在linux(cygwin)上的c中,我需要在退出时删除所有子进程。 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");
}

for some reason, "works" doesn't even print ever. 由于某些原因,“作品”甚至都不会打印。 I press ctrl + c to exit. 我按Ctrl + C退出。

ALSO I have tried- 我也尝试过-

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

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

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. 但是因为我使用的是cygwin,所以当我#include <sys/prctl.h> ,cygwin说它找不到文件或目录,并且我不知道要为其安装什么软件包。 Also, if my prctl() function were to work, would that kill all the zombies? 另外,如果我的prctl()函数正常工作,那会杀死所有僵尸吗?

My program is a client server and my server forks() to handle each client. 我的程序是一个客户端服务器,我的服务器forks()处理每个客户端。 I'm suppose to leave no remaining zombies when the server shuts down. 我想在服务器关闭时不留下任何僵尸。

Your waitpid does not supply the usual parameters, I'm surprised it does not crash. 您的waitpid没有提供通常的参数,我很惊讶它没有崩溃。 The prototype is: 原型是:

pid_t waitpid(pid_t pid, int *status, int options); 

The second parameter should be a pointer to an int , you are supplying an int . 第二个参数应该是一个指向 int指针 ,您正在提供一个int Notice also that you should call waitpid for each child, you are only calling it for one. 还要注意,您应该为每个孩子调用waitpid ,而您只为其中一个调用它。

atexit() is only called if you exit normally. 仅当您正常退出时才调用atexit() If you are exiting through CTRL+C then you need to call your function from a handler on SIGINT. 如果要通过CTRL + C退出,则需要从SIGINT的处理程序中调用函数。

From the Linux documentation of atexit(3) : atexit(3)的Linux文档中:

Functions registered using atexit() (and on_exit(3) ) are not called if a process terminates abnormally because of the delivery of a signal. 如果进程由于传递信号而异常终止,则不会调用使用atexit() (和on_exit(3) )注册的函数。

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. 如果您想在应用程序收到SIGINT或SIGTERM时进行清理,则需要安装适当的信号处理程序并在其中进行工作。

You'll need to keep track of how many children your process has and then call wait that many times. 您需要跟踪您的进程有多少个子进程,然后多次调用wait。 Also, as others have said, your atexit() function won't be called if the process is terminated by a signal, so you'll need to call killzombies() from a signal handler as well. 而且,正如其他人所说,如果进程被信号终止,则不会调用atexit()函数,因此您还需要从信号处理程序中调用killzombies()。 You'll need something like: 您将需要以下内容:

int n_children = 0; // global

void handle_sig(int sig) {
    killzombies();
    exit(sig);
}

// your atexit()
void killzombies() {
    kill(0, SIGKILL);
    while (n_children > 0) {
        if (wait(NULL) != -1) {
            n_children--;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM