问题
Let's say the system powers down unexpectedly due to a power outage.
Are flock locks always considered to be "unlocked" when the system starts up?
On Linux, flock relies on fcntl(...) (file descriptors).
Asked another way: Is it unnecessary to manually call flock -u <lock_filename>
when the system first starts up? (i.e. from cron @reboot)?
Update:
BSD flock man pages says:
Locks are on files, not file descriptors. That is, file descriptors
duplicated through dup(2) or fork(2) do not result in multiple instances
of a lock, but rather multiple references to a single lock.
回答1:
My Linux guru friend here mentions that there is a kernel locks table (for file locks) (usually stored in memory) which disappears on a reboot.
And that the file lock is just there for as long as the process is running.
回答2:
According to the linux man page:
Locks created by flock() are associated with an open file table entry.
This is a data structure in the kernels memory and not in the file system that may be on persistent disk storage.
Open files are closed when the process exits - thus flocks are valid only as long as a process holding it is running.
来源:https://stackoverflow.com/questions/20070943/do-flock-locks-reset-after-a-system-restart