【发布时间】:2021-06-24 15:08:57
【问题描述】:
首先让我说这是我关于 stackoverflow 的第一个问题,而且我对编码也很陌生,所以我提前对任何缺点表示感谢。
我正在尝试找出子进程如何显示其父进程的父 pid(子进程的祖父 pid)
这是我的代码,我添加了需要更改的注释:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
int pid, status, status2;
pid = fork();
switch (pid)
{
case -1: // An error has occured during the fork process.
printf("Fork error.");
break;
case 0:
pid = fork();
switch (pid)
{
case -1:
printf("Fork error.");
break;
case 0:
printf("I am the child process C and my pid is %d\n", getpid());
printf("My parent P has pid %d\n", getppid());
printf("My Grandparent G has pid %d\n", //what to put here );
break;
default:
wait(&status);
printf("I am the parent process P and my pid is %d\n", getpid());
printf("My parent G has pid %d\n", getppid());
break;
}
break;
default:
wait(&status2);
printf("I am the Grandparent process G and my pid is %d\n", getpid());
break;
}
}
另外,一般提示将不胜感激
【问题讨论】:
-
关于
"%d"应该是pid_t pidWhat is the correct printf specifier for printing pid_t