skip to content

questions

5

On Linux, `ls -l /proc/meminfo` reports a size of 0 bytes, yet reading the file returns pages of text. What kind of filesystem is /proc, and where does that content come from?

level: juniorimportance: must knowfreq 70%

answer

  1. no blocks on any disk
  2. content produced at read time
  3. size field cannot be known in advance
  4. a kernel interface dressed as files
  5. filesystem type proc in /proc/mounts

basics

~20 s

/proc is procfs, a virtual filesystem with no storage behind it. The kernel generates each file's contents at the moment a process reads it, so the reported size is 0 while the data returned is a live snapshot of kernel state.

solid answer

~40 s

/proc is a mount of the `proc` filesystem type — a kernel interface dressed up as files, not data on a disk. Nothing under /proc occupies blocks anywhere; when you `open()` and `read()` /proc/meminfo, the kernel runs a function that formats the current memory counters into a buffer and hands them back. Because the content does not exist until you ask for it, the kernel cannot report a meaningful length in `stat()`, so most procfs files advertise size 0. The practical consequences are that every read is a fresh snapshot rather than a cached value, that `du`, `df` and backups are meaningless there, and that a few files under /proc are writable — writing to them asks the kernel to change a setting rather than storing bytes.

code

bash · 4 lines
bash
stat -c '%s bytes: %n' /proc/meminfo
wc -c < /proc/meminfo
grep ' /proc ' /proc/mounts
stat -c '%s bytes: %n' /sys/class/net/lo/mtu

go deeper

for a junior

Be able to say plainly that /proc is a virtual filesystem generated by the kernel, that nothing there is on disk, and that the 0-byte size does not mean the file is empty.

for a middle

Explain the mechanics: each read invokes kernel code that formats current state, so st_size cannot be known ahead of time, and /proc/sys is the writable subtree where a write changes a kernel variable rather than storing bytes.

for a senior

Show you know the operational consequences — reads are snapshots with real kernel cost, entries can vanish mid-scrape, backups and du are meaningless there, and a missing /proc mount silently breaks tooling inside containers and chroots.

for a principal

Frame it as an interface-design choice: exposing kernel state as unversioned text files gave every language free access but created an ABI nobody can change, which is why newer state lands in sysfs, netlink or eBPF instead.

## The idea: files as a kernel API Unix's design instinct is "everything is a file", and /proc is the clearest expression of it. Rather than inventing a system call for every piece of information a user might want about the running kernel, Linux exposes that information through a filesystem whose files are computed on demand. Any program that can `open()`, `read()` and parse text can therefore inspect the kernel — no library, no special API, no privileges beyond ordinary file permissions. The filesystem type is literally named `proc`, and you can see the mount: ``` $ grep ' /proc ' /proc/mounts proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0 ``` It is mounted by the boot process (or by the container runtime, inside a container) and it is not a partition, not a disk image, and not a RAM disk such as tmpfs. tmpfs really does store bytes in memory; procfs stores nothing at all. ## Why the size is zero When you call `stat()` on an ordinary file, the kernel reads the length recorded in the inode. A procfs file has an inode, but there is no stored content whose length could be recorded — the text is produced by a kernel function at read time, and its length depends on the state of the machine at that instant. /proc/meminfo on a box with a lot of NUMA nodes is longer than on a laptop; /proc/<pid>/maps grows and shrinks as a process maps and unmaps memory. Rather than lie, the kernel reports `st_size` as 0 for most procfs files. This is why naive tools misbehave here. `du -sh /proc` reports nothing useful, a backup tool that trusts `st_size` may copy an empty file, and `cp /proc/meminfo backup.txt` works only because `cp` keeps reading until EOF rather than trusting the size. It is also why /proc never appears in `df` output as consuming disk. ``` $ stat -c '%s %n' /proc/meminfo 0 /proc/meminfo $ wc -c < /proc/meminfo 1543 ``` A useful contrast is sysfs: attribute files under /sys typically report a size of 4096 (one page), because the sysfs convention is that a single attribute fits in one page buffer. Same trick, different convention. ## What lives there Procfs holds two broad families of entries. The numeric directories — /proc/1, /proc/4711 — are one per running process, holding that process's command line, memory maps, open file descriptors, limits and status. /proc/self is a magic symlink that always resolves to the directory of whichever process is reading it, which is how a program inspects itself without knowing its own PID. The non-numeric entries are system-wide: /proc/meminfo, /proc/cpuinfo, /proc/loadavg, /proc/uptime, /proc/stat, /proc/mounts, /proc/cmdline (the kernel command line the machine booted with), /proc/interrupts. All of them are formatted fresh on each read. ## Reading versus writing Most of /proc is read-only: the kernel is reporting, not accepting input. The important exception is the /proc/sys subtree, which mirrors the kernel's tunable parameters. There, writing a value to a file changes kernel behaviour immediately: ``` # echo 1 > /proc/sys/net/ipv4/ip_forward ``` Nothing is stored in a file by that write — the kernel parses the bytes and flips an internal variable, which is why the change is lost at the next reboot. A handful of per-process files are also writable, such as /proc/<pid>/oom_score_adj. ## Snapshot semantics Because each read runs kernel code, every read is a fresh sample rather than a cached one. That is exactly what you want for monitoring, but it has two consequences worth internalising. First, the cost of reading is paid in the kernel, and it is not always trivial — files that must walk large internal structures are genuinely expensive. Second, there is no stability guarantee across reads: a process directory can disappear between the moment you list /proc and the moment you open a file inside it, and two successive reads of the same file legitimately return different numbers. ## What an interviewer is checking They want to hear that /proc is a kernel interface, not storage; that the zero size follows from content being generated on demand; and that /proc/sys is the writable corner where writes become kernel configuration. Candidates who describe /proc as "a folder where Linux keeps process information on disk" have the mental model backwards, and that mistake tends to resurface later when they try to persist a sysctl by editing a file under /proc.

  • If /proc holds no data, what happens inside a container that is started without /proc mounted?
    Anything that inspects the system breaks. `ps` and `top` enumerate /proc's numeric directories, so they report nothing; language runtimes that read /proc/self/... for their own memory maps or CPU count fail or fall back to wrong defaults; and /proc/sys is unavailable, so tunables cannot be read at all. Container runtimes therefore mount a fresh procfs in the container's own PID namespace as part of standard setup.
  • Why can `cat /proc/meminfo` succeed while a backup tool that trusts st_size copies an empty file?
    `cat` ignores the reported size and simply reads until it gets EOF, so it receives whatever the kernel generates. A tool that calls `stat()` first and allocates or copies exactly `st_size` bytes copies zero bytes, because procfs advertises 0. This is why /proc is excluded from backups by convention rather than being merely useless in them.
  • What is /proc/self, and why is it useful?
    /proc/self is a symlink the kernel resolves per-reader: it always points at the /proc/<pid> directory of the process doing the lookup. A program can read /proc/self/maps, /proc/self/limits or /proc/self/cmdline without discovering its own PID first, and the same path in two processes yields two different directories.

