【发布时间】:2018-12-11 19:05:27
【问题描述】:
我需要直接从代码中打印进程树的帮助。感谢 stackoverflow 社区,我编写了一个程序,它使用 fork() 函数创建了多个进程,现在我需要使用 execlp() 函数在屏幕上打印一个进程树。
int main()
{
int t = 5;
int mainPID = getpid();
cout << "Main process: " << mainPID << endl << endl;
int pid;
if ((pid = fork()) == -1) return -1;
if (pid == 0)
{
cout << "source for child process ";
}
else{
cout << "source for parent process ";
}
sleep(t);
return 0;
}
当我在另一个终端类型的实例上运行程序时
pstree /mainPID/
我得到从 mainPID 开始打印的树。我需要从代码中打印出那棵树,但是当输入代码时
execlp("pstree", "pstree", "-c", "-p", (int *)NULL);
我从所有系统中得到打印的树
execlp("pstree", "pstree", mainPID, "-c", "-p", (int *)NULL);
什么都不打印。
【问题讨论】: