首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >父进程的进程id - Linux

父进程的进程id - Linux
EN

Stack Overflow用户
提问于 2014-01-20 09:33:19
回答 1查看 2.2K关注 0票数 0

我在一个链中有三个进程: P1 -> P2 -> P3。我希望能够从子进程(P3)中打印出这三个进程的id。

那么,我的问题是如何使用getppid()之类的方法获得孙子(P3)的祖父母(P1)的pid?

或者我必须将每一个的pid存储在它们自己的变量中,以供以后使用(不可取)?

谢谢你能给我的任何帮助。另外,只是因为,这是我目前为止的代码:

代码语言:javascript
复制
//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 );
}

因此,我最终使用堆栈来存储信息。如上面的代码所示。

EN

回答 1

Stack Overflow用户

发布于 2014-01-20 13:46:51

在子case 0:中,您需要打印pid和ppid,请参阅此代码。

代码语言:javascript
复制
  //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:

代码语言:javascript
复制
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 17 0 0 0 221 0 0 0

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21224943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档