经过大约 100 次组合后,我得到了以下代码,该代码使用一个进程作为服务器,通过管道读取来自客户端的响应,我称之为“伦敦”函数,它是管道“london_pipe”(我非常不确定将管道称为“伦敦” ' 也是因为命名冲突?)。在下面,有 2 个客户端,每个客户端都通过自己的管道与服务器进行通信以读取数据。我遵循相同的命名约定,使用用于读取的函数的名称来命名管道。所以,'berlin' 正在读取 'berlin_pipe' 等。
整体结构使用1个管道到1个客户端和1个管道用于服务器,如下所示:
london ----writes madrid pipe-------->
london <----reads london_pipe----<- |
london ----writes berlin_pipe---> | |
| ^ |
| | |
berlin <---reads berlin_pipe--<- | |
berlin ----writes london_pipe-----| |
^ |
| |
madrid ----writes london_pipe------ v
madrid <----reads madrid_pipe------<-
现在,当我运行我的代码时,我遇到了一个非常奇怪的情况,我的客户反复读取空字符串 - 当我构建我的程序时 - 他们响应向服务器发送错误警报!当我添加 if 子句(参见代码注释)时,问题就消失了!
任何人都可以解释我的管道的奇怪行为吗?还是代码本身存在某种问题?
简而言之,一些澄清:代码
while true; do
.......
done 4<"$pipename"
即使 fd 4 每次都反复用于指向不同的管道,也适用于所有客户端。现在代码:
#!/bin/bash
shopt -u failglob
shopt -s extglob nullglob dotglob
DIR=$( cd "$( dirname "$0" )" && pwd )
#the server
function london (){
local i message answer london_pipe berlin_pipe madrid_pipe
london_pipe=london_$RANDOM.$RANDOM.$RANDOM.$$
madrid_pipe=madrid_$RANDOM.$RANDOM.$RANDOM.$$
berlin_pipe=berlin_$RANDOM.$RANDOM.$RANDOM.$$
cd $DIR
mkfifo $london_pipe
mkfifo $madrid_pipe
mkfifo $berlin_pipe
( madrid $madrid_pipe $london_pipe ) &
( berlin $berlin_pipe $london_pipe ) &
i=0
while true; do
if [[ i -gt 100 ]]; then
echo 'quit' > $madrid_pipe
echo 'quit' > $berlin_pipe
break
else
echo "loop #$i"
echo '========='
message="London loop (#$i)"
#***send to Madrid***#
echo "$message" > $madrid_pipe
read -r answer <&3
echo 'London says:> '"$answer"
#***send to Berlin***#
echo "$message" > $berlin_pipe
read -r answer <&3
echo 'London says:> '"$answer"
(( i++ ))
fi
done 3< $london_pipe
wait
cd "$DIR"
rm -rf $london_pipe
rm -rf $madrid_pipe
rm -rf $berlin_pipe
}
#a client
function berlin (){
local i message answer berlin_pipe london_pipe
berlin_pipe=$1
london_pipe=$2
cd $DIR
i=0
exec 3> $london_pipe
while true; do
read -r answer <&4
#***if deleted it reads empty strings!!!***#
if [[ ! $answer ]]; then
continue
fi
echo 'Berlin says:> '"$answer"
message="Greetings from Berlin!($i)"
echo "$message" >&3
(( i++ ))
if [[ $answer = 'quit' ]]; then
break
fi
done 4< "$berlin_pipe"
}
#another client
function madrid (){
local i message answer madrid_pipe london_pipe
madrid_pipe=$1
london_pipe=$2
cd $DIR
i=0
exec 3> $london_pipe
while true; do
read -r answer <&4
#***if deleted it reads empty strings!!!***#
if [[ ! $answer ]]; then
continue
fi
echo 'Madrid says:> '"$answer"
message="Greetings from Madrid!($i)"
echo "$message" >&3
(( i++ ))
if [[ $answer = 'quit' ]]; then
break
fi
done 4< "$madrid_pipe"
}
london
如果代码是这个块
#***if deleted it reads empty strings!!!***#
if [[ ! $answer ]]; then
continue
fi
有了这个,一些结果是:
loop #97
=========
Madrid says:> London loop (#97)
London says:> Greetings from Madrid!(97)
Berlin says:> London loop (#97)
London says:> Greetings from Berlin!(97)
没有两个客户:
loop #91
=========
Madrid says:> London loop (#86)
Madrid says:> London loop (#87)
Madrid says:> London loop (#88)
Madrid says:> London loop (#89)
Madrid says:> London loop (#90)
Madrid says:>
Madrid says:>
Madrid says:>
很明显,“马德里”确实读取了空字符串以及来自“伦敦”的循环编号为 86 等的消息,这些消息已被先前的空读取推送,现在出现故障。有人可以解释一下吗?
谢谢