2

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?

3
  • 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. Jun 4 '19 at 19:02
  • found this ... i don't know if it helps ... kodedevil.com/2017/07/07/linux-fifos-python ... found it by searching linux non-blocking pipe write
    – jsotola
    Jun 4 '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 Jun 4 '19 at 19:14
0

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."

-1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.