preventing锁传播 [英] Preventing lock propagation

查看:127
本文介绍了preventing锁传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单而看似可靠的方法bash下锁定是:

A simple and seemingly reliable way to do locking under bash is:

exec 9>>lockfile
flock 9

然而,众所周知,bash的传播这样的FD锁分叉所有的东西,包括执行的程序等。

However, bash notoriously propagates such a fd lock to all forked stuff including executed programs etc.

有没有办法告诉bash不能复制FD?这是伟大的锁连接到当程序终止时,不管它如何终止该被删除的FD。

Is there any way to tell bash not to duplicate the fd? It's great that the lock is attached to a fd which gets removed when the program terminates, no matter how it gets terminated.

我知道我可以做的东西,如:

I know I can do stuff like:

run_some_prog 9>&-

不过那是相当繁琐的。

But that is quite tedious.

有没有更好的解决方案?

Is there any better solution?

推荐答案

您可以使用 -o 命令行选项,羊群(1) (长选项 - 关闭,这可能是在为自我记录性质的脚本写出更好的)指定文件描述符应通过执行命令羊群之前关闭(1)

You can use the -o command line option to flock(1) (long option --close, which might be better for writing in scripts for the self-documenting nature) to specify that the file descriptor should be closed before executing commands via flock(1):

   -o, --close
          Close the file descriptor on which the lock is held
          before executing command.  This is useful if command
          spawns a child process which should not be holding
          the lock.

这篇关于preventing锁传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