我正在尝试从子进程中获取其“兄弟”的退出状态,
我试过这样做:
int main(void){
int i,j,status,p1;
pid_t pids[2],pid;
for (i = 0; i < 2; i++){
pids[i]=fork();
/*child 1*/
if(pids[i]==0 && i==0)exit(5);
/*child 2*/
else if(pids[i]==0 && i==1){
waitpid(pids[0],&p1,0);
exit(WEXITSTATUS(p1));
}
}
for(j=0;j<2;j++ ){
pid = wait(&status);
if(pid==pids[0]){
printf("child process 0 exit with status %d\n",WEXITSTATUS(status));
}
else if(pid==pids[1]){
printf("child process 1 exit with status %d\n",WEXITSTATUS(status));
}
}
return 0;
}
但我得到的输出是:
child process 0 exit with status 5
child process 1 exit with status 0
有没有办法做到这一点?