父级的父级的进程 ID - Linux
Process id of the parent's parent - Linux
我在链中有三个过程:p1-> p2-> p3。我希望能够从孩子内部(P3)过程中打印出所有三个ID。
所以,我的问题是我如何获得带有getppid()()之类的大孩子(P3)的大父母(P1)的pid?
或我将不得不求助于将每个PID存储在自己的变量中以供以后使用(不可取)?
感谢您提供的任何帮助。另外,仅仅是因为,这是我到目前为止的代码:
//test_wait.cpp
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
using namespace std;
int main()
{
pid_t pid; //process id
const char *message;
int n;
int exit_code;
std::stack<int> proc_stack;
cout << "Fork program startingn";
proc_stack.push(getpid());
pid = fork();
switch ( pid ) {
case -1:
cout << "Fork failure!n";
return 1;
case 0:
pid = fork();
switch ( pid ) {
case -1:
cout << "Fork Failure!n";
return 1;
case 0:
cout << "Grandchild PID: " << getpid() << endl;
cout << "Parent PID: " << getppid() << endl;
cout << "Grand Parent PID: " << proc_stack.top() << endl;
exit_code = 9;
break;
default:
exit_code = 0;
break;
}
default:
exit_code = 0;
break;
}
//waiting for child to finish
if ( pid != 0 ) { //parent
int stat_val;
pid_t child_pid;
child_pid = wait ( &stat_val ); //wait for child
}
exit ( exit_code );
}
所以,我最终使用堆栈存储信息。如上面的代码中所示。
您需要在儿童case 0:
中打印PID和PPID,请参阅此代码。
//test_wait.cpp
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
pid_t pid; //process id
const char *message;
int n;
int exit_code;
cout << "fork program startingn";
pid = fork();
switch ( pid ) {
case -1:
cout << "Fork failure!n";
return 1;
case 0:
cout << "Child1 finished: PID = " << getpid() << endl;
cout << "Parent PID = " << getppid() << endl;
pid = fork();
switch ( pid ) {
case -1:
cout << "Fork Failure!n";
return 1;
case 0:
cout << "Child2 finished: PID = " << getpid() << endl;
cout << "Parent PID = " << getppid() << endl;
message = "This is the childn";
n = 5;
exit_code = 9;
break;
default:
message = "This is the parentn";
n = 3;
exit_code = 0;
break;
}
default:
message = "This is the grand parentn";
n = 3;
exit_code = 0;
break;
}
for ( int i = 0; i < n; ++i ) {
cout << message;
sleep ( 1 );
}
//waiting for child to finish
if ( pid != 0 ) { //parent
int stat_val;
pid_t child_pid;
child_pid = wait ( &stat_val ); //wait for child
}
exit ( exit_code );
}
您也可以从/proc
获得PPIDcat/proc/pid/stat例如对于PID#20467:
cat /proc/20467/stat
20467(ABC)20137 20467 20125 34818 20467 4202496 1930 5196 5196 22 0 113 162 32 25 20 0 1 0 1 1701252 1701252 14548992 1606 1606 429494967295 134889479 2151083916 0 0 17 0 0 0 0 0 221 0 0
上述20137年(第四场)是PPID。
相关文章:
- 有没有一种方法可以捕获进程中的堆栈溢出?C++Linux
- 如何制作启动 Python 脚本的 linux 后台进程(在 c 中)
- 如何在 Linux 下使用 c++ 知道文件是否被其他进程使用?
- linux mint进程上的代码块已终止,状态为-1
- 在 Linux 平台上以 C/C++ 打印进程的所有线程堆栈跟踪
- 使用C 获取Linux进程列表和状态
- Fedora Linux 中的进程
- 当多个进程尝试同时替换同一文件时,Linux 如何处理这种情况?
- 如何在 Linux 上调试本机代码时自动附加到多个子进程
- 在 Linux 中从单独的单线程进程生成多线程进程时出现问题
- 如何在 Linux 上恢复所有当前进程
- 在 Linux 下的 Qt,如何检测一个进程已经在运行
- Linux进程等待I/O,其状态为S
- 在 Linux 中退出时如何注册进程的回调
- 如何识别一个进程是 java 还是 Linux 中的 c 或 c++ 进程
- Linux 进程加载程序在 C++
- Linux 进程调度延迟了很长时间
- 用C++创建Linux进程
- 杀死一个Linux进程,停止它在Oracle数据库中的查询工作吗?
- OpenCV:在Linux进程之间共享网络摄像头是可能的吗?