I use a FIFO (named pipe) for IPC. Now process A calls
mkfifo(path)
open(path)
Naturally open()
will block until the file is written to by process B. Now I need a way to invalidate a FIFO. Therefore I call
unlink(path)
Now I expected that any blocking open
call would return, but they don't and my process hangs indefinitely.
How can I unblock the open
call when the FIFO is unlinked?
Do I have to resort to O_NONBLOCK
?
PS: I tried the suggested write/unlink/close approach to no avail. The open
call blocks immediately.
void invalidate() {
int fd = open(path, O_WRONLY)
unlink(path)
close(fd)
}
I think the issue is
The FIFO must be opened on both ends (reading and writing) before data can be passed. Normally, opening the FIFO blocks until the other end is opened also.
However invalidate
should work without knowing if the FIFO is currently opened for reading or not.
The invalidator should open the FIFO in non-blocking write mode. This way, its call to open()
won't hang if there are no readers waiting for it. It should then close the FIFO immediately.
Any processes that were waiting in a read-only open will return when the FIFO is opened. Then they'll immediately read EOF.
void invalidate() {
int fd = open(path, O_WRONLY | O_NONBLOCK);
unlink(path);
close(fd);
}