C 为什么主进程会创建许多没有循环的子进程?
我使用EclipseLunaIDE在Debian发行版下用C语言编程 我的程序的主进程应该只执行一次,因此只创建3个子进程,然后在所有子进程结束后结束 我看不出我的代码有什么问题,但我的程序创建了3个以上的孩子,他们的父亲不同 有人能告诉我如何获得预期的产出吗 主函数似乎执行了多次,但没有循环 这是我的密码:C 为什么主进程会创建许多没有循环的子进程?,c,multithreading,concurrency,fork,C,Multithreading,Concurrency,Fork,我使用EclipseLunaIDE在Debian发行版下用C语言编程 我的程序的主进程应该只执行一次,因此只创建3个子进程,然后在所有子进程结束后结束 我看不出我的代码有什么问题,但我的程序创建了3个以上的孩子,他们的父亲不同 有人能告诉我如何获得预期的产出吗 主函数似乎执行了多次,但没有循环 这是我的密码: #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <s
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(){
pid_t IMU1_PID, IMU2_PID, IMU3_PID;
IMU1_PID=fork();
IMU2_PID=fork();
IMU3_PID=fork();
if (IMU1_PID<0 || IMU2_PID<0 || IMU3_PID<0) { perror("fork"); exit(errno);}
if(IMU1_PID==0){
printf("child1's PID: %d - father's PID: %d\n",getpid(),getppid());
sleep(2);
exit(0);
}
if(IMU2_PID==0){
printf("child2's PID: %d - father's PID: %d\n",getpid(),getppid());
sleep(2);
exit(0);
}
if(IMU3_PID==0){
printf("child3's PID: %d - father's PID: %d\n",getpid(),getppid());
sleep(2);
exit(0);
}
printf("PARENT %d waiting until all CHILDS end\n",getpid());
while(wait(NULL)!=-1);
printf("PARENT %d after all CHILDS ended\n",getpid());
return 0;
}
这是我所期望的:
PARENT 4282 waiting until all CHILDS end
child3's PID: 4285 - father's PID: 4282
child2's PID: 4284 - father's PID: 4282
child1's PID: 4288 - father's PID: 4282
PARENT 4282 after all CHILDS ended
正如
fork()
手册页中明确解释的那样,函数1)如果失败,返回-1;函数2)如果成功,返回两个不同的进程(每个进程运行原始代码)。在父进程中,返回的值是子进程的pid。在子进程中,返回值为0。发布的代码没有检查返回值,因此父级和子级都在执行对fork()
的任何以下调用
建议检查返回的值,并让代码正确运行
例如:
pid1 = fork();
if( pid1 <0 ) {handle error and exit}
if( pid1 ==0 ) { processing for child1 and exit}
// if got here then parent
pid2 = fork();
if( pid2 <0 ) { handle error and exit }
if( pid2 == 0) { processing for child2 and exit }
// if got here then parent
etc.
pid1=fork();
如果(pid1使用Erik Eidt的话“当您fork
,根据定义,fork()
)的剩余函数(在本例中为main
)在原始进程和新创建的进程中执行。”
正如Charles所写,我以前的代码在调用第二个或第三个调用fork()
之前没有检查pid!初始进程调用第一个fork()
,然后父进程和第一个子进程都调用第二个fork()
,然后原始进程和两个子进程中的每一个都将调用第三个fork()
因此,以下是正确的代码:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(){
pid_t IMU1_PID, IMU2_PID, IMU3_PID;
IMU1_PID=fork();
if (IMU1_PID<0) { perror("fork"); exit(errno);}
if(IMU1_PID==0){
printf("child1's PID: %d - father's PID: %d\n",getpid(),getppid());
sleep(2);
exit(0);
}
IMU2_PID=fork();
if (IMU2_PID<0) { perror("fork"); exit(errno);}
if(IMU2_PID==0){
printf("child2's PID: %d - father's PID: %d\n",getpid(),getppid());
sleep(2);
exit(0);
}
IMU3_PID=fork();
if (IMU3_PID<0) { perror("fork"); exit(errno);}
if(IMU3_PID==0){
printf("child3's PID: %d - father's PID: %d\n",getpid(),getppid());
sleep(2);
exit(0);
}
printf("PARENT %d waiting until all CHILDS end\n",getpid());
while(wait(NULL)!=-1);
printf("PARENT %d after all CHILDS ended\n",getpid());
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main(){
pid_t IMU1_pid,IMU2_pid,IMU3_pid;
IMU1_PID=fork();
如果(IMU1_pid)您为什么认为“我的程序的主进程应该只执行一次…”当您fork时,根据定义,fork()
(在本例中,main
)在原始进程和新创建的进程中执行。Tks!起初我不理解您,但现在我理解了。抱歉,我看不出我们的逻辑之间有任何区别。您编写的内容与我编写的内容完全相同。if(pidx==0)中的所有内容pidx的主人是一个普通人吗?所有的一切都是父亲的吗routine@CleberMarques,在调用fork()的第二个或第三个调用之前,代码不会检查pid!初始进程调用第一个fork(),父进程和第一个子进程都调用第二个fork(),原始进程,三个子进程中的每一个子进程都将调用第三个fork()。@CharlesE.Grant,Tks,现在我明白了。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(){
pid_t IMU1_PID, IMU2_PID, IMU3_PID;
IMU1_PID=fork();
if (IMU1_PID<0) { perror("fork"); exit(errno);}
if(IMU1_PID==0){
printf("child1's PID: %d - father's PID: %d\n",getpid(),getppid());
sleep(2);
exit(0);
}
IMU2_PID=fork();
if (IMU2_PID<0) { perror("fork"); exit(errno);}
if(IMU2_PID==0){
printf("child2's PID: %d - father's PID: %d\n",getpid(),getppid());
sleep(2);
exit(0);
}
IMU3_PID=fork();
if (IMU3_PID<0) { perror("fork"); exit(errno);}
if(IMU3_PID==0){
printf("child3's PID: %d - father's PID: %d\n",getpid(),getppid());
sleep(2);
exit(0);
}
printf("PARENT %d waiting until all CHILDS end\n",getpid());
while(wait(NULL)!=-1);
printf("PARENT %d after all CHILDS ended\n",getpid());
return 0;
}