Search code examples
c++mkfifo

FIFO block program until other process read


I am using a FIFO file, created as mkfifo myFIFO on Linux terminal, in a C++ code bellow:

#include <iostream>
using namespace std;
#include <stdio.h>
int main(int argc, char** argv) {
     FILE* fp = fopen("/tmp/myFIFO", "w");
     fprintf(fp, "Hello, world!\n");
     fclose(fp);
     cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
     return 0;
 }

The program stays blocked until other process read myFIFO. How to avoid this? (I want that this peace of code stay processing and writing in the FIFO not carrying about the other process read. Until the FIFO be full, when, if possible, I may discard the oldest messages).


Solution

  • The best way that I found was, using the O_NONBLOCK option, write in the file/pipe the content of a internal queue of the software. So the buffer is created by the writer software and the pipe file have the function only of message channel.