1

I'd like to use a FIFO buffer from a C++ code. There are two processes, one of them always writes to the FIFO, the other always reads it. It's very simple.

I don't really need to read the contents of the buffer, I just want to know how much data is in it, and clear it.

What is the most sophisticated solution for this in C++?

This code works well, but I never need the buffer's contents:

int num;
char buffer[32];

num = read(FIFO, buffer, sizeof(buffer));
//num is the important variable

Thank you!

Daniel
  • 2,318
  • 2
  • 22
  • 53
  • Your post is not clear. What is this FIFO? What do you want to achieve? – Kirill Kobelev Jun 24 '12 at 22:20
  • 1
    What kind of interprocess protocol communicates important variables using buffer length? – Seva Alekseyev Jun 24 '12 at 22:28
  • FIFO is a file descriptor created with mkfifo function. One process is continuously writing to the fifo, and this other one is reading from it. I only need to know that the other process is still alive. That's why that the buffer itself is not important at all. If I can read, it's okay. – Daniel Jun 25 '12 at 07:43

4 Answers4

0

You could take a look at this question: Determing the number of bytes ready to be recv()'d

On linux, code for sockets should work with minimal effort on FIFOs too. Windows, though, I'm not sure.

Community
  • 1
  • 1
Robert Mason
  • 3,949
  • 3
  • 31
  • 44
0

As far as I am aware of, the only way to clear bytes from a linux FIFO (short of destroying the FIFO) is to read them out. You can clear them out faster by reading larger amounts of data at a time (32 is a very small read size, unless that is the size that is normally written to the FIFO). If you are in blocking mode, then you should query for the bytes as described in the link indicated by Robert Mason. If the descriptor is in non-blocking mode, you can read until EAGAIN is returned to know it was cleared. You may use poll to determine when more data has arrived on the FIFO.

jxh
  • 69,070
  • 8
  • 110
  • 193
0

The only way to clear a pipe is to read it so the question of how many bytes are present is moot - you'll know after you read them. The real issues ends up being the same as for any read:

(1) If you don't care about the data then presumably you don't want to block waiting for it so make the FIFO non-blocking.

(2) Since you presumably don't want to sit and waste time polling the FIFO to see if there is something to read you should put the FIFO fd in a select statement. When there is something to read then drain it and add to a counter.

Duck
  • 26,924
  • 5
  • 64
  • 92
-1

Not sure if I've got you right, sophisticated - do you mean the most efficient, or the most obfuscated?

Anyway, if you don't need the buffer contents - you may just use a (shared) interlocked variable.

valdo
  • 12,632
  • 2
  • 37
  • 67
  • 1
    -1: Where is the answer? You are not giving any valuable advice. – Kirill Kobelev Jun 24 '12 at 22:21
  • @Kirill Kobelev: You're right, it's not a complete answer, but it **is** a valuable advice IMHO. Create a shared memory region (the API depends on the OS I guess), and you **only** interlocked operations (i.e. no locking, memory copying and etc.) on a single integer variable belonging to the shared memory. – valdo Jun 25 '12 at 08:48