简体   繁体   English

waitPID function 不检查子进程是否退出

[英]waitPID function not checking if the child process exited

I am unable to get the waitPid to tell me if the child process exited or not.我无法让waitPid告诉我子进程是否退出。 In the parent process this is what I am doing:在父进程中,这就是我正在做的事情:

if (forkID != 0) {
    
        pid_t result = waitpid(forkID, &status, WNOHANG);
        if (result == 0) {
          // Child still alive
            std::cout<<"Child Alive"<<std::endl;
        } else if (result == -1) {
          // Error
            std::cout<<"Error"<<std::endl;
        } else {
          // Child exited
            std::cout<<"Child Exited"<<std::endl;
        }

        DriverParser P;
        while (!std::cin.eof()) {

             std::string line;
             std::getline(std::cin, line);

              arguments = P.BeginParsing(line);
              if (arguments.size() > 0) {
                  for (int k = 0; k < 4; k++) {
                      finalArguments[k] = arguments[k];
                  }
                  
                  close(fd[0]);
                  write(fd[1],finalArguments,4*sizeof(int));
                  close(fd[1]);
                  
                  
                  
              }
         }
        
        
       // pid = wait(&status);
        
      //  std::cout<<waitpid(forkID, &status, 0)<<std::endl;
    

}else if (forkID == 0) {
   // child: reading only, so close the write-descriptor
    
    std::cout<<"I am child process my PID is: "<<getpid()<<std::endl;

    execv("./a.out", argList);
    
    // close the read-descriptor
    close(fd[0]);
    
   
    
}

I am basically sending the result of the parser as it reads from the stdin via pipe to child process, and then executing a process in execv.当解析器通过 pipe 从标准输入读取时,我基本上将解析器的结果发送到子进程,然后在 execv 中执行一个进程。 Now when this program exits using exit(0) - I am not seeing Child Exited on the terminal which means the waitpid is essentially not working for me.现在,当该程序使用exit(0)时 - 我没有在终端上看到Child Exited ,这意味着waitpid基本上对我不起作用。

What should I do?我应该怎么办?

You are calling waitpid with WNOHANG :您正在使用WNOHANG调用waitpid

pid_t result = waitpid(forkID, &status, WNOHANG);

I shared with you the documentation , which says:我与您分享了文档,其中说:

If WNOHANG was specified in options and there were no children in a waitable state, then waitid() returns 0 immediately and the state of the siginfo_t structure pointed to by infop is unspecified.如果在选项中指定了 WNOHANG,并且在可等待的 state 中没有子级,则 waitid() 立即返回 0,并且 infop 指向的 siginfo_t 结构的 state 未指定。 To distinguish this case from that where a child was in a waitable state, zero out the si_pid field before the call and check for a nonzero value in this field after the call returns.为了将这种情况与孩子处于可等待 state 中的情况区分开来,请在调用之前将 si_pid 字段清零,并在调用返回后检查该字段中的非零值。

It also says:它还说:

WNOHANG万航
return immediately if no child has exited.如果没有孩子退出,则立即返回。

Considering that the call to waitpid happens immediately after fork, the chances are that the child is still running.考虑到对waitpid的调用是在 fork 之后立即发生的,孩子很可能仍在运行。

If you want to wait for the child process to end, just remove the WNOHANG option (pass 0 instead):如果要等待子进程结束,只需删除WNOHANG选项(改为传递0 ):

pid_t result = waitpid(forkID, &status, 0);

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

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