【发布时间】:2020-09-25 20:02:51
【问题描述】:
这就是我现在的位置
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main(){
int n;
int i;
int x = 1;
printf("\nenter number of forks:\n");
scanf ("%d",&n);
printf("\nnow forking %d times....\n\n", n);
for (i = 1;i <= n; i++){
int pid = fork();
if (pid < 0){
return -1;
}
if (pid != 0){
printf("\nI am the parent (
ppid = %d pid = %d)\n",getppid(),getpid()
);
printf(" x = %d\n",x);
x++;
wait();
}else{
printf("\nI am the child (
ppid = %d, pid = %d)\n x =
%d\n-------------------------------\n",
getppid(),getpid(),x
);
}
}
}
这是我传入 4 时的输出:
enter number of forks:
4
now forking 4 times....
I am the parent (ppid = 26178 pid = 39785)
x = 1
I am the child (ppid = 39785, pid = 39786)
x = 1
-------------------------------
I am the parent (ppid = 39785 pid = 39786)
x = 1
I am the child (ppid = 39786, pid = 39787)
x = 1
-------------------------------
I am the parent (ppid = 39786 pid = 39787)
x = 1
I am the child (ppid = 39787, pid = 39788)
x = 1
-------------------------------
I am the parent (ppid = 39787 pid = 39788)
x = 1
I am the child (ppid = 39788, pid = 39789)
x = 1
-------------------------------
I am the parent (ppid = 39786 pid = 39787)
x = 2
I am the child (ppid = 39787, pid = 39790)
x = 2
-------------------------------
I am the parent (ppid = 39785 pid = 39786)
x = 2
I am the child (ppid = 39786, pid = 39791)
x = 2
-------------------------------
I am the parent (ppid = 39786 pid = 39791)
x = 2
I am the child (ppid = 39791, pid = 39792)
x = 2
-------------------------------
I am the parent (ppid = 39785 pid = 39786)
x = 3
I am the child (ppid = 39786, pid = 39793)
x = 3
-------------------------------
I am the parent (ppid = 26178 pid = 39785)
x = 2
I am the child (ppid = 39785, pid = 39794)
x = 2
-------------------------------
I am the parent (ppid = 39785 pid = 39794)
x = 2
I am the child (ppid = 39794, pid = 39795)
x = 2
-------------------------------
I am the parent (ppid = 39794 pid = 39795)
x = 2
I am the child (ppid = 39795, pid = 39796)
x = 2
-------------------------------
I am the parent (ppid = 39785 pid = 39794)
x = 3
I am the child (ppid = 39794, pid = 39797)
x = 3
-------------------------------
I am the parent (ppid = 26178 pid = 39785)
x = 3
I am the child (ppid = 39785, pid = 39798)
x = 3
-------------------------------
I am the parent (ppid = 39785 pid = 39798)
x = 3
I am the child (ppid = 39798, pid = 39799)
x = 3
-------------------------------
I am the parent (ppid = 26178 pid = 39785)
x = 4
I am the child (ppid = 39785, pid = 39800)
x = 4
-------------------------------
我注意到的第一件事是,对于代码运行的每个实例,“子”的 PPID 是“父”的 PID,这很好。
但是当我手工绘制图表时: diagram of my output (I'm new so I can't post photos)
为了分配,它应该是一个平衡的树,这样一个级别的想法是有意义的。我希望将每个进程打印为图表中的一个节点,例如我使用 graphviz 绘制的图表,并且每个节点都应该包括它的 PID 和它的级别。
下面是 Geeks for Geeks 的一个例子,展示了我所说的级别:
L1 // There will be 1 child process
/ \ // created by line 1.
L2 L2 // There will be 2 child processes
/ \ / \ // created by line 2
L3 L3 L3 L3 // There will be 4 child processes
// created by line 3
我想我过度编码了。我应该如何更改我的代码以循环分叉并获得树状结构?我正在尝试使用变量 x 来表示级别,但我不确定它是否有效,因为输出的结构对我来说是非常出乎意料的。
【问题讨论】:
-
你画的好像是一棵树
-
是的,但它缺乏像图中那样定义级别所需的那种组织。它应该看起来更像示例,但它无处不在
-
为什么代码是双倍行距的?
-
你的输出会因为缺少
fflush而变得混乱。当您调用 printf 然后 fork 而不刷新时,您应该期望数据被写入两次。如果您多次 fork 而不刷新输出流,您将看到重复的相同消息。尝试重定向到一个文件,看看会发生什么。 (当您重定向时,您可以期望 stdout 被块缓冲而不是行缓冲。) -
@plants 也许我对“根本不是一棵树”的解释有点过于字面意思了:D