1

I've found many questions and answers about pipes on Linux, but almost all discuss the reader side.

For a process that shall be ready to deliver data to a named pipe as soon as the data is available and a reading process is connected, is there a way to, in a non-blocking fashion:

  1. wait (poll(2)) for reader to open the pipe,
  2. wait in a loop (again poll(2)) for signal that writing to the pipe will not block, and
  3. when such signal is received, check how many bytes may be written to the pipe without blocking

I understand how to do (2.), but I wasn't able to find consistent answers for (1.) and (3.).

EDIT: I was looking for (something like) FIONWRITE for pipes, but Linux does not have FIONWRITE (for pipes) (?)

EDIT2: The intended main loop for the writer (kind of pseudo code, target language is C/C++):

forever
    poll(can_read_command, can_write_to_the_fifo)
    if (can_read_command) {
        read and parse command
        update internal status
        continue
    }
    if (can_write_to_the_fifo) {
        length = min(data_available, space_for_nonblocking_write)
        write(output_fifo, buffer, length)
        update internal status
        continue
    }
Zrin
  • 919
  • 15
  • 25
  • I am suffering some confusion. In the 2nd paragraph you say you are interested in the situation where both fifo ends are already open. Doesn't that negate number (1)? – Duck Oct 25 '13 at 15:57
  • What would be the point of (3)? Let's say poll tells you there is room to write something. You somehow check and decide you can't write your full message to the fifo. What are you going to do besides go back to poll and have it tell you that it is ready to write again? – Duck Oct 25 '13 at 16:04
  • @Duck: it can happen that a reader process is already there when the writer gets to open the pipe, but it can also happen that the reader will "arrive" later. The writer needs to wait for another command from a control process which may as well cancel the output. It is important not to get blocked on write(). Even if the reader is connected, the pipe might get full. Under no circumstances shall the writer get blocked. – Zrin Oct 26 '13 at 19:07
  • 1
    On (1) there is no great solution. It is the single biggest pain about FIFOs. You either have to use the "open as read/write" trick or open it as non-blocking in which case it will fail until there is a reader. You are then left with constantly polling to see if there is a reader until you get a successful open. On (3) so you have it open as non-blocking. The worst case is that you write to what space is available (say 10 of 30 msg bytes) and go back to poll. Unless there is absolute necessity to write the whole message at once you aren't losing anything. There is 0 guarantee the – Duck Oct 26 '13 at 19:31
  • reader is going to process all 30 bytes at once anyway. IOW, just open it as non-blocking. That is exactly what you are trying to do in a harder way. – Duck Oct 26 '13 at 19:31
  • Oh, it occurs to me that maybe you are confused about non-blocking. NB doesn't mean you return immediately if there isn't enough space. It means as many bytes will be written as there is space available and then you return. You write the remainder of the bytes on subsequent writes. – Duck Oct 26 '13 at 19:42

0 Answers0