Basically, I have multiple children generated by a for loop with fork(). Lets assume 5 children are generated. And the parent will send mathematical equations to the children one by one through a pipe (Totally 20 equations). And the children will calculate the result of the equation an output into a file.
However, the while loop doesn't seem to end even if all the equations have been calculated.
In the child:
while(read(pipes[procid][0], buf, MAIN_BUF_LEN) > 0){
exp_readln(&exp, buf);//Change the string into Expression class
result = exp_cal(&exp);//Calculate the result
printf("Recieved: #%d %d %0.3f\n", procid, mypid, result);
fprintf(outfileptr, "#%d %d %0.3f\n", procid, mypid, result);
fflush(outfileptr);
}
printf("Exit loop\n");
close(pipes[procid][0]);
exit(0);
In the parent:
i = 0;
while (fd_readln(fileno(infileptr), buf, MAIN_BUF_LEN) > 0){
printf("Sent %d: %s\n", i, buf);
dprintf(pipes[i][1],"%s\n", buf);
i = i + 1;
if (i >= proc){
i = 0;
}
}
The expected result is that after a child received and processed all the equations, it will output the calculation result. And it did. However, it does not output the "Exit loop" statement, showing that the program stayed in the while loop even if there is no more data/equations coming through the pipe. So how should I fix this problem?