Memory Management, Virtual Memory and Bottlenecks
IB Computer Science · Resource Management (HL)
Virtual memory and paging. Programs are written as if a large continuous memory space were available, but physical memory is smaller. The operating system divides the virtual space into fixed-size pages, keeps only the pages currently in use in physical memory, and holds the rest on disk. When a program accesses a page that is not resident, a page fault occurs: the operating system loads that page from disk and, to make room, writes another page out. This allows a program larger than physical memory to run, and allows more programs to be resident at once than would otherwise fit.
Thrashing. Adding more concurrent programs raises throughput at first, because the processor can work for one program while another waits for the disk. But each additional program takes physical memory away from the others, so each holds fewer pages. Beyond a threshold, no program holds enough pages to do useful work: a page loaded is immediately evicted to make room for the next, then needed again straight away. The system enters thrashing, in which almost all time is spent moving pages and almost none doing work. The diagnostic signature is important: the processor appears busy while very little is completed, and disk activity is extremely high. The correct cure is more memory or fewer concurrent programs; a faster processor changes nothing, because the bottleneck is the disk.
Polling and interrupts. A processor can learn that a device needs attention in two ways. Polling means repeatedly checking each device: simple and predictable, but it wastes processor time when nothing has happened and it responds late if the interval is long. An interrupt means the device signals the processor, which suspends what it is doing, saves its state, runs a handler and resumes: no time is wasted checking, and response is immediate, but the mechanism is more complex and interrupts must be prioritised. Almost all modern input and output uses interrupts.
Buffering and caching. A buffer is memory that holds data between a fast producer and a slow consumer, so that neither has to wait for the other continuously. Disk caching keeps recently used disk blocks in memory, exploiting the same locality of reference that makes processor cache work.
Dependencies and bottlenecks. Resources are interdependent, so the resource that limits performance — the bottleneck — determines what the whole system can achieve. Increasing capacity anywhere except at the bottleneck produces no improvement at all. Diagnosing correctly therefore means identifying which resource is saturated, not upgrading whichever component is easiest to replace.
Worked example: Diagnosing a slow machine without guessing
A server becomes very slow at peak hours. Monitoring data show processor utilization is 96%, disk read/write activity is extremely high, the page-fault rate per second is very large, and memory utilization is 99%. A technician proposes replacing the processor with a more powerful one. Diagnose and rebut that proposal.
Solution
Step 1 — recognize that the 96% figure is misleading. High processor utilization at first glance seems to point straight at the processor. But this metric only measures whether the processor is busy, not what it is busy doing. In continuous thrashing, most of the busy time is spent handling page faults, managing page tables and waiting for the disk — that is, busy without completing the users' work.
Step 2 — read the other three metrics together. Memory at 99% plus a very large page-fault rate plus extremely high disk activity is the characteristic triple sign of continuous thrashing. None of these three signs would appear if the problem were really a lack of processing capacity: a machine short of processor would have high processor utilization BUT low disk activity and a low page-fault rate.
Step 3 — state the mechanism. Physical memory is not enough for the set of pages actually in use by all the running programs. Each page just loaded is pushed out immediately to make room for the next page, and then is needed again right afterward, so the system spends almost all its time moving pages between memory and disk. Because the disk is thousands of times slower than memory, each such transfer takes time equal to millions of processing cycles.
Step 4 — rebut the proposal to replace the processor. The bottleneck is in the memory capacity and the disk speed, not in the processor. Adding capacity somewhere that is not the bottleneck improves nothing: a processor twice as fast will only wait for the disk twice as fast, and the number of jobs completed per second is almost unchanged. That expenditure is therefore wasted, and worse, it leaves the real problem unaddressed.
Step 5 — three correct measures, ranked by effectiveness per dollar. First and cheapest is to add memory: it acts directly on the cause, allowing enough pages of each program to be kept so the page-fault rate drops sharply and the system leaves the collapse zone. Second is to reduce the number of programs running concurrently at peak hours — move batch tasks to night, limit the number of processes serving at once — because the shape of the performance graph shows that running fewer programs completes more work once the system is to the right of the peak. Third is to replace the mechanical disk with a solid-state drive: it does not remove the cause but sharply reduces the cost of each page fault, so it pushes back the collapse threshold.
The principle to take away for answering every diagnosis question. Do not read each metric in isolation. Ask which resource is saturated, and remember that high processor utilization can be a symptom of a bottleneck elsewhere rather than the cause.
Worked example: Polling or interrupts: choose by the nature of the event
Choose polling or interrupts for each situation and explain: (a) the keyboard of a personal computer; (b) a temperature sensor in an industrial furnace that must be read exactly every 100 milliseconds; (c) the emergency stop button on a production line.
Solution
Selection criteria. Two questions decide it. Does the event happen often or rarely, and can the time it happens be predicted. Rare and unpredictable events use interrupts; regular events with a known timing make polling reasonable.
(a) Keyboard — use interrupts. A keypress is both rare relative to processor speed — the gap between two presses equals hundreds of millions of cycles — and completely unpredictable. With polling, the processor would have to check the keyboard thousands of times per second and almost every time find nothing, wasting almost all of the checks. With interrupts, the processor spends not one cycle on the keyboard until the moment a key is pressed, and the reaction is instant so the user does not perceive any delay.
(b) Temperature sensor read every 100 milliseconds — use polling. This is a case where polling is truly right, and it is often chosen wrongly. The event here is regular with a known timing: the system actively decides when it needs a temperature value, not the sensor. Using interrupts in this situation brings no benefit and adds complexity, and if the sensor raised interrupts faster than needed it would cause unnecessary load. Scheduled polling also gives predictable timing behavior, an important requirement in real-time control systems.
(c) Emergency stop button — use interrupts, and it must be the highest-priority interrupt. The event is extremely rare, completely unpredictable, and has the strictest latency requirement of the three situations. If polling with a 100-millisecond period were used, in the worst case the machine would react 100 milliseconds late — for a moving production line that may be the gap between safety and an accident. An interrupt gives an almost instant reaction. A detail worth adding: this interrupt must be assigned the highest priority so that it can preempt even other interrupt handlers that are running, and it is usually also supplemented by a hardware stop circuit that works independently of the software, because human safety should not be placed in the hands of a program that can hang.
The general sentence to write in the answer. Interrupts are efficient when the event is rare and unpredictable; polling is reasonable when the event is regular and initiated by the system itself, or when predictable timing behavior is needed.
Common mistake
Trap 1 — reading high processor utilization as "lack of processing capacity". In continuous thrashing, the processor is busy but busy waiting for the disk. You must read the triple sign together: memory nearly full, high page-fault rate, high disk activity.Trap 2 — upgrading a component that is not the bottleneck. Adding capacity somewhere that is not the bottleneck improves nothing. Always identify the saturated resource before proposing a solution.
Trap 3 — concluding that interrupts are always better than polling. Polling is reasonable when the event is regular and initiated by the system, or when predictable timing behavior is needed in a control system.