Using flock, several processes can have a shared lock at the same time, or be waiting to acquire a write lock. How do I get a list of these processes?

That is, for a given file X, ideally to find the process id of each process which either holds, or is waiting for, a lock on the file. It would be a very good start though just to get a count of the number of processes waiting for a lock.


lslocks, from the util-linux package, does exactly this.

In the MODE column, processes waiting for a lock will be marked with a *.

  • 3
    Apt-cache says that util-linux is already the newest version (2.20.1-1ubuntu3), but I don't have lslocks; is there a repo I can use that will give me it? – Benubird Aug 8 '13 at 15:36
  • 2
    Looks like this was added in 2.22, so Ubuntu's version is too old. Presumably a new version will be available eventually. (This is also the case with RHEL 6 or CentOS.) You could build it yourself, or you could use the lsof approach Joel Davis suggests. – mattdm Aug 8 '13 at 18:26
  • 6
    lslocks reads /proc/locks, in a pinch you can read that directly yourself, with the caveat that files are identified by device and inode rather than name. Since you know the file, that ought not be a problem. Blocked entries have a -> prefix before the lock type column (thus adding a column to that line). – mr.spuratic Aug 9 '13 at 10:34

Two possibilities: lsof (my preference) or lslk (specifically for file locks):

[root@policyServer ~]# lslk | grep "master.lock"
master      1650 253,0 12423   33  w 0    0  0    0   0 /var/lib/postfix/master.lock

[root@policyServer ~]# lsof | grep "master.lock"
master     1650      root   10uW     REG              253,0       33      12423 /var/lib/postfix/master.lock

Output of lslk is self-expanatory but lsof puts the lock description in the "FD" column (which is 10uW above). From the man page:

The mode character is followed by one of these lock characters, describing the type of lock applied to the file:

N for a Solaris NFS lock of unknown type;
r for read lock on part of the file;
R for a read lock on the entire file;
w for a write lock on part of the file;
W for a write lock on the entire file;
u for a read and write lock of any length;
U for a lock of unknown type;
x for an SCO OpenServer Xenix lock on part      of the file;
X for an SCO OpenServer Xenix lock on the      entire file;
                       space if there is no lock.

So the "FD" column of lsof above breaks down to:

10 The literal descriptor of this open file. What's linked to by /proc/1650/fd/10

u File is open for reading and writing

W program has a write lock on the file.

  • 1
    I can't find where to get lslk, and it appears to no longer be maintained. Worth pointing out also, is that if you want lsof to only show the processes that are actually locking the file, you need to grep for "^mutex". It also doesn't distinguish between 'holding' and 'blocking'. – Benubird Aug 9 '13 at 8:35
  • The first letter character of the FD field is the file it has the file opened with (I'm assuming that's what you mean by holding) the optional second letter character is the lock (if any) it has on the file (which I'm assuming is what you mean by "blocking") Also flock != mutex. Your grep would have missed locks like the one in the post (not to mention the first field is program name...) – Bratchley Aug 9 '13 at 11:49
  • D'oh! You're right - "mutex" was the name of my script, so grepping for 'mutex' is only applicable to my case. Thanks for pointing that out – Benubird Aug 9 '13 at 15:50
  • well looks like I made a boo-boo too: "first letter character is the mode it has the file opened with..." – Bratchley Aug 9 '13 at 17:06

lsof can help to see the list of file. here is way to see the locked files.

sudo lsof /var/lib/dpkg/lock 

in case lsof itself is missing on the system, ls /proc/*/fd/* | grep LOCK_FILE_NAME should provide the same information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.