I have one named pipe (FIFO) say : 'MY_FIFO'
One process reads the fifo from multiple processes to get instructions.
(1) reader bash script:
while read LINE < 'MY_FIFO'
do
# process line
done
(many) writer bash script:
while [ 1 ]
do
INSTRUCTION = # get an instruction
echo $INSTRUCTION > 'MY_FIFO'
done
This works fine to pass instructions to the reader when we have 1 writer or that the writers don't write at the same time. But if we have many writers writing at the same time, the reader gets the first message from whoever was the very first to write to the FIFO, and then hangs waiting for input instead of fetching the next instruction.
What happens behind the scene that prevents me from having multiple writers write to the same fifo? Are they overwriting each other's instructions?
Is there a way for me to test that the reader is ready to read from fifo before writing to the fifo?
flock-style exclusive lock that needs to be held before any writer opens the file. - Charles Duffy