Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 避免在C+中产生僵尸进程+;_C++_Concurrency_Fork_Zombie Process - Fatal编程技术网

C++ 避免在C+中产生僵尸进程+;

C++ 避免在C+中产生僵尸进程+;,c++,concurrency,fork,zombie-process,C++,Concurrency,Fork,Zombie Process,非常奇怪的虫子,也许有人会看到我丢失的东西 我有一个C++程序,它可以把BASH shell拆散,然后将命令传递给它。 周期性地,这些命令将包含废话,bash进程将挂起。我使用semtimedwait检测到这一点,然后运行如下小函数: if (kill(*bash_pid, SIGKILL)) { cerr << "Error sending SIGKILL to the bash process!" << endl; exit(1); } else {

非常奇怪的虫子,也许有人会看到我丢失的东西

我有一个C++程序,它可以把BASH shell拆散,然后将命令传递给它。 周期性地,这些命令将包含废话,bash进程将挂起。我使用semtimedwait检测到这一点,然后运行如下小函数:

if (kill(*bash_pid, SIGKILL)) {
    cerr << "Error sending SIGKILL to the bash process!" << endl;
    exit(1); 
} else {
    // collect exit status
    long counter = 0;
    do {
        pid = waitpid(*bash_pid, &status, WNOHANG);
        if (pid == 0) { // status not available yet
            sleep(1);
        }
        if(counter++ > 5){
            cerr << "ERROR: Bash child process ignored SIGKILL >5 sec!" << endl;
        }
    } while (pid != *bash_pid && pid != -1);
    if(pid == -1){
        cerr << "Failed to clean up zombie bash process!" << endl;
        exit(1);
    }

    // re-initialized bash process
    *bash_pid = init_bash();
 }
if(kill(*bash_pid,SIGKILL)){

cerr我读过的文章指出,僵尸进程的原因是子进程执行退出,而父进程从不收集子进程的退出

本文提供了一种技术,即使用SIGKILL以外的其他信号,例如SIGTERM

这不应该被使用

其中一种技术是杀死父进程,从而也杀死其子进程,包括任何僵尸进程。作者指出,在操作系统重新启动之前,似乎有一些子进程仍然是僵尸进程


您没有提到用于将命令传递给子进程的机制。但是,一个选项可能是通过断开子进程与其父进程的连接来释放子进程,类似于终端进程的子进程与终端会话的断开连接方式。这样子进程将成为其自己的进程,并且如果存在p问题可能会退出而不会变成僵尸。

一个有效的解决方法是调用信号(SIGCHLD,SIG_IGN);位于main的顶部,但这并不理想。我想直接获取子进程。嗨,Richard,谢谢你的想法。为了澄清,子进程最初没有死。我提供的代码首先杀死进程,然后尝试使用waitpid收集其退出状态。不知怎的,似乎可以通过t子进程退出。@JohnDoucette,hmmm。所以我想知道在发送信号后,在对pid进行等待之前,睡眠100毫秒的结果如何。