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?

2 Answers2

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
-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.

davidgo
  • 5,964
  • 2
  • 21
  • 38
  • 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 08 '14 at 04:30
  • Indeed, @davidgo, there is no lockfile, the processes have a lock on a file. e.g. int result = flock(fd, LOCK_SH); – tudor -Reinstate Monica- May 08 '14 at 06:21