C++ Waitpid正在等待已失效的子进程
在崩溃的情况下,我们使用以下函数转储堆栈以获取更多关于崩溃的信息:C++ Waitpid正在等待已失效的子进程,c++,linux,fork,waitpid,pstack,C++,Linux,Fork,Waitpid,Pstack,在崩溃的情况下,我们使用以下函数转储堆栈以获取更多关于崩溃的信息: 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 )
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;
}
不知道为什么父母不能收获这个过程
如果我在这里遗漏了什么,您能帮我吗首先,您最好使用backtrace()函数,请参见 至于您的代码,如果您使用的是64位Linux(很可能),pstack将无法工作。对我来说,这是错误的。此外,我同意关于vWork()和execlp()的评论。此外,您可能需要以root用户身份执行程序。下面的代码适合我(打印父堆栈,但不确定这是否非常有用):
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用std::cout;
静态void dumpStack(){
char-buf[64];
pid_t结果;
pid_t pid=getpid();
sprintf(buf,“/proc/%d/stack”,pid);
//您的意思是在父进程崩溃的情况下吗?该函数将如何调用?请提供一个完整的、可以按原样复制和编译的函数来演示问题,而不仅仅是一个单独的函数。您是从信号处理程序调用该函数吗?请注意,vWork
不是信号安全的,而且exec也不是lp
。您可能会使用Linux特定的函数,但它也没有列为信号安全函数(但在其他地方的信号示例中使用过,如)。请避免使用,因为我从未使用过它
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
#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;
}