pid_t kids[argc];
int childCount = argc - 1;
int fd[2];
/* create the pipe*/
if (pipe(fd) == -1) {
fprintf(stderr ,"Pipe failed");
return 1;
for(i=0; i < childCount; i++) {
kids[i] = fork();
if(kids[i] < 0){
//fork fail
}
else if(kids[i] == 0) {
/* child process */
sum = max + min;//sum and dif are results from each child process
dif = max - min;
/* close the unused end of pipe, in this case, the read end */
close(fd[READ_END]);
/* write to the pipe */
write(fd[WRITE_END], &sum, sizeof(sum));
write(fd[WRITE_END], &dif, sizeof(dif));
/* close write end */
close(fd[WRITE_END]);
exit(0);
}
else {
waitpid(kids[i], &status, 0);
close(fd[WRITE_END]);
read(fd[READ_END], &sum, sizeof(float));
read(fd[READ_END], &dif, sizeof(float));
close(fd[READ_END]);
}
}
上記のコードは、少し単純化されています。
私がやりたいことは、子が終了するのを待ってそのデータを処理し、すべての子が終了するまでこれを繰り返すことです。
子によって生成されたデータを親にパイプする方法を教えてもらえますか?