有人可以建议我一个简单的方法来创建僵尸程序,几分钟之内无法收割。
这样做的目的是测试父进程在僵尸进程再次变得可收割之后是否能够收割僵尸进程。

here发现了一种无法收割的僵尸的情况。我猜可能会有更简单的方法。

操作系统:Linux

首选语言:C / C ++

最佳答案

这是一个小程序,它将导致僵尸在您的“ ps aux”输出中显示几分钟。注意注释掉的waitpid()调用;如果取消注释该调用,则僵尸将不会显示,因为waitpid()调用会导致父进程声明其死子。

(免责声明:我只在MacOS / X上进行了测试,因为那是我使用的计算机,但在Linux下应该可以正常使用)

#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
   printf("Starting Program!\n");

   int pid = fork();
   if (pid == 0)
   {
      printf("Child process %i is running!\n", getpid());
      sleep(300);  // wait 5 minutes
      printf("Child process %i is exiting!\n", getpid());
      return 0;
   }
   else if (pid > 0)
   {
      printf("Parent process is continuing child's pid is %i...\n", pid);
      sleep(5);  // just for clarity
      printf("Parent process is sending SIGKILL to child process...\n");
      if (kill(pid, SIGKILL) != 0) perror("kill");
      // waitpid(pid, NULL, 0);  // uncomment this to avoid zombie child process

      printf("Parent process is sleeping... ps aux should show process #%i is a zombie now\n", pid);
      sleep(500);  // just for clarity
      printf("Parent process is exiting!\n");
   }
   else perror("fork()");
}

关于c++ - 如何创建无法在几分钟内收获的僵尸进程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26705907/

10-11 18:12