Question about reading from pipes.

This is a discussion on Question about reading from pipes. within the C Programming forums, part of the General Programming Boards category; I use a pipe to send an int from father to child. I write in father, and use read in ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    Question about reading from pipes.

    I use a pipe to send an int from father to child.
    I write in father, and use read in child.

    I thought that when i read from the pipe it becomes empty, so i do another read to wait for a second int.

    The problem is that when i make the second read it picks up the int from the 1st message.

    Am i making a mistake or is this behavior expected?
    If so, can i empty the pipe so that the second read only reads when i write another int in the father?
    Thank You

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess is that the second read is actually not picking up anything, but returning some error code similar to "pipe empty", and you are not checking the result carefully enough. When you read data like that, and get an error, the original data will be undefined (it may have changed, but it may also be the same).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Are you calling FlushFileBuffers after your Readfile call in the child?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BobS0327 View Post
    Are you calling FlushFileBuffers after your Readfile call in the child?
    I got the impression the original post related to Unix pipes - I could be wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by matsp View Post
    I got the impression the original post related to Unix pipes - I could be wrong.

    --
    Mats
    I believe you're right. Assuming it is Unix pipes, one possible option would be to call fflush on the pipe. This is assuming that the OPer used fdopen to create a stream as opposed to using the fopen call. The former would allow the use of standard I/O functions.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by Ironic View Post
    I use a pipe to send an int from father to child.
    I write in father, and use read in child.

    I thought that when i read from the pipe it becomes empty, so i do another read to wait for a second int.
    Yes after reading from the pipe it is empty. Calling read() a second time makes it wait for the second int as it blocks until data is available in the pipe.
    Quote Originally Posted by Ironic View Post
    The problem is that when i make the second read it picks up the int from the 1st message.

    Am i making a mistake or is this behavior expected?
    That is not expected behavior. Follow matsp's advice and examine its return value to determine if the child process indeed read something off the pipe.
    Quote Originally Posted by Ironic View Post
    If so, can i empty the pipe so that the second read only reads when i write another int in the father?
    Thank You
    The pipe is emptied automatically by the kernel and the read() in the child blocks until the parent writes data to the pipe.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    i am sure that the father only sends one message.

    I have a infinite cycle in child in which it i read from pipe, and type out number of read bytes and the message.

    in the 1st cycle it prints correct number of bytes and message.
    But then it starts repeating itself. it says 0 bytes read, and the variable in which the message should go remains the same.

    Canīt understand why...
    Just so you can check the code: (msg is a struct)
    Code:
    write(fdclient[WRITE],&msg,sizeof(msg));
    Code:
    read((int)fdclient,&msg,sizeof(msg))

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by Ironic View Post
    i am sure that the father only sends one message.

    I have a infinite cycle in child in which it i read from pipe, and type out number of read bytes and the message.

    in the 1st cycle it prints correct number of bytes and message.
    But then it starts repeating itself. it says 0 bytes read, and the variable in which the message should go remains the same.

    Canīt understand why...
    That's because the parent process exits, the read() call in the child process sees an EOF and returns a value of zero to the caller.
    As such no data transfer takes place and the buffer used as the argument to read() is not overwritten or clobbered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. A question about pipes
    By Overworked_PhD in forum Linux Programming
    Replies: 1
    Last Post: 02-11-2008, 02:57 AM
  3. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  4. reading data from vector question
    By Rubiks14 in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2006, 10:40 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

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