简体   繁体   English

Waitpid等待已终止的子进程

[英]Waitpid waiting on defunct child process

In case of crash we dump the stack to get more information about the crash using below function: 如果发生崩溃,我们使用以下函数转储堆栈以获取有关崩溃的更多信息:

static void dumpStack()
    {
        char buf[64];

        pid_t pid = getpid();

        sprintf( buf, "%d", pid );

        pid_t fork_result = vfork();

        int status;

        if( fork_result == 0 )

            execlp( "pstack", "pstack", buf, NULL );

        else if( fork_result > 0 )

            waitpid( fork_result, &status, 0 );

        else

            std::cerr << "vfork failed with err=%s" << strerror( errno ) << std::endl;
    }

in above code the parent stuck at waitPid forever. 在上面的代码中,父级永远停留在waitPid上。 I checked the status of child process it became zombie: 我检查了子进程变成僵尸的状态:

Deepak@linuxPC:~$ ps aux | grep  21054

    700048982 21054 0.0  0.0      0     0 pts/0    Z+   03:01   0:00 [pstack] <defunct>

Also the stack printed by the child is also not complete. 而且孩子打印的纸叠也不完整。 It just prints a single line and exits. 它只打印一行并退出。

#0  0x00007f61cb48d26e in waitpid () from /lib64/libpthread.so.0

Not sure why parent is not able to reap the process. 不知道为什么父母不能收割过程。

Can you please help if i am missing anything here 如果我在这里缺少任何东西,可以请你帮忙

First of all, you may be better off using the backtrace() function, see How to automatically generate a stacktrace when my program crashes 首先,最好使用backtrace()函数,请参见如何在程序崩溃时自动生成堆栈跟踪

As for your code, if you are on 64-bit Linux (likely), pstack will not work. 至于您的代码,如果您使用的是64位Linux(可能),则pstack将不起作用。 For me, it segfaulted. 对我来说,它断断续续。 Furthermore, I agree with the comments on vfork() and execlp(). 此外,我同意对vfork()和execlp()的评论。 Also, you may need to execute your program as root. 另外,您可能需要以root用户身份执行程序。 The code below works for me (prints parent's stack, but not sure that is very useful): 下面的代码对我有用(打印父级的堆栈,但不确定是否非常有用):

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <iostream>
#include <system_error>

using std::cout;

static void dumpStack() {
  char buf[64];
  pid_t result;
  pid_t pid = getpid();
  sprintf(buf, "/proc/%d/stack", pid );
  //cout << buf << '\n';                                                                                                         
  pid_t fork_result = vfork();
  int status;
  if( fork_result == 0 ) {
    //execlp( "pstack", "pstack", buf, NULL );                                                                                   
    cout << "Child before execlp\n";
    execlp("cat", "cat", buf, NULL);
    cout << "Child after execlp\n";  // Will not print, of course
  } else if( fork_result > 0 ) {
    result = waitpid( fork_result, &status, 0 );
    if (result < 0) {
      throw std::system_error(errno, std::generic_category());
    }
  } else {
    std::cerr << "vfork failed with err=%s" << strerror( errno ) << std::endl;
  }
  cout << std::endl;
}

int main() {
  dumpStack();

  return 0;
}

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

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