小编Beg*_*ner的帖子

输出中出现意外的父进程 ID

我的代码正在分叉一个进程并打印每个进程的 PID 和 PPID。我期待孩子的 PPID 与父母的 PID 相同,但事实并非如此。

我正在使用 Ubuntu 14.04。

#include <stdio.h>
#include <sys/wait.h>

int main(){
    int pid;
    pid = fork();
    if(pid==0){
        printf("\nI am the child and my parent id is %d and my id %d\n", getppid(), getpid());
    }
    else
        printf("\nI am the parent and my pid is %d and my parent id is %d\n", getpid(), getppid());

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

I am the parent and my pid is 29229 and my parent id is 27087
I am the …
Run Code Online (Sandbox Code Playgroud)

linux ubuntu system-calls fork

2
推荐指数
1
解决办法
543
查看次数

标签 统计

fork ×1

linux ×1

system-calls ×1

ubuntu ×1