How to flush a named pipe (FIFO) after reading from it?

This is a discussion on How to flush a named pipe (FIFO) after reading from it? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <limits.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main() { mkfifo("test",0777); int ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64

    How to flush a named pipe (FIFO) after reading from it?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main() {
      mkfifo("test",0777);
      int i = open("test",O_RDONLY);
      char* msg = malloc(PIPE_BUF*sizeof(char));
      while(1) {
        read(i,msg,PIPE_BUF);
        printf("%s\n",msg);
      }
      return 0;
    }
    I build this small program on Ubuntu Linux and successfully compiled it with gcc and executed it. However, if I execute the following command:
    Code:
    echo "This is a sample test" > test
    the program keeps printing "This is a sample test" indefinitely. But what I want is to change the program so that it blocks each time it reads, until a new message arrives.

    How do I do it? Thank you in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always try checking the return value of read. Then do something appropriate.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. working with FIFO files
    By icebabe in forum C Programming
    Replies: 6
    Last Post: 05-06-2006, 11:35 AM
  2. Having trouble with a named pipe
    By crazeinc in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 01:00 AM
  3. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 01:48 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. Pipe the console from a DLL
    By loobian in forum Windows Programming
    Replies: 12
    Last Post: 11-30-2003, 07:55 PM

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