我需要在打印过程树的帮助,正好从代码。感谢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);不打印任何内容。
发布于 2019-12-02 02:23:00
execlp将char *const argv[]作为其余参数...
所以尝试将"mainPID“转换为char[]
这在我的案例中起作用了。
发布于 2019-11-23 00:23:06
您应该将mainPID转换为字符串或字符*,然后尝试此操作。
https://stackoverflow.com/questions/53730773
复制相似问题