3

I have this little test script:

rm fooo | cat
mkfifo fooo
echo 'bar' > fooo  # blocks here
echo 'done'

I am guessing that because there is nobody reading from the named pipe, that the write call will block until then.

Is there some way to write even if there are no readers or to check to see if there are no readers?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Alexander Mills
  • 9,330
  • 19
  • 95
  • 180
  • I don't necessarily need to write to the pipe - if it's not open for reading, I can just skip the write. But I don't know how to check if it's open for reading. – Alexander Mills Jun 04 '19 at 19:02
  • found this ... i don't know if it helps ... http://kodedevil.com/2017/07/07/linux-fifos-python/ ... found it by searching `linux non-blocking pipe write` – jsotola Jun 04 '19 at 19:11
  • I can't remember where I saw it but someone suggested using `dd` to open the named pipe in non-blocking mode or something – Alexander Mills Jun 04 '19 at 19:14

2 Answers2

-1

According to https://man7.org/linux/man-pages/man7/fifo.7.html:

" Under Linux, opening a FIFO for read and write will succeed both in blocking and nonblocking mode. POSIX leaves this behavior undefined. This can be used to open a FIFO for writing while there are no readers available. A process that uses both ends of the connection in order to communicate with itself should be very careful to avoid deadlocks."

-2

One solution would be to add a reader on the same line:

 echo "unlocked" > "$fifo" > >(read line; echo "$line")

but I don't know the right syntax for it...All I know is I need to open it for reading before the first write call blocks. The above doesn't seem to work.

Alexander Mills
  • 9,330
  • 19
  • 95
  • 180