Part 2 of 6 · 2 min read
Special file systems
Think of the gauges on a boiler. Nothing is stored in them: each time you look, the needle shows the state of the boiler at that moment. Reading a gauge does not move the water. The analogy stops in one place: a gauge can show one value, while /proc shows thousands, arranged like a file tree.
In plain words: /proc and /sys are special file systems (Level 9). They hold no data on a disk. When a program reads a file there, the kernel makes the content at that moment from its own tables, and returns it as text. /proc began as the information about processes: a directory for each process ID, plus files about the whole system. /sys shows the kernel's device and driver model, in a tree of directories and one-value files. Some files can be written too, and a write changes the kernel's setting (for example /proc/sys/vm/swappiness). Because the content is produced on demand, the files report a size of 0, and each read returns the current state.
A worked example. On the test machine, some files and their content:
$ cat /sys/block/vda/queue/rotational
1
$ cat /sys/block/vda/queue/scheduler
none [mq-deadline] kyber bfq
$ cat /proc/sys/kernel/pid_max
32768
$ cat /proc/sys/vm/swappiness
60
$ cat /proc/sys/kernel/randomize_va_space
2
The first says that the disk vda reports itself as rotating (a virtual disk may claim anything, Level 8). The second shows the I/O scheduler: the bracket marks the one in use, mq-deadline (Level 8). The next three are kernel settings: the largest process number (32,768), how eagerly the kernel swaps (60, Level 7) and the address randomisation of Level 10 (2, full).
The key lines. Files in /proc and /sys are the kernel's own interface. Tools such as ps, free, uptime and lsof are programs that read them and format the text.
Special file system (procfs and sysfs)
A special file system presents kernel data as files and directories, without storing anything on a disk. procfs (mounted at /proc) shows processes and system state. sysfs (mounted at /sys) shows devices, drivers and kernel objects.
Example: reading /proc/loadavg makes the kernel format the current load averages as one line of text.
Check yourself:
1. Why does a file in /proc have size 0?
2. Which file system shows devices and drivers?
3. How can you change a kernel setting through a file?