intersting are only the last 10 lines. I try to write to factor, then read, than write again (...) until 0 is given. This wont work, because read(p_stdout[READ], ....) always blocks the programm, waiting for some better waeather are someso. I tried to use fsync() between write and read, but it didnt help. Only fclose() solve the problem, but if p_stdin[WRITE] is closed, i can only iterate the loop one times.
Of course, we can help. It's just a matter how much you're willing to pay
More seriously, I think your problem is simply related to the fact that stdout is line buffered. If you switch to unbuffered mode in the child, this may perhaps solve your problem:
thx for your tips - unfortunately "setvbuf(stdout, NULL, _IONBF, 0)" does not solve the problem (I think its the same like a explizit fsync() call). Any other ideas?
yeah, my bad. I prototyped using a parent/child... But setvbuf() settings are not preserved across an exec()! Besides that, I am not sure that it has to do with your specific problem... I need to dig further. Perhaps you could tell us what this 'factor' is? a shell script?
so I have a good and a bad news. The good news: I found the reason for your problem. The stream stdout is normally "line-buffered", but become block-buffered (e.g. 8Kb) when stdout is not connected to a terminal (for instance, when stdout is redirected to a pipe or a file)... This can be simply seen if you invoke factor as follows
factor | more
123
456
(no output)
x
factor: `x' is not a valid positive integer
123: 3 41
456: 2 2 2 3 19
The 'x' causes a message on stderr stream, and stdout to be flushed.
There are solutions to this problem, but those are not necessarily straightforward. That's the bad news. Basically, you have to let the child process think that it is attached to a pseudo-terminal. On Linux, this can be achieved using forkpty(). More info on this technique can be found here: rmathew: Terminal Sickness
If you want to play with bidirectional pipes, I suggest you to have a binary that you control (e.g. a shell script, or a C-program from you) where you can flush the stdout stream.