-2
我有痛苦大量试图弄清楚这个问题了。所以,我想要做的是创建3个来自父进程的子进程。我已经想通了,但问题是我的程序退到第三个孩子离开孤儿时(我想)。这是我的第一代的过程代码:fork函数涉及for循环
int i = 0;
int corpse;
int status;
pid_t child;
about("Dad");
printf("Now .. Forking !!\n");
about("Dad");
for(i=0; i<3; i++){
child = fork();
if (child == 0){
about ("son");
break;
}else if (child < 0){
perror ("Unable to fork!");
break;
}
}
void about(char * msg)
{
pid_t me;
pid_t old;
me = getpid();
old = getppid();
printf("[%s] Hi, my pid is %d and I come from %d.\n", msg, me, old);
}
while ((corpse = wait(&status)) > 0){
printf("Child process PID=%d terminates\n", corpse);
}
现在我的输出如下:
[Dad] Hi, my pid is 2940 and I come from 2883.
Now .. Forking !!
[Dad] Hi, my pid is 2940 and I come from 2883.
[son] Hi, my pid is 2941 and I come from 2940.
[son] Hi, my pid is 2942 and I come from 2940.
Child process PID=2941 terminates
Child process PID=2942 terminates
[son] Hi, my pid is 2943 and I come from 2940.
Child process PID=2943 terminates
显示你的*实际代码*。用“continue”语句显示代码但没有循环是没有意义的。 – 2014-09-27 13:41:09
你没有第三个孩子。你只分了一次。 – 2014-09-27 13:42:34
我添加了for循环,对不起,我复制了错误的东西。 – 2014-09-27 13:52:18