I'm trying to figure out how many processes this program creates, including the initial parent process. The correct answer should be 9, but I don't understand why the answer is 9. How are these 9 processes created? Thanks in advance!
我试图弄清楚这个程序创建了多少个进程,包括初始的父进程。正确答案应该是9,但是我不明白为什么答案是9。这9个过程是如何创建的?提前谢谢!
#include <stdio.h>
#include <unistd.h>
…
int main()
{
pid_t john;
john = fork( );
if (john == 0) {
fork( ); fork( ); fork( );
}
/* Consume resources of another process */
/* This does NOT create a new process. */
Consume( ); Consume( );
return 0;
}
4 个解决方案
#1
6
john = fork( ); //fork a new process, we have 2 now.
if (john == 0) {// child process go in the if statement
fork( ); //child process fork to 2 processes
fork( ); //2 processes continue to fork,so we have 2 more.
fork( ); //4 processes continue to fork, so we have 4 more.
}
//up to here, the child process of the first fork is now 8 processes
//adding the first parent process, that is 9 in total.
#2
8
Remember that on the fork();fork();fork();
, both the parent and the child hit the next fork.
请记住,在fork()、fork()、fork()中,父类和子节点都将按下一个fork。
main
|
|\ john = fork()
| \
| \
| |\ fork()
| | \-----\
| |\ |\ fork()
| | \ | \
| | \ | \
| | \ | \
| |\ |\ |\ |\ fork()
| | | | | | | | |
1 2 3 4 5 6 7 8 9
#3
0
P1 forks, creating P2. P1 has john = <NOT ZERO>
, and P2 has john = 0
. Therefore P2 executes the if
. It forks, creating P3. Now, P2 and P3 are at the second fork. So they fork, creating P4 and P5. Now P2, P3, P4 and P5 all have one fork left. They fork, creating P6, P7, P8 and P9. Total of nine processes spawned.
P1叉子,创建P2。P1有john = < 0 >, P2有john = 0。因此P2执行if。叉,创建P3。P2和P3在第二个分叉处。它们分叉,生成P4和P5。P2, P3, P4和P5还有一个叉。他们用叉子,创建P6, P7, P8和P9。总共产生了9个进程。
fork
creates an exact copy of the program image of the calling process, except for the fact that it returns 0 to the child and a PID to the parent. And the child will be at the same place as the parent is after the fork.
fork创建调用进程的程序映像的精确副本,但它向子进程返回0,向父进程返回PID。而子节点将与父节点在fork之后的位置相同。
#4
0
friend, I am learning process too. And I got something about function fork(). You know, fork will create a new child process, and it will get a copy of data, content, stack, heap and so on from parent process, and child process will get a copy of PCB from parent process as well. Program's running is controled by Program Counter which contains the program's instructions. PCB contains the information of program counter. Because of the same PCB between child and parent, child process will run the left instructions of Program Counter, so in parent process the code before fork will not be run in the child. In the if statement, when the first fork run, the child process will only run the second and third fork. you can use this to draw a process diagram to help you resolve this question.Like this.Sorry, I don't have encough reputation to post a images for you. Hope my words can help you.
朋友,我也在学习过程。我得到了函数叉()fork将创建一个新的子进程,它将从父进程获得数据、内容、堆栈、堆等的副本,子进程也将从父进程获得PCB的副本。程序的运行由包含程序指令的程序计数器控制。PCB包含程序计数器的信息。由于子进程和父进程之间的PCB相同,子进程将运行程序计数器的左指令,所以在父进程中,fork之前的代码不会在子进程中运行。在if语句中,当第一个fork运行时,子进程将只运行第二个和第三个fork。您可以使用它来绘制流程图来帮助您解决这个问题。像这样。对不起,我没有encough的声誉,为您张贴图片。希望我的话能对你有所帮助。
#1
6
john = fork( ); //fork a new process, we have 2 now.
if (john == 0) {// child process go in the if statement
fork( ); //child process fork to 2 processes
fork( ); //2 processes continue to fork,so we have 2 more.
fork( ); //4 processes continue to fork, so we have 4 more.
}
//up to here, the child process of the first fork is now 8 processes
//adding the first parent process, that is 9 in total.
#2
8
Remember that on the fork();fork();fork();
, both the parent and the child hit the next fork.
请记住,在fork()、fork()、fork()中,父类和子节点都将按下一个fork。
main
|
|\ john = fork()
| \
| \
| |\ fork()
| | \-----\
| |\ |\ fork()
| | \ | \
| | \ | \
| | \ | \
| |\ |\ |\ |\ fork()
| | | | | | | | |
1 2 3 4 5 6 7 8 9
#3
0
P1 forks, creating P2. P1 has john = <NOT ZERO>
, and P2 has john = 0
. Therefore P2 executes the if
. It forks, creating P3. Now, P2 and P3 are at the second fork. So they fork, creating P4 and P5. Now P2, P3, P4 and P5 all have one fork left. They fork, creating P6, P7, P8 and P9. Total of nine processes spawned.
P1叉子,创建P2。P1有john = < 0 >, P2有john = 0。因此P2执行if。叉,创建P3。P2和P3在第二个分叉处。它们分叉,生成P4和P5。P2, P3, P4和P5还有一个叉。他们用叉子,创建P6, P7, P8和P9。总共产生了9个进程。
fork
creates an exact copy of the program image of the calling process, except for the fact that it returns 0 to the child and a PID to the parent. And the child will be at the same place as the parent is after the fork.
fork创建调用进程的程序映像的精确副本,但它向子进程返回0,向父进程返回PID。而子节点将与父节点在fork之后的位置相同。
#4
0
friend, I am learning process too. And I got something about function fork(). You know, fork will create a new child process, and it will get a copy of data, content, stack, heap and so on from parent process, and child process will get a copy of PCB from parent process as well. Program's running is controled by Program Counter which contains the program's instructions. PCB contains the information of program counter. Because of the same PCB between child and parent, child process will run the left instructions of Program Counter, so in parent process the code before fork will not be run in the child. In the if statement, when the first fork run, the child process will only run the second and third fork. you can use this to draw a process diagram to help you resolve this question.Like this.Sorry, I don't have encough reputation to post a images for you. Hope my words can help you.
朋友,我也在学习过程。我得到了函数叉()fork将创建一个新的子进程,它将从父进程获得数据、内容、堆栈、堆等的副本,子进程也将从父进程获得PCB的副本。程序的运行由包含程序指令的程序计数器控制。PCB包含程序计数器的信息。由于子进程和父进程之间的PCB相同,子进程将运行程序计数器的左指令,所以在父进程中,fork之前的代码不会在子进程中运行。在if语句中,当第一个fork运行时,子进程将只运行第二个和第三个fork。您可以使用它来绘制流程图来帮助您解决这个问题。像这样。对不起,我没有encough的声誉,为您张贴图片。希望我的话能对你有所帮助。