Bash 为什么等待可以';在命名管道中读卡器进程不工作?

Bash 为什么等待可以';在命名管道中读卡器进程不工作?,bash,wait,named-pipes,status,Bash,Wait,Named Pipes,Status,bash中的wait用于进程更改状态。在下面的bash脚本中,创建一个命名管道,打开5个进程将数据流写入其中,打开1个进程将数据流从其中读取到其他文件中 cat pipe.sh fifo_file=fifo.pipe mkfifo $fifo_file exec 6<>$fifo_file rm $fifo_file DateWrite () { i=0 while [[ $i -lt 200 ]] do str=`date`

bash中的
wait
用于进程更改状态。在下面的bash脚本中,创建一个命名管道,打开5个进程将数据流写入其中,打开1个进程将数据流从其中读取到其他文件中

cat pipe.sh
fifo_file=fifo.pipe
mkfifo $fifo_file
exec 6<>$fifo_file
rm $fifo_file

DateWrite ()
{
    i=0
    while [[ $i -lt 200 ]]
    do
        str=`date`
        i=$(( i+1 ))
        echo "$i $str"
    done
}

writers=()
for  (( i=0; i<5; i++ ))
do 
    DateWrite >&6  & 
    echo "add a writer process pid  $!"
    writers+=($!)
done


while read date_time
do
    echo $date_time >> output.file 
done  <&6  &
reader=$!

for writer in "${writers[@]}" 
do
    wait "$writer"
    echo "the status of writer process pid $writer changed"   
done

echo "reader process pid is $reader"
wait  "$reader"
echo "the status of reader process pid $reader changed"   

exec 6<&-
信息列表不包含

the status of reader process pid 4754 changed
语句
wait“$reader”
在无限while循环中执行,
wait
不能工作是正常的。
如果我删除两行:

wait  "$reader"
echo "the status of reader process pid $reader changed"   
并执行
bash pipe.sh

add a writer process pid  19670
add a writer process pid  19671
add a writer process pid  19673
add a writer process pid  19674
add a writer process pid  19675
the status of writer process pid 19670 changed
the status of writer process pid 19671 changed
the status of writer process pid 19673 changed
the status of writer process pid 19674 changed
the status of writer process pid 19675 changed
reader process pid is 19676
在控制台中搜索pid 19676

ps aux |grep '[p]ipe'
debian   19676  0.0  0.0  16516  2100 pts/1    S    19:54   0:00 bash pipe.sh
pipe.sh
完成时,在
pipe.sh
中创建的读取器进程仍在运行。
我不想在
pipe.sh
中将
wait“$reader”
更改为
kill“$reader”
是否有办法知道
echo$date\u time
已读取管道末端,并发送信号关闭读取过程

@菲利普和这里的所有朋友,
sleep 1;echo q>&6
,可能时间间隔很短。让我们假设更复杂的情况:

while read date_time
do
    test "$date_time" = q && exit
    echo $date_time >> output.file 
    bash_some_code_do_some_thing
done  <&6  &

睡眠100;echo q>&6#如果运行时间超过200秒怎么办


那么如何解决这个问题呢?

无法将
EOF
发送到一个以读写方式打开的管道。 解决方法之一是发送退出命令:

fifo_file=fifo.pipe
mkfifo $fifo_file
exec 6<>$fifo_file
rm $fifo_file

DateWrite ()
{
    i=0
    while [[ $i -lt 200 ]]
    do
        str=`date`
        i=$(( i+1 ))
        echo "$i $str"
    done
}

writers=()
for  (( i=0; i<5; i++ ))
do 
    DateWrite >&6  & 
    echo "add a writer process pid  $!"
    writers+=($!)
done


while read date_time
do
    test "$date_time" = q && exit
    echo $date_time >> output.file 
done  <&6  &
reader=$!

for writer in "${writers[@]}" 
do
    wait "$writer"
    echo "the status of writer process pid $writer changed"   
done

echo "reader process pid is $reader"
sleep 1; echo q >&6
wait  "$reader"
echo "the status of reader process pid $reader changed"   

exec 6<&-
fifo\u文件=fifo.pipe
mkfifo$fifo_文件
exec 6$fifo_文件
rm$fifo_文件
日期写入()
{
i=0
而[$i-lt 200]]
做
str=`日期`
i=$((i+1))
回显“$i$str”
完成
}
作者=()
对于((i=0;i&6&
echo“添加写入程序进程pid$!”
编剧+=($!)
完成
在读取日期和时间时
做
测试“$date\u time”=q&退出
echo$date\u time>>output.file
完成&6
等待“$reader”
echo“读卡器进程pid$reader的状态已更改”

exec 6您在等待读卡器完成后关闭FD 6,但在关闭FD 6之前读卡器不会完成。请参阅我更新的帖子,这不是原因,
close FD 6
与此无关。
sleep 10; echo q >&6
fifo_file=fifo.pipe
mkfifo $fifo_file
exec 6<>$fifo_file
rm $fifo_file

DateWrite ()
{
    i=0
    while [[ $i -lt 200 ]]
    do
        str=`date`
        i=$(( i+1 ))
        echo "$i $str"
    done
}

writers=()
for  (( i=0; i<5; i++ ))
do 
    DateWrite >&6  & 
    echo "add a writer process pid  $!"
    writers+=($!)
done


while read date_time
do
    test "$date_time" = q && exit
    echo $date_time >> output.file 
done  <&6  &
reader=$!

for writer in "${writers[@]}" 
do
    wait "$writer"
    echo "the status of writer process pid $writer changed"   
done

echo "reader process pid is $reader"
sleep 1; echo q >&6
wait  "$reader"
echo "the status of reader process pid $reader changed"   

exec 6<&-