Pipes sometimes fail

This is a discussion on Pipes sometimes fail within the Linux Programming forums, part of the Platform Specific Boards category; Hello all, I'm working on an application which basically does the following: 1. Create pipes for stdin/stdout/stderr 2. Fork Then ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262

    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.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 03:15 AM
  2. Pipes
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 03-31-2009, 04:09 AM
  3. bad and fail of steam
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 02-19-2008, 03:07 AM
  4. Need some help with pipes please.
    By carrja99 in forum C Programming
    Replies: 1
    Last Post: 05-05-2004, 05:13 PM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 07:46 AM

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