/proc is a window, not a warehouse: it looks like a room full of documents, but each page is printed the instant you ask for it and nothing is filed away behind the glass.

saying these in an interview costs you the question

  • Says /proc is stored on the root filesystem or is a tmpfs in RAM
  • Reads the 0-byte size as meaning the file is empty
  • Thinks /proc can be backed up or copied to preserve system state
  • Confuses /proc with /dev and expects device nodes there
  • Believes editing a file under /proc persists across a reboot

context

open as a page

On a Linux host, what is the relationship between the `sysctl` command and the /proc/sys directory, and why does a value set with `sysctl -w` disappear after a reboot?

level: middleimportance: must knowfreq 66%

basics

~20 s

sysctl is a thin wrapper over /proc/sys: each dotted key maps to a path, so net.ipv4.ip_forward is /proc/sys/net/ipv4/ip_forward. Writes there change a live kernel variable and store nothing, so persistence requires a config file under /etc/sysctl.d.

open as a page

You are writing a metrics collector that reads Linux /proc files directly instead of shelling out to tools. Which properties of procfs, as opposed to an ordinary file on disk, does the code have to handle?

level: seniorimportance: should knowfreq 34%

basics

~20 s

Procfs files are generated per read, so each open costs kernel work, values are instantaneous samples, most counters are cumulative since boot and need two samples to become rates, process directories vanish mid-scrape, PIDs are reused, and some entries are far more expensive to read than they look.

open as a page

A long-running daemon on a Linux host cannot be restarted, and you need to know which config file it opened, what open-file limit it is actually running under, and whether it is leaking descriptors. How does /proc/<pid> answer all three?

level: seniorimportance: should knowfreq 52%

basics

~20 s

The kernel publishes each process's live state under /proc/<pid>: fd/ holds a symlink per open descriptor, limits shows that process's own soft and hard rlimits, and counting fd/ entries against the limit reveals a descriptor leak. All three are read without touching the process.

open as a page

In Linux sysfs, a network interface is reachable both as /sys/class/net/<name> and as a path somewhere under /sys/devices. How is sysfs organised, and which of those two locations is the canonical one?

level: middleimportance: nice to knowfreq 28%

basics

~20 s

/sys/devices is the canonical tree, laid out by how devices physically hang off buses. /sys/class, /sys/block and /sys/bus are alternative views made of symlinks into it, grouping the same objects by function, and each leaf file is one attribute of one kernel object.

open as a page