C语言 fork() 子进程和父进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7265718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
fork() child and parent processes
提问by raphnguyen
I am trying to create a program that uses fork() to create a new process. The sample output should look like so:
我正在尝试创建一个使用 fork() 创建新进程的程序。示例输出应如下所示:
This is the child process. My pid is 733 and my parent's id is 772.
This is the parent process. My pid is 772 and my child's id is 773.
这是子进程。我的 pid 是 733,我父母的 id 是 772。
这是父进程。我的pid是772,我孩子的id是773。
This is how I coded my program:
这是我编写程序的方式:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), fork());
return 0;
}
This results in the output:
这导致输出:
This is the child process. My pid is 22163 and my parent's id is 0.
This is the child process. My pid is 22162 and my parent's id is 22163.
这是子进程。我的pid是22163,我父母的id是0。
这是子进程。我的PID是22162,我的父母的ID是22163。
Why is it printing the statement twice and how can I get it to properly show the parent's id after the child id displays in the first sentence?
为什么要打印两次语句,如何在第一句中显示子 ID 后正确显示父 ID?
EDIT:
编辑:
#include <stdio.h>
#include <stdlib.h>
int main() {
int pid = fork();
if (pid == 0) {
printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), getppid());
}
else {
printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), pid);
}
return 0;
}
回答by Tom
Start by reading the fork man pageas well as the getppid/ getpidman pages.
首先阅读fork 手册页以及getppid/ getpid手册页。
From fork's
从叉子
On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and errno will be set appropriately.
成功时,在父执行线程中返回子进程的PID,在子执行线程中返回0。失败时,将在父进程的上下文中返回 -1,不会创建子进程,并且将适当设置 errno。
So this should be something down the lines of
所以这应该是
if ((pid=fork())==0){
printf("yada yada %u and yada yada %u",getpid(),getppid());
}
else{ /* avoids error checking*/
printf("Dont yada yada me, im your parent with pid %u ", getpid());
}
As to your question:
至于你的问题:
This is the child process. My pid is 22163 and my parent's id is 0.
This is the child process. My pid is 22162 and my parent's id is 22163.
这是子进程。我的 pid 是 22163,我父母的 id 是 0。
这是子进程。我的 pid 是 22162,我父母的 id 是 22163。
fork()executes before the printf. So when its done, you have two processes with the same instructions to execute. Therefore, printf will execute twice. The call to fork()will return 0to the child process, and the pidof the child process to the parent process.
fork()在printf. 所以当它完成时,你有两个具有相同指令的进程要执行。因此, printf 将执行两次。调用fork()将返回0到子进程,子进程的调用将返回到pid父进程。
You get two running processes, each one will execute this instructionstatement:
你得到两个正在运行的进程,每个进程都会执行这条指令语句:
printf ("... My pid is %d and my parent's id is %d",getpid(),0);
and
和
printf ("... My pid is %d and my parent's id is %d",getpid(),22163);
~
~
To wrap it up, the above line is the child, specifying its pid. The second line is the parent process, specifying its id (22162) and its child's (22163).
总结一下,上面这行是孩子,指定了它的pid. 第二行是父进程,指定它的 id (22162) 和它的子进程 (22163)。
回答by Hogan
It is printing the statement twice because it is printing it for both the parent and the child. The parent has a parent id of 0
它打印语句两次,因为它同时为父级和子级打印。父级的父级 ID 为 0
Try something like this:
尝试这样的事情:
pid_t pid;
pid = fork();
if (pid == 0)
printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(),getppid());
else
printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), getppid() );
回答by F128247 Muhammad Awais
We control fork() process call by if, else statement. See my code below:
我们通过 if, else 语句控制 fork() 进程调用。请参阅下面的代码:
int main()
{
int forkresult, parent_ID;
forkresult=fork();
if(forkresult !=0 )
{
printf(" I am the parent my ID is = %d" , getpid());
printf(" and my child ID is = %d\n" , forkresult);
}
parent_ID = getpid();
if(forkresult ==0)
printf(" I am the child ID is = %d",getpid());
else
printf(" and my parent ID is = %d", parent_ID);
}
回答by Nitish Jain
This is the correct way for getting the correct output.... However, childs parent id maybe sometimes printed as 1 because parent process gets terminated and the root process with pid = 1 controls this orphan process.
这是获得正确输出的正确方法.... 然而,孩子的父 id 有时可能会打印为 1,因为父进程被终止并且 pid = 1 的根进程控制这个孤立进程。
pid_t pid;
pid = fork();
if (pid == 0)
printf("This is the child process. My pid is %d and my parent's id
is %d.\n", getpid(), getppid());
else
printf("This is the parent process. My pid is %d and my parent's
id is %d.\n", getpid(), pid);
回答by Daniel Pereira
It is printing twice because you are calling printf twice, once in the execution of your program and once in the fork. Try taking your fork() out of the printf call.
它打印两次,因为您调用 printf 两次,一次是在程序执行中,一次是在 fork 中。尝试从 printf 调用中取出 fork()。

