heres the code i have, the number of child is base on the user input, here i just put 5, my intention is to create multiple child to process individual data and then pipe it back to parent where it will store in an array, and print it out after collecting all data
Code:
pipe(fd);
pid=fork();
for(i=1; i<5; i++){
if(pid<0){
exit(-1);
}
else if (pid==0){
close(fd[0]);
//process data and write() to parent
exit(1);
}
else {
wait(NULL);
close(fd[1]);
//read() from child then store in integer array[i]
pipe(fd);
pid=fork();
}
}
// this is outside the for loop
for(a=1; a<5;a++){
printf("%d", array[a]);
}
everything works except that the print for-loop executes twice and give me 2 sets of inputs, i tried to put exit() and wait() in parent but no luck, anyone know what my problem is?
thank you in advance