为什么命名管道中的读取进程不能使用wait?

问题描述 投票:0回答:1

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<&-

要在脚本中检查wait命令 pipe.shbash pipe.sh.

add a writer process pid  4749
add a writer process pid  4750
add a writer process pid  4751
add a writer process pid  4752
add a writer process pid  4753
the status of writer process pid 4749 changed
the status of writer process pid 4750 changed
the status of writer process pid 4751 changed
the status of writer process pid 4752 changed
the status of writer process pid 4753 changed
reader process pid is 4754

信息列表不包含

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 是完成。我不想改变 wait "$reader" 变成 kill "$reader"pipe.sh. 有没有办法知道 echo $date_time 读取了管道的末端,并发送信号关闭读取过程?

@Philippe和这里的所有朋友。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  &

运转时间 bash_some_code_do_some_thing 长于10秒,至少10秒,不超过10秒,你无法估计精确的时间.你不能写这样的代码,如

sleep 10; echo q >&6

sleep 100; echo q >&6 #如果运行时间多了200秒怎么办?

如何解决这种情况?

bash wait named-pipes status
1个回答
1
投票

没有办法发送一个 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<&-

另一种方法是向阅读器发送TERM信号。

© www.soinside.com 2019 - 2024. All rights reserved.