bash 进程退出时群会自动释放吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10987606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:28:16  来源:igfitidea点击:

Is flock automatically released on process exit?

linuxbashunixoperating-system

提问by Bandicoot

In a bash script in Linux, I am using flock [the command flock, not the system call flock()] to implement file locking thereby guarding concurrent access against a shared resource [which is a file in tmpfs].

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

I have trap handlers to handle abnormal termination of my script: trap "{ rm -rf $LOCK ; rm -rf $TMPFS_FILE; exit 255; }" SIGINT SIGTERM

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

where $LOCK is my lock file and $TMPFS_FILE is my shared resource.

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

My question is do I need to explicitly do a file unlock as well ? Or does Linux do it for me upon all program termination [both voluntary termination as well as forced] scenarios ?

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

回答by Paused until further notice.

From man 1 flock:

来自man 1 flock

-u, --unlock

          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.

-u, --unlock

          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.