Part 3 of 6 · 2 min read
Per-process files
Think of a patient's folder in a hospital: one sheet for identity, one for drugs, one for allergies, one for the last visit. Each sheet answers one kind of question. The analogy stops in one place: a folder is written by staff over weeks, and /proc/PID is rebuilt at each read.
In plain words: The directory /proc/PID holds one file or link for each kind of fact about the process.
| File | Content | Level |
|---|---|---|
status |
Name, state, IDs, threads, VmSize, VmRSS, capabilities, Seccomp (readable text) |
1, 6, 10 |
cmdline |
The command line, arguments separated by zero bytes | 1 |
cwd, exe, root |
Links to the working directory, the program file and the root directory | 9 |
fd/ |
One link for each open file descriptor | 1, 9 |
limits |
The resource limits (rlimit) |
10 |
maps, smaps |
The memory mappings, with sizes and permissions | 6, 7 |
io |
Bytes and calls of reading and writing (rchar, wchar, syscr, syscw) |
8 |
ns/ |
The namespaces | 11 |
cgroup |
The control group | 11 |
A worked example. For the shell of the test run:
$ readlink /proc/$$/cwd /proc/$$/exe
/tmp/l8/l12
/usr/bin/bash
$ grep -E "Max (open files|processes|stack size)" /proc/$$/limits
Max stack size 8388608 unlimited bytes
Max processes 32017 32017 processes
Max open files 20000 20000 files
$ grep -E "^(rchar|wchar|syscr|syscw)" /proc/self/io (for a `cat`)
rchar: 4092 wchar: 0 syscr: 9 syscw: 0
The shell's working directory and program are links. The limits show a stack of 8,388,608 bytes (8 MiB), at most 32,017 processes and 20,000 open files. The io counters for the short cat command show 4,092 characters read through 9 read calls, and none written to files.
The key lines. /proc/self is a link to the directory of the process that reads it, so a program can inspect itself without knowing its number.
/proc/PID
The directory /proc/PID is the view of the kernel's record of one process. Its files and links give the state, memory, open files, limits, namespaces and group of the process, produced from kernel data at the time of reading.
Example: /proc/self/fd lists the open descriptors of the program that is reading it.
Check yourself:
1. Where is the working directory of a process found?
2. Which file gives the resource limits?
3. What does /proc/self mean?