file locks

From Noah.org
Revision as of 17:15, 3 August 2015 by Root (talk | contribs) (Created page with 'Category: Engineering The Linux command-line utility, '''flock''' can be used to work with locks. To see locks us '''lsof''' and view '''/proc/locks'''. == /proc/locks == …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


The Linux command-line utility, flock can be used to work with locks.

To see locks us lsof and view /proc/locks.

/proc/locks

Field #5 gives the PID of the process that obtained the lock. Note that this PID may be dead because the lock may be inherited by a parent or subprocess or thread. You may have to search around a bit. The third sub-field of field #6 gives the inode of the lock. This often must be used to find the PID of the process that currently holds the lock.

1: FLOCK  ADVISORY  WRITE 13863 00:11:4181978 0 EOF                                                                                        
2: POSIX  ADVISORY  WRITE 882 ca:01:4559 0 EOF                                                                                             
3: POSIX  ADVISORY  WRITE 1063 00:0f:8265 0 EOF                                                                                            
4: FLOCK  ADVISORY  WRITE 987 ca:01:262934 0 EOF                                                                                           
5: FLOCK  ADVISORY  WRITE 987 ca:01:262780 0 EOF                                                                                           
6: POSIX  ADVISORY  WRITE 761 00:0f:7622 0 EOF                                                                                             
7: FLOCK  ADVISORY  WRITE 740 00:0f:7608 0 EOF  

This command will produce a list of locks by searching for inodes, instead of PIDs. The first field is the PID of the process that originally obtained the lock; the second field is the /proc path to the PID that currently holds the lock (and the filedescriptor seen by that PID); the last field is the full path to the locked file. The first field PID often the same as the PID found in the second field, but not always.

while IFS=': ' read x x x x pid x x inode x; do sudo find -L /proc/*/fd -maxdepth 1 -inum "${inode}" -printf "${pid}: %p: " -exec readlink {} \; -quit; done < /proc/locks