FailedFace
FailedFace

Reputation: 17

Process id of the parent's parent - Linux

I have three processes in a chain: P1 -> P2 -> P3. I would like to be able to print out the id of all three from within the child(P3) process.

So, my question is how do I get the pid of a grand parent(P1) of the grand child(P3) with getppid() and such?

Or am i going to have to resort to storing the pid of each in their own variables for later use(not desireable)?

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.

Upvotes: 0

Views: 2715

Answers (1)

Rahul R Dhobi
Rahul R Dhobi

Reputation: 5826

You need to print pid and ppid when in child case 0: refer this code.

  //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

cat /proc/pid/stat e.g. for 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

The 20137 in the above (4th field) is the PPID.

Upvotes: 1

Related Questions