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]);
}
}
Above are the code, which is simplified a little bit.
What I want to do is waiting for any child to finish and processing its data then repeating this untill all children are finished.
Can someone tell me how to pipe the data generated by the children to the parent?