Data in linux FIFO seems lost
Posted
by Utoah
on Stack Overflow
See other posts from Stack Overflow
or by Utoah
Published on 2010-04-26T08:45:15Z
Indexed on
2010/04/26
9:03 UTC
Read the original article
Hit count: 413
Hi,
I have a bash script which wants to do some work in parallel, I did this by putting each job in an subshell which is run in the background. While the number of job running simultaneously should under some limit, I achieve this by first put some lines in a FIFO, then just before forking the subshell, the parent script is required to read a line from this FIFO. Only after it gets a line can it fork the subshell. Up to now, everything works fine. But when I tried to read a line from the FIFO in the subshell, it seems that only one subshell can get a line, even if there are apparently more lines in the FIFO. So I wonder why cannot other subshell(s) read a line even when there are more lines in the FIFO.
My testing code looks something like this:
#!/bin/sh
fifo_path="/tmp/fy_u_test2.fifo"
mkfifo $fifo_path
#open fifo for r/w at fd 6
exec 6 $fifo_path
process_num=5
#put $process_num lines in the FIFO
for ((i=0; i<${process_num}; i++)); do
echo "$i"
done >&6
delay_some(){
local index="$1"
echo "This is what u can see. $index \n"
sleep 20;
}
#In each iteration, try to read 2 lines from FIFO, one from this shell,
#the other from the subshell
for i in 1 2
do
date >>/tmp/fy_date
#If a line can be read from FIFO, run a subshell in bk, otherwise, block.
read -u6
echo " $$ Read --- $REPLY --- from 6 \n" >> /tmp/fy_date
{
delay_some $i
#Try to read a line from FIFO
read -u6
echo " $$ This is in child # $i, read --- $REPLY --- from 6 \n" >> /tmp/fy_date
} &
done
And the output file /tmp/fy_date has content of:
Mon Apr 26 16:02:18 CST 2010
32561 Read --- 0 --- from 6 \n
Mon Apr 26 16:02:18 CST 2010
32561 Read --- 1 --- from 6 \n
32561 This is in child # 1, read --- 2 --- from 6 \n
© Stack Overflow or respective owner