I have a script that writes to a named pipe and another that reads from the pipe. Occasionally, when starting the script I have noticed that the contents of the pipe exist from a previous run of the script. Is there a way to flush out the pipe at the beginning of the script?
A:
You can read from the pipe until it is empty. This will effectively flush it.
Before you attempt this daring feat, call fcntl(mypipe, F_SETFL, O_NONBLOCK)
(I don't know the shell-scripting equivalent) to make a read when the pipe is empty not hang your program.
Borealid
2010-07-27 22:49:43
+1
A:
I think dd
is your friend:
dd if=myfifo iflag=nonblock of=/dev/null
strace shows
open("myfifo", O_RDONLY|O_NONBLOCK)
and indeed doesn't even block on an empty fifo.
mvds
2010-07-27 23:26:00
mvds is my friend.
User1
2010-07-28 22:58:29
A:
Try this:
"Opening the FD read/write rather than read-only when setting up the pipeline prevents blocking."
from:
fooit
2010-07-28 21:59:28