Pipes sometimes fail
I'm working on an application which basically does the following:
1. Create pipes for stdin/stdout/stderr
Then for the child:
1. Close unneeded side of the pipes, close 0, 1, 2, dup the pipes to stdin, stdout and stderr
2. Wait for a single-char message on stdin
3. Continue the program, sending data to stdout/stderr
Then for the parent:
1. Wait until we can read or write from one of the pipes (poll)
2. Do whatever we can do (all input is already available before this runs, so we close the input pipe on our end). If we can send output, first send a single character. (to stop waiting for a single char message in the child)
Now, most of the times it works fine. Sometimes, the output doesn't seem to work (that is, the child can't send on the stdout and stderr pipes). If I sleep for a little while in the child, however, it always works fine.
What could the problem be?
Thanks in advance.
It's hard to tell what you mean by "doesn't seem to work" without seeing some code.
Originally Posted by EVOEx
I presume you are using fprintf or something to write to the pipes, unless by stdout you just mean stdout, since I'm not sure why you would need a pipe connected to stdout unless stdout is itself is closed in the child...which I'm not sure that would be possible or make sense to do.
Anyway, the processes are asynchronous and maybe not surprising that a delay here and there may help things go smoothly. A satisfactory gap is probably quite short: if you set up your own delay function with nanosleep() you could experiment with that, probably a fifth or quarter second is plenty.
Thank you for your answer.
Yes, I am closing stdin, stdout and stderr. It's quite common practice. At least, I've seen it done in many programs. And I have to do it in this program I'm working on, since it pipes input/output to another program. Similar to the standard pipe operator in Linux.
What I mean by it doesn't work... Well, it's painful to debug. I found out that it's actually just not sending data. I haven't been able to figure out the returned errno yet, but my guess is it's a broken pipe.
But there MUST be a cleaner way to fix such a thing. Yes, sleeping for a very short time is my current solution, and I have implemented it and it works. But I find it a disgusting hack..