Bash queue - atomic wait & lock

情到浓时终转凉″ 提交于 2019-12-08 10:23:26

问题


I want to use a queue in Bash, which one process (eg. one Bash script) will be writing to and several other processes will be reading from.

I want to do this:

writer:

# block if another process is reading or writing,
# then lock the queue using an exclusive lock
lock -ex queue  
echo "$message" >> queue
unlock queue

reader:

# wait until there is an item in the queue
# and then lock the queue using a shared lock
wait_while_empty_and_lock -sh queue
read -r message < <(tail -n+1 queue)
(do stuff...)
unlock queue

Preferably, I would like to use Bash + standard / commonly available Linux/UNIX utilities (not Redis et al.). I have considered several approaches involving flock, several FIFOs, etc., but I couldn't come up with satisfactory results. Is there a simple way to do this in Bash/Linux/UNIX, or do I have to resort to third-party tools such as Redis?

来源:https://stackoverflow.com/questions/39666334/bash-queue-atomic-wait-lock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!