[英]Waitpid waiting on defunct child process
如果發生崩潰,我們使用以下函數轉儲堆棧以獲取有關崩潰的更多信息:
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;
}
在上面的代碼中,父級永遠停留在waitPid上。 我檢查了子進程變成僵屍的狀態:
Deepak@linuxPC:~$ ps aux | grep 21054
700048982 21054 0.0 0.0 0 0 pts/0 Z+ 03:01 0:00 [pstack] <defunct>
而且孩子打印的紙疊也不完整。 它只打印一行並退出。
#0 0x00007f61cb48d26e in waitpid () from /lib64/libpthread.so.0
不知道為什么父母不能收割過程。
如果我在這里缺少任何東西,可以請你幫忙
首先,最好使用backtrace()函數,請參見如何在程序崩潰時自動生成堆棧跟蹤
至於您的代碼,如果您使用的是64位Linux(可能),則pstack將不起作用。 對我來說,它斷斷續續。 此外,我同意對vfork()和execlp()的評論。 另外,您可能需要以root用戶身份執行程序。 下面的代碼對我有用(打印父級的堆棧,但不確定是否非常有用):
#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.