C에서 return 값인 wait
을 확인한 후 오류가 아니면 WIFEXITED
및 WIFEXITSTATUS
이라는 또 다른 검사가 있습니다. 왜 이렇게 중복되지 않습니까? 내가 이해하는 한, wait
은 오류가 발생하면 -1을 반환하고, 은자식이 정상적으로 종료되면 0이 아닌 값을 반환합니다. 그래서이 라인에 아무런 오류가 없다면 if (wait(&status) < 0)
무엇이 잘못되었을 것입니까? WIFEXITED
확인 하시겠습니까? 리눅스 시스템 호출에서 자식 프로세스를 죽이기 위해 기다린 후에 WIFEXITED를 확인해야하는 이유는 무엇입니까?
이
코드입니다 :#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#define CHILDREN_NUM 5
int main() {
int i, status, pid, p;
for(i = 0; ((pid = fork()) < 0) && i < CHILDREN_NUM;i++)
sleep(5);
if (pid == 0)
{
printf(" Child %d : successfully created!\n",i);
exit(0); /* Son normally exits here! */
}
p = CHILDREN_NUM;
/* The father waits for agents to return succesfully */
while (p >= 1)
{
if (wait(&status) < 0) {
perror("Error");
exit(1);
}
if (! (WIFEXITED(status) && (WEXITSTATUS(status) == 0))) /* kill all running agents */
{
fprintf(stderr,"Child failed. Killing all running children.\n");
//some code to kill children here
exit(1);
}
p--;
}
return(0);
}