前叉功能流程图,有效和无效输出 [英] fork function process graph, valid and invalid outputs
问题描述
printf("L1 \n");
if (fork() != 0) {
printf("L2 \n");
if (fork() != 0) {
printf("L3 \n");
fork();
}
}
printf("End \n");
我很难理解此代码.有人可以解释/显示流程图吗?此外,还有一个有效和无效的序列.
I am having a hard time understanding this code. Could someone explain/ display what the process graph would look like? Also, a valid and invalid sequence.
我对流程图的想法是这样的:
My thought of the process graph was something like this:
L1将保存两个L2进程,然后每个L2进程将保存2个L3进程,每个L3进程将保存一个结束进程.这是正确的吗?
L1 will hold two L2 processes then each L2 processes will hold 2 L3 processes and each L3 processes will hold an end processes. Is this correct?
对我来说有效的顺序是L1,L2,L3,结束
A valid sequence for me was L1, L2, L3, End
一个无效的将是L2,L1,结束,这是因为L1必须在L2之前出现,L2依赖于L1.
An invalid one would be L2, L1, End this is because L1 must come before L2, L2 is dependent on L1.
推荐答案
好的,这是正确缩进并带有编号行的代码.
Ok, here's the code properly indented and with numbered lines.
1. printf("L1 \n");
2. if(fork() != 0) {
3. printf("L2 \n");
4. if(fork() != 0) {
5. printf("L3 \n");
6. fork();
7. }
8. }
9. printf("End \n");
让我们假设第一次运行时,父亲的PID是1000(无论多少,这都取决于操作系统,但是我们在做这个假设是为了使过程更清楚树).
Let's assume that when you run it for the first time the father's PID is 1000 (it doesn't matter what number is it, anyway it depends on OS, but we're making this assumption to make it clear on the process tree).
进程PID==1000
打印L1
.到目前为止,我们有:
Process PID==1000
prints L1
. So far we have:
进程树:
1000
输出:
L1
第2行:
进程PID==1000
派生,创建一个子进程(假设它是PID==1001
).根据man 2 fork
,只有进程PID==1000
进入if块,因为fork()
将为子进程返回0
.
Line2:
Process PID==1000
forks, creating a child process (assume it's PID==1001
). According to man 2 fork
only process PID==1000
is entering the if block, since fork()
will return 0
for the child process.
进程PID==1000
在第3行继续,它将打印L2
,然后继续到第5行,而进程PID==1001
在if
块之后跳到第9行,因此将打印End
.到目前为止,我们有:
Process PID==1000
continues at Line 3 where it will print L2
and then continue to Line 5, whereas process PID==1001
jumps to Line 9, after the if
block, so it will print End
. So far, we have:
进程树:
-------------- 1000
|
|
1001
输出:
L1
L2
End
第4行:
进程PID==1000
再次派生,创建子进程PID==1002
.
Line 4:
Process PID==1000
forks again, creating child process PID==1002
.
同样,进程PID==1002
跳到第9行,打印End
,因为fork()
为此返回0
,而父进程PID==1000
继续在第5行,打印L3
,然后继续到第6行.到目前为止,我们有:
Again, process PID==1002
jumps to Line 9, printing End
because fork()
returns 0
for it, whereas father process PID==1000
continues at Line 5, printing L3
, and after that it's going to Line 6. So far we have:
进程树:
------------ 1000
| |
| |
1001 1002
输出:
L1
L2
End
L3
End
第6行:
父进程PID==1000
再次分叉.创建子进程PID==1003
.之后,父进程和子进程都转到第9行,因此它们都打印End
.最后,我们有:
Line 6:
Father process PID==1000
forks again. creating child process PID==1003
. After that, both father and child processes go to Line 9, so they both print End
. So finally, we have:
进程树:
------------- 1000 --------------
| | |
| | |
1001 1002 1003
输出:
L1
L2
End
L3
End
End
End
要亲自查看,可以使用getpid(2)
更改当前代码中的printf()
,以准确查看每个进程将打印的内容.代码将如下所示:
To see that by yourself, you could change printf()
in your current code using getpid(2)
to see exactly what will be printed by each process. Code will be like this:
1. printf("(pid %d)\tL1\n", (int) getpid());
2. if (fork() != 0) {
3. printf("(pid %d)\tL2\n", (int) getpid());
4. if (fork() != 0) {
5. printf("(pid %d)\tL3\n", (int) getpid());
6. fork();
7. }
8. }
9. printf("(pid %d)\tEnd\n", (int) getpid());
这篇关于前叉功能流程图,有效和无效输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!