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]);
}
}
上面是代碼,它被簡化了一點。
我想要做的是等待任何孩子完成並處理其數據,然後重複此操作,直到所有的孩子都完成了。
有人可以告訴我如何將孩子生成的數據傳給父母嗎?
關於從多子進程讀取數據的管道問題
是的,我在帖子後找到了它。無論如何thnx .. –