Thread: Named pipe problem

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Named pipe problem

    Hi
    This is my first post on this forum!
    I have a process where I am opening a named pipe (in non blocking mode) and then doing a select on, waiting for other processes to write to the pipe.

    However, what i noticed is that once a select is successful and a process has written to it, the subsequent selects all say that it is ready to read, when there is no data (as no other process has written to the pipe after that).

    Just as a note that i am doing a FD_ZERO of the named pipe descriptor before each select, and after the FD_ISSET is checked and is true for the named pipe, I am doing a FD_CLEAR on it as well.
    This is a bit strange, as the whole point of using select is to know when there is data available to be read.

    However, if I close/open the pipe after the read, then it's fine again and the select keeps doing what i need it to do.


    Any help in fixing this problem/clarifying what I have observed would be gratefully accepted!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rahul_c View Post
    However, what i noticed is that once a select is successful and a process has written to it, the subsequent selects all say that it is ready to read, when there is no data (as no other process has written to the pipe after that).
    A common misunderstanding of what select() does. select() does not determine whether data is available. What it determines is whether a read operation will BLOCK. Since you've opened the FIFO in non-blocking mode, it obviously will never block, and so select() never waits.

    This is a bit strange, as the whole point of using select is to know when there is data available to be read.
    No, the point of select() is to tell you whether the operation will block. It has nothing to do with whether data is available or not. You have to determine that yourself, by examining the return code from the read() call.

    However, if I close/open the pipe after the read, then it's fine again and the select keeps doing what i need it to do.
    It isn't select() waiting here, rather, it is the call to open() the FIFO for read which blocks, until a corresponding writer opens the other side.

    In summary, don't use non-blocking mode when using select().

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    In the same process, I have opened sockets in non-blocking mode, and then I can use selects to wake up my process when there is something to read. That seems to work without any problems at all.
    What you are saying about named pipes, if it applies to sockets as well, would make my select always complete without waiting, which isn't the case.

    When the pipe has nothing written to it, even opening the pipe in non-blocking mode makes it wait on the select. It is just that after the first write from the client process (when the select does wake up my server process), further selects always finish immediately saying it is ready to read even when I have cleared the bit masks using FD_CLEAR and FD_ZERO. That is my confusion as to why it happens that way.
    Last edited by rahul_c; 10-02-2007 at 04:56 PM.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Secondly, if I dont open the fifo in non-blocking mode for reading, then the read will block and my code will not progress beyond the open. I have to open the FIFO and then wait on the select, so a write wakes it up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Problem with signals
    By kahad in forum C Programming
    Replies: 9
    Last Post: 12-07-2006, 10:42 AM
  4. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  5. named pipe problem
    By fnoyan in forum Linux Programming
    Replies: 0
    Last Post: 05-28-2006, 05:54 AM