小编all*_*azz的帖子

多个进程之间的通信

我有一个 bash 脚本,它运行 manager() 函数作为 x 次的单独进程。如何从脚本中将消息转发到所有 manager() 进程?

我读过匿名管道,但我不知道如何与它共享消息..我尝试使用命名管道来做,但似乎我必须为每个进程创建一个单独的命名管道?

什么是最优雅的方式来做到这一点?

到目前为止,这是我的代码:

#!/bin/bash

manager () {
    while :
    do
        echo "read what has been passed to \$line"
    done
}

x=1
while [ $x -le 5 ]
do
  manager x &
  x=$(( $x + 1 ))
done


while :
do
while read line
do
  echo "What has been passed through the pipe is ${line}"
  # should pass $line to every manager process

done < $1
done

exit 0
Run Code Online (Sandbox Code Playgroud)

bash pipe shell-script concurrency

13
推荐指数
1
解决办法
7423
查看次数

标签 统计

bash ×1

concurrency ×1

pipe ×1

shell-script ×1