내가 4 개 개의 병렬 처리사용 대기는 모든 프로세스가 종료하지만 이상한 출력
I 첫번째 포크 onec와 함께 몇 가지 일을하려는의 대기 시간, 아동 및 부모 포크 다시 4 개 과정을 얻을 수 있습니다.
내가 원하는 모든 4 개 과정이 끝난 후 뭔가를 할 것입니다, 그래서 waitpid(-1, &status, 0);
을 사용하고 나의 이상적인 출력은
in
numbers
out
하지만 실제 출력이 가끔있을 수 있다는 것이다
in
numbers
out
one number
저는 알아낼 수 없습니다.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <error.h>
int main()
{
pid_t cpid, w;
int status;
printf("%s\n", "in");
cpid = fork();
if (cpid == 0)
{
cpid = fork();
if (cpid == 0)
{
printf("%s\n", "1");
exit(EXIT_SUCCESS);
}
else{
printf("%s\n", "2");
exit(EXIT_SUCCESS);
}
}
else{
cpid = fork();
if (cpid == 0)
{
printf("%s\n", "3");
exit(EXIT_SUCCESS);
}
else{
printf("%s\n", "4");
//exit(EXIT_SUCCESS);
}
}
waitpid(-1, &status, 0);
printf("%s\n", "out");
//do something
exit(EXIT_SUCCESS);
return 0 ;
}