- 论坛徽章:
- 0
|
int main(void){
pid_t pid1,pid2;
printf("The parent's pid is: %d.\n",getpid());
//create new process pid1
pid1 = fork();
//if son process pid1,sleep(10)
if ( pid1 == 0 ) {
for(int i=2; i>;0; i--) {sleep(5);}
printf("pid1 exit\n" ;
exit(0);
}
//if father process,create new process pid2
else if ( pid1 >; 0 ){
printf("pid1 is: %d\n",pid1);
pid2 = fork();
//if son precess pid2,sleep(30)
if ( pid2 == 0 ) {
for(int i=2; i>;0; i--) {sleep(15);}
printf("pid2 exit\n" ;
exit(0);
}
//if father process,
else if ( pid2 >; 0 ){
printf("pid2 is: %d\n",pid2);
waitpid( -1, NULL, 0 );
printf("exit!!!\n" ;
}
else printf("There was an error with forking pid2!\n" ;
}
else printf("There was an error with forking pid1!\n" ;
}
程序就是这样的,可为什么父进程不等待进程2执行完才退出,而是每当进程1结束,父进程就输出EXIT? |
|