5

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. Commented May 8, 2014 at 4:18
  • Possibly inotify-tools? Commented 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". Commented May 8, 2014 at 6:17
  • Thanks, @Andrew, possibly inotifywatch if used correctly. I'll look at that and get back to you. Commented May 8, 2014 at 6:24

2 Answers 2

4

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. Commented May 8, 2014 at 12:40
  • @ewwhite Make it a question! I'd love to know how else you're using this. :-) Commented 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# Commented 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. Commented 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); Commented May 8, 2014 at 6:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.