简体   繁体   English

子进程在fork和exec之后变为Defunct

[英]Child process becomes Defunct after fork and exec

I am learning fork and exec and creating multiple child processes using fork and execlp and all I do in the child process is let it sleep. 我正在学习fork和exec,并使用fork和execlp创建多个子进程,而我在子进程中所做的只是让它休眠。 Basically I just want all my child to be alive. 基本上,我只是希望我的所有孩子都还活着。 But as soon as i start my monitor.cpp which creates processes all of the child exit immediately and they do defunct! 但是,一旦我启动了monitor.cpp,它立即创建了所有子进程的进程,它们退出了!

Monitor which forks multiple children 监视哪个分叉多个孩子

#include <iostream>
#include <thread>
#include <chrono>
#include <string>

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
 for(size_t i=0; i<std::stoi(argv[1]) ; ++i)
 {
  int pid = fork();
  if(pid == 0)
  {
    execlp("child", "child", std::string(std::to_string(i)).c_str(), (char *)0);
    std::cout << "child exiting " << std::endl;
    exit(1);
  }
  else if(pid > 0)
  {
   std::cout <<"child started with " << pid << std::endl;
  }
  else
  {
   std::cout << "fork failed" << std::endl;
  }
 }

 while(true)
 {
  std::this_thread::sleep_for(std::chrono::seconds(100000));
 }

 return 0;
}

Child Code 子代码

#include <iostream>
#include <thread>
#include <chrono>

int main(int argc, char* argv[])
{
  std::cout << " child started with id " << argv[1] << std::endl;

  std::cout <<"child sleeping " << argv[1] << std::endl;
  std::this_thread::sleep_for(std::chrono::seconds(1000));

  std::cout << "child exiting " << argv[1] << std::endl;
  return 0;
}

Output: 输出:

 child started with 1834 child started with 1835 child exiting child started with 1836 child exiting child started with 1837 child started with 1838 child started with 1839 child exiting child started with 1840 child started with 1841 child exiting child started with 1842 child started with 1843 child exiting child exiting child exiting child exiting child exiting child exiting 

ps -ef shows all of my child processes as Defunct even though my parent is still alive. ps -ef将我的所有所有子进程显示为“已终止”,即使我的父母还活着。

Can you please explain what am I missing? 你能解释我在想什么吗?

From the 'execlp' man page: 在“ execlp”手册页中:

The exec() functions only return if an error has occurred. exec()函数仅在发生错误时返回。 The return value is -1, and errno is set to indicate the error. 返回值为-1,并且将errno设置为指示错误。

Since "child exiting" is being printed in two places, it's not obvious if it's exiting. 由于"child exiting"是在两个位置打印的,因此是否退出并不明显。 You need to check it's return value and errno . 您需要检查它的返回值和errno

You need to reap the child-process as they exit. 您需要在子进程退出时获得其子进程。 This is done using wait or waitpid calls. 这是使用waitwaitpid调用完成的。

Until the parent has done this, they will be visible as defunc / zombie processes. 在父级执行此操作之前,它们将作为defunc / zombie进程可见。 (init, process 1, is responsible for reaping all process that do not have a parent after they exit) (init,进程1,负责在退出后获得没有父级的所有进程)

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

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