fork() 子进程和父进程
我正在尝试创建一个使用 fork() 来创建新进程的程序。示例输出应如下所示:
这是子进程。我的 pid 是 733,我父母的 id 是 772。
这是父进程。我的 pid 是 772,我孩子的 id 是 773。
这就是我编写程序的方式:
#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;
}
这导致输出:
这是子进程。我的 pid 是 22163,我父母的 id 是 0。
这是子进程。我的 pid 是 22162,我父母的 id 是 22163。
为什么它打印了两次语句,如何让它在第一句中显示子 id 后正确显示父母的 id?
编辑:
#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;
}
I am trying to create a program that uses fork() to create a new process. The sample output should look like so:
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.
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.
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?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先阅读fork 手册页以及getppid / getpid 手册页。
来自叉子的
所以这应该是关于你的问题的内容
:
fork()
在printf
之前执行。因此,完成后,您将有两个进程执行相同的指令。因此,printf会执行两次。调用fork()
将向子进程返回0
,并向父进程返回子进程的pid
。您将获得两个正在运行的进程,每个进程都会执行此
instruction语句:和
~
总结一下,上面的行是子进程,指定其
pid
。第二行是父进程,指定其 id (22162) 及其子进程 (22163)。Start by reading the fork man page as well as the getppid / getpid man pages.
From fork's
So this should be something down the lines of
As to your question:
fork()
executes before theprintf
. So when its done, you have two processes with the same instructions to execute. Therefore, printf will execute twice. The call tofork()
will return0
to the child process, and thepid
of the child process to the parent process.You get two running processes, each one will execute this
instructionstatement:and
~
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).它打印该语句两次,因为它同时为父级和子级打印该语句。父级的父级 id 为 0
尝试如下操作:
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
Try something like this:
我们通过if、else语句控制fork()进程的调用。请参阅下面我的代码:
We control fork() process call by if, else statement. See my code below:
它打印两次,因为您调用 printf 两次,一次在程序执行中,一次在 fork 中。尝试将 fork() 从 printf 调用中取出。
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.
这是获得正确输出的正确方法...但是,子进程的父进程 id 有时可能会打印为 1,因为父进程被终止,并且 pid = 1 的根进程控制着这个孤儿进程。
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.