【问题标题】:Is flock automatically released on process exit?进程退出时会自动释放羊群吗?
【发布时间】:2012-06-14 19:13:18
【问题描述】:

在Linux 的一个bash 脚本中,我使用flock [命令flock,而不是系统调用flock()] 来实现文件锁定,从而保护对共享资源[这是tmpfs 中的文件] 的并发访问。

我有陷阱处理程序来处理我的脚本的异常终止: trap "{ rm -rf $LOCK ; rm -rf $TMPFS_FILE; exit 255; }" SIGINT SIGTERM

其中 $LOCK 是我的锁定文件,$TMPFS_FILE 是我的共享资源。

我的问题是我是否也需要明确地进行文件解锁?还是 Linux 在所有程序终止时都为我做这件事 [包括自愿终止和强制] 情况?

【问题讨论】:

标签: linux bash unix operating-system


【解决方案1】:

来自man 1 flock

-u,--解锁

          Drop  a  lock.   This  is  usually not required, since a lock is
          automatically dropped when the file is closed.  However, it  may
          be  required  in special cases, for example if the enclosed com‐
          mand group may have forked a background process which should not
          be holding the lock.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-11
    • 2011-01-13
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    相关资源
    最近更新 更多