25-Comp-A5 Operating Systems · May 2013
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated
Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.
Given. Page reference string (17 references): 51, 52, 53, 54, 55, 53, 54, 51, 56, 57, 58, 57, 58, 59, 57, 58, 59; three page frames; demand paging (a page is loaded only on first reference/fault); all three frames start empty.
Find. (i) The page-fault count under LRU. (ii) The minimum possible page-fault count for any replacement algorithm with 3 frames (Belady's optimal / MIN algorithm).
Approach. Step through the reference string maintaining the 3-frame contents; for LRU evict the frame least recently referenced; for the optimal bound evict the frame whose page is referenced farthest in the future (or never again) — Belady proved this minimizes faults for any fixed frame allocation.
| Ref # | Page | Hit/Fault | Frames after |
|---|---|---|---|
| 1 | 51 | Fault | 51 |
| 2 | 52 | Fault | 51, 52 |
| 3 | 53 | Fault | 51, 52, 53 |
| 4 | 54 | Fault (evict 51) | 54, 52, 53 |
| 5 | 55 | Fault (evict 52) | 54, 55, 53 |
| 6 | 53 | Hit | 54, 55, 53 |
| 7 | 54 | Hit | 54, 55, 53 |
| 8 | 51 | Fault (evict 55) | 54, 51, 53 |
| 9 | 56 | Fault (evict 53) | 54, 51, 56 |
| 10 | 57 | Fault (evict 54) | 57, 51, 56 |
| 11 | 58 | Fault (evict 51) | 57, 58, 56 |
| 12 | 57 | Hit | 57, 58, 56 |
| 13 | 58 | Hit | 57, 58, 56 |
| 14 | 59 | Fault (evict 56) | 57, 58, 59 |
| 15 | 57 | Hit | 57, 58, 59 |
| 16 | 58 | Hit | 57, 58, 59 |
| 17 | 59 | Hit | 57, 58, 59 |
| Algorithm | Page faults (3 frames) |
|---|---|
| LRU | 10 |
| Optimal (Belady/MIN) — the minimum achievable | 10 |
(b) Thrashing occurs when the CPU spends more time servicing page faults (swapping pages in and out) than doing useful computation, because the set of pages a process is actively using (its working set) does not fit in the frames currently allocated to it. Example: suppose the operating system, seeing low CPU utilization, responds by admitting more processes to increase multiprogramming; each new process needs its own working set resident, so the total frames demanded across all processes now exceeds physical memory. Every process starts faulting constantly, each fault forces a slow disk I/O, CPU utilization drops further (because processes are blocked waiting on page-in), and the OS's own low-utilization response is to admit still more processes — a positive-feedback collapse. Detecting thrashing is typically done by monitoring the page-fault rate (or CPU utilization) per process or system-wide: a fault rate rising sharply while CPU utilization simultaneously falls is the signature of thrashing (as opposed to a fault rate rising because a process's actual demand grew, where utilization would stay healthy). Controlling it is done via the working-set model (track each process's actual referenced-page set over a trailing window $\Delta$ and only allocate a process CPU time if its whole working set can be resident) or via local/priority replacement combined with load control (reduce the degree of multiprogramming — suspend or swap out one or more processes entirely when a system-wide fault-rate threshold is exceeded, freeing their frames for the remaining processes).
(c) External fragmentation is the accumulation of free memory as many small, non-contiguous holes scattered throughout memory, such that the total free space may be large but no single hole is big enough to satisfy a new request — the fragmentation is "external" to any allocated block (as opposed to internal fragmentation, which is wasted space inside an allocated block). It arises naturally under variable-partition (contiguous) allocation as processes of different sizes are repeatedly loaded and removed, leaving irregular gaps. One standard control method is compaction: periodically relocate the resident processes so that all free memory is coalesced into one contiguous block (typically requiring relocatable code, e.g. via a base/relocation register, since every process's addresses shift). A more common modern alternative that avoids compaction's cost is paging itself — by allocating memory in fixed-size frames rather than variable contiguous partitions, external fragmentation is eliminated entirely (replaced by, at most, internal fragmentation of at most one page per process).