简体   繁体   English

父级的父级的进程ID-Linux

[英]Process id of the parent's parent - Linux

I have three processes in a chain: P1 -> P2 -> P3. 我有一个链中的三个过程:P1-> P2-> P3。 I would like to be able to print out the id of all three from within the child(P3) process. 我希望能够从child(P3)进程中打印出所有三个的ID。

So, my question is how do I get the pid of a grand parent(P1) of the grand child(P3) with getppid() and such? 因此,我的问题是如何使用getppid()等获取子级孩子(P3)的父级父亲(P1)的pid?

Or am i going to have to resort to storing the pid of each in their own variables for later use(not desireable)? 还是我不得不求助于将每个变量的pid存储在自己的变量中供以后使用(不需要)?

Thank you for any assistance you can give me. 谢谢您能给我的任何帮助。 Also, just because, here is my code thus far: 另外,正因为如此,到目前为止,这是我的代码:

//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 );
}

So, I ended up using a stack to store the information. 因此,我最终使用堆栈来存储信息。 As seen in the above code now. 如上面的代码所示。

You need to print pid and ppid when in child case 0: refer this code. 在子case 0:时,您需要打印pid和ppid case 0:请参考此代码。

  //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 );
}

Also you can get ppid from /proc 你也可以从/ proc获取ppid

cat /proc/pid/stat eg for PID #20467: cat / proc / pid / stat,例如PID#20467:

cat /proc/20467/stat

20467 (abc) S 20137 20467 20125 34818 20467 4202496 1930 5196 22 0 113 162 32 25 20 0 1 0 1701252 14548992 1606 4294967295 4194304 6492552 2076847264 2076842300 694388884 0 0 0 134889479 2151083916 0 0 17 0 0 0 221 0 0 20467(abc)S 20137 20467 20125 34818 20467 4202496 1930 5196 22 0 113 162 32 25 20 0 1 0 1701252 14548992 1606 4294967295 4194304 6492552 2076847264 2076842300 694388884 0 0 0 134889479 2151083916 0 0 17 0 0 0 221 0 0

The 20137 in the above (4th field) is the PPID. 上面(第4字段)中的20137是PPID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM