Page Fault — Demand Paging and Protection
A page fault occurs when an address translation or access cannot proceed using the current page-table state. A fault can be normal: the kernel may allocate an anonymous page on first write, resolve a copy-on-write mapping or locate file data. A protection violation or invalid mapping may instead cause a signal such as SIGSEGV; not every fault is an error. Minor faults are serviced without the I/O associated with a major fault; major-fault accounting does not directly measure latency or a particular disk. Track deltas rather than cumulative totals and account for interpreter/runtime activity. RUSAGE_SELF aggregates all threads in the process, whereas Linux RUSAGE_THREAD measures only the calling thread. The bounded example creates a private anonymous mapping of 64 pages, writes one byte per page, checks that the process minor-fault counter increased and closes the mapping. It does not force major faults, induce crashes or establish an exact fault count. Example prerequisites: Linux, Bash and Python 3 standard library; run as a non-root user in a private temporary working directory. Example scope: native userspace observation. Expected output: first writes increased minor-fault counter. No system configuration changes.
Formal Syntax & Structure
getrusage(RUSAGE_SELF): ru_minflt, ru_majfltParameters & Attributes
| Name | Type | Description |
|---|---|---|
| ru_minflt | counter | Minor faults accounted to the process. |
| ru_majflt | counter | Major faults accounted to the process. |
| page size | bytes | Use system-provided page size, not a hardcoded constant. |
Return Value
getrusage reports cumulative counters; output normalizes a positive minor-fault delta.
Working Example
python3 - <<'PY'import mmap, resourcepage=mmap.PAGESIZEwith mmap.mmap(-1,64*page,flags=mmap.MAP_PRIVATE|mmap.MAP_ANONYMOUS) as area: before=resource.getrusage(resource.RUSAGE_SELF) for offset in range(0,len(area),page): area[offset]=1 after=resource.getrusage(resource.RUSAGE_SELF) assert after.ru_minflt>before.ru_minflt assert after.ru_majflt>=before.ru_majfltprint('first writes increased minor-fault counter')PY