Linux Labs

Inside the system: resources, kernel, processes / Stage 7 of 12

strace and /proc: what a process is doing

Not running Starting usually ~20 s
Leave the lesson

Step 5 of 8 · theory · 6 min

/proc: a process from the inside

strace named the call and the file number, and /proc turns the number into a name. What remains is to find whom importer is waiting for.

flock(3, LOCK_EX — importer asks for a lock on the file with descriptor 3 and waits: another process holds it. This is how programs agree not to work on the same data at the same time.

/proc is a directory the kernel builds on the fly. Each process has its own directory in it, named by PID, and the files inside are what the kernel knows about the process right now:

File What it holds
fd/ open files: descriptor number → path, socket or pipe
status name, state, parent, owner, memory
cmdline, cwd, environ command line, current directory, environment variables

You read your own process without sudo, and the fd of a root process only with sudo. Ubuntu does not let strace attach without sudo even to your own process unless you started it from your shell — hence Operation not permitted.

Who holds a file is found from the other side — across all processes at once. That is what lsof does:

bash
lsof import.lock
# COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
# archiver 950 student    3wW  REG  253,4        0 308088 import.lock
# importer 957 student    3w   REG  253,4        0 308088 import.lock

W after the descriptor number is a write lock. archiver holds it, and importer only waits.

Check yourself 1 question

archiver holds the lock on import.lock, and importer waits in flock. What happens to importer if archiver ends?

  • The kernel releases the lock when the holder closes the file or ends, and hands it straight to the waiting process. No restart is needed.

  • archiver's lock goes away, while the import.lock file stays in place. For importer this is exactly the moment it was waiting for.

When archiver ends, the kernel releases its lock and gives it to importer. importer loads the orders, writes imported.txt and exits — pgrep importer finds nothing any more.

Key points

  • sudo strace -p <PID> shows the call a process is waiting in; without sudo Ubuntu does not let you attach.
  • /proc/<PID>/fd turns a descriptor number into a file, and status shows the process's state and owner.
  • lsof <file> finds every process with that file; W in the FD column marks who holds the lock.
Ubuntu 24.04
Loading…