I open a named pipe (fifo created by mkfifo) with non blocking flag (open(...O_NONBLOCK)) then start polling (poll
Because pipes provide only a single unidirectional channel (not a separate bidirectional channel for each client like a socket), they are normally used when you have just one process that needs to send data to only one other process. When the writer closes the pipe, POLLHUP
(hangup) tells the reader that the pipe is closed and it can finish processing and terminate.
It is possible to use a pipe with multiple writers but you will need to be careful if the messages could be larger than PIPE_BUF
or 512 bytes. Otherwise, because it is only a single channel, the messages from multiple writers writing at the same time could be interleaved. Also because it is a single channel you won't be able to tell whether a long message is a single write from one client or multiple writes from multiple clients, unless you have some convention like one line (terminated by a newline) per client message.
POLLHUP
indicates that the last writer has closed the pipe, and persists until another process opens the pipe for writing or it is closed by all readers. If you don't want this, open the pipe with O_RDWR
instead of O_RDONLY
so that the pipe will stay open. This works because then there will always be a writer (your program) as long as you have it open.