student@ubuntu:~$ lsof /tmp/lk
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
flock 1094 student 3rW REG 253,4 0 2703 /tmp/lk
sleep 1096 student 3r REG 253,4 0 2703 /tmp/lk
flock 1112 student 3r REG 253,4 0 2703 /tmp/lk
FD is the descriptor number, then the mode: r — read, w — write,
u — both. A third letter is a file lock: W — a write lock on the whole
file, R — a read lock on the whole file, w and r — on part of it.
Instead of a number there can be cwd (current directory), txt (program
file), mem (a memory-mapped file).
Here flock 1094 holds the lock (3rW), sleep 1096 is its child with the
same open file, and flock 1112 has opened the file and waits: it has no
lock letter. Waiters also show in /proc/locks (see
proc-filesystem).
Deleted files: +L1 and DEL
A file that was deleted or replaced with a new one lives on while a
process holds it — an open unlinked file.
lsof +L1 shows open files that have no name left on disk. This is a
service whose binary was replaced by an update:
student@ubuntu:~$ sudo lsof +L1
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
demo-slee 4214 root txt REG 253,4 35336 0 4464
/usr/local/bin/demo-sleep (deleted)
NLINK 0 means the file has no names; txt means it is the program file.
A deleted shared library in a process's memory
does not show in +L1: such a file is not open but mapped into memory.
lsof for the process marks it DEL in the FD column:
student@ubuntu:~$ sudo lsof -p 3830 | grep -E 'COMMAND|DEL'
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sleep 3830 root DEL REG 253,4 2727
/usr/local/lib/libdemo.so.1
The same files marked (deleted) are in /proc/<PID>/maps (see
proc-filesystem). A replaced file is freed only when the process
restarts.