【问题标题】:Printing pstree using execlp()使用 execlp() 打印 pstree
【发布时间】: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);

什么都不打印。

【问题讨论】:

    标签: c++ fork exec pstree


    【解决方案1】:

    execlp 将 char *const argv[] 作为其余参数 ...

    所以尝试将“mainPID”转换为char[]

    这在我的情况下有效。

    【讨论】:

      【解决方案2】:

      你应该将 mainPID 转换为字符串或字符 *,然后试试这个。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2021-11-02
      • 2013-07-05
      • 1970-01-01
      • 2019-09-18
      相关资源
      最近更新 更多