4

I have a point of contention on my linux server. One of a number of processes access a single file and lock the file at a random time for a considerable period (>60 seconds) which, in turn, causes other things to fail.

Is there a way to detect how long a file has been locked and by which process?

4
  • lsof will help you find which process locked the file, but doesn't give you how long since the process lock it.
    – masegaloeh
    May 8, 2014 at 4:18
  • Possibly inotify-tools?
    – Andrew
    May 8, 2014 at 4:57
  • @masegaloeh Yes, I was aware of lsof for the second part of the question, hence the emphasis on "how long". May 8, 2014 at 6:17
  • Thanks, @Andrew, possibly inotifywatch if used correctly. I'll look at that and get back to you. May 8, 2014 at 6:24

2 Answers 2

3

I think what you are looking for is the file /proc/locks. It shows the current file locks in the system. This not shows how long a file has been locked, but it shows by which process. Maybe you could detect when the lock is register in this file and measure the elapsed time. A sample is this:

cat /proc/locks 
1: POSIX  ADVISORY  WRITE 2245 08:06:1182714 1073741824 1073741824
2: POSIX  ADVISORY  WRITE 2245 08:06:1182714 1073741826 1073742335
3: POSIX  ADVISORY  WRITE 3058 08:06:10752740 0 0
4: POSIX  ADVISORY  WRITE 3058 08:06:10752739 0 0
5: POSIX  ADVISORY  WRITE 2421 08:06:10752766 0 EOF
6: POSIX  ADVISORY  WRITE 2421 08:06:11142048 0 EOF
7: POSIX  ADVISORY  WRITE 2421 08:06:9964366 1073741824 1073742335
8: POSIX  ADVISORY  WRITE 2421 08:06:11142040 0 EOF

Where the columns are:

  • First: lock #.
  • Second: lock type (POSIX if the lock was created with fcntl and FLOCK if created with flock.
  • Third: lock mode (ADVISORY or MANDATORY)
  • Forth: Lock type (WRITE or READ), corresponding to locks shared or exclusive.
  • Fifth: PID of the process with the lock.
  • Sixth: Three numbers separated by : that identified the locked file.
  • Seventh: Start byte of the lock.
  • Eighth: End byte of the lock
3
  • 1
    +1 I was never really aware of /proc/locks - This actually solves another problem I have on some of my systems.
    – ewwhite
    May 8, 2014 at 12:40
  • @ewwhite Make it a question! I'd love to know how else you're using this. :-) May 9, 2014 at 0:40
  • +1. Just FYI, "Sixth: Three numbers separated by : that identified the locked file." ; i think the 3 numbers that id the file are- major#:minor#:inode#
    – kaiwan
    May 4, 2015 at 12:20
-2

You can tell how old a lock file is by simply looking at the timestamp when it was created. Similarly if you "cat logfile.name" it will very often have, as its sole contents, the process ID which created it.

2
  • Ahhhh.......You misread the question there is no lockfile.....what this user is asking concerns the lock function on a file that is open.
    – mdpc
    May 8, 2014 at 4:30
  • Indeed, @davidgo, there is no lockfile, the processes have a lock on a file. e.g. int result = flock(fd, LOCK_SH); May 8, 2014 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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