【问题标题】:Process id of the parent's parent - Linux父进程的父进程 ID - Linux
【发布时间】:2014-01-20 01:33:19
【问题描述】:

我在一个链中有三个进程: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 starting\n";
  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 );
}

所以,我最终使用堆栈来存储信息。现在在上面的代码中可以看到。

【问题讨论】:

  • 可以遍历进程树的 Linux 实用程序基本上从/proc 读取(如pstree)。没有 POSIXy 类型的 API 来获取父级的父级。
  • 你应该在每个fork之前cout &lt;&lt; fflush
  • 整洁,但不幸的是输出“1”输出的进程最终不是最小的子进程。由于这是一项要求,因此我无法选择您的要求作为最佳答案。谢谢。
  • @Joe 所以,据我了解,我需要对系统进程树进行一些解析才能找到“P3”父级的父级?

标签: c++ linux process fork


【解决方案1】:

您需要在子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 starting\n";
  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 child\n";
      n = 5;
      exit_code = 9;
      break;
    default:
      message = "This is the parent\n";
      n = 3;
      exit_code = 0;
      break;
    }
  default:
    message = "This is the grand parent\n";
    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 获取 ppid

cat /proc/pid/stat 例如对于 PID #20467:

cat /proc/20467/stat

20467(ABC)20137 20467 20125 34818 20430 5196 20 0 0 0 0 0 0 0 0 0 0 2 2210 0 0

上面(第 4 个字段)中的 20137 是 PPID。

【讨论】:

  • 好主意,但不幸的是,输出“Child1 finished”和“parent PID”输出的进程最终不是最小的子进程。由于这是一项要求,因此我无法选择您的要求作为最佳答案。感谢您的知识。
  • 是的,必须做一些解析。谢谢。
猜你喜欢
  • 2013-08-11
  • 1970-01-01
  • 2011-07-16
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多