C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-02-2009, 06:01 AM   #1
Registered User
 
Join Date: Oct 2008
Posts: 567
Pipes sometimes fail

Hello all,

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   Reply With Quote
Old 05-02-2009, 06:50 AM   #2
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,230
Quote:
Originally Posted by EVOEx View Post
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?
It's hard to tell what you mean by "doesn't seem to work" without seeing some code.

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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 05-02-2009, 01:47 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:02 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22