ben
ben

Reputation: 6023

Named pipe: How to block until closed write side is reopened?

I've got a process running which basically serves as a cache and communicates with other processes via named pipes. Periodically these other processes may send messages to the caching process. Now in principle what would work for me in the cache process is something like:

while(true) {
    int read_status = read(fifo_fd, buffer, BUFFER_SIZE);
    if (read_status > 0) {
        //at least a byte was read -> do something with the message
    }
}

But this obviously sucks in terms of performance since read() will return immediately on EOF resulting in a busy wait loop as soon as there is nothing to read in the pipe. (EDIT) I'm receiving EOF since a writing process may perform the following cycle multiple times: open() write() close()

I'd like to have the whole loop to be blocking if there is no character other than EOF in the pipe. I also investigated on select() and poll() which both are also not blocking for EOF.


Why am I needing this?

The caching process is supposed to be a daemon and the other processes are supposed to communicate with this daemon whenever the user invokes respective binaries.

Upvotes: 3

Views: 1870

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295403

Open your pipe read/write rather than read-only. This will prevent an EOF when the external writer closes it.

Alternately, when receiving an EOF, close the pipe and re-open it.

Upvotes: 4

Related Questions