![]() |
| | #1 |
| Registered User Join Date: Oct 2008
Posts: 567
| Pipes sometimes fail I'm working on an application which basically does the following: 1. Create pipes for stdin/stdout/stderr 2. Fork 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. |
| EVOEx is offline | |
| | #2 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,230
| Quote:
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. | |
| MK27 is offline | |
| | #3 |
| Registered User Join Date: Oct 2008
Posts: 567
| 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.. |
| EVOEx is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Non-blocking socket connection problem | cbalu | Linux Programming | 25 | 06-03-2009 02:15 AM |
| Pipes | Martin Kovac | C Programming | 1 | 03-31-2009 03:09 AM |
| bad and fail of steam | George2 | C++ Programming | 8 | 02-19-2008 03:07 AM |
| Need some help with pipes please. | carrja99 | C Programming | 1 | 05-05-2004 04:13 PM |
| Services and Pipes | nickname_changed | Windows Programming | 0 | 07-16-2003 06:46 AM |