Sub topic 13
Frame Allocation and Replacement Scope
This chapter assumes you can trace FIFO, LRU and Optimal replacement on a reference string. It also assumes you know what a page fault is and how demand paging works. Earlier chapters covered which page to evict. This chapter covers two other questions. How many frames does each process get? And may a faulting process evict a frame that belongs to another process?
The Problem
Physical memory is divided into a fixed number of frames. Several processes compete for them. If the OS gives a process too few frames, it faults constantly. If it gives one process too many, other processes starve. The OS therefore needs two policies:
- Allocation policy: how many frames each process receives.
- Replacement scope: whether a victim page comes only from the faulting process (local) or from any process (global).
Limits on Allocation
A process cannot get an arbitrary number of frames. Two bounds apply.
- Maximum: the total number of frames available to user processes.
- Minimum: set by the hardware instruction set. When an instruction faults, it must be restarted, and every page it touches must be resident at once.
Consider an instruction that has an operand with one level of indirect addressing. It needs at least three frames at the same time:
- The page containing the instruction.
- The page containing the pointer.
- The page containing the final data.
If the machine allows multiple levels of indirection, the minimum rises. In the worst case it approaches the whole address space, so architectures usually cap the indirection depth.
Allocation Algorithms
Let m be the number of free frames, n the number of processes, and sᵢ the size (in pages) of process i. Let S be the sum of all sᵢ.
Equal Allocation
Every process gets ⌊m / n⌋ frames. The leftover frames can go into a free-frame pool. It is simple, but it ignores size. A 10-page process and a 500-page process get the same share, so the small one wastes frames and the large one thrashes.
Proportional Allocation
Process i gets
aᵢ = (sᵢ / S) × m
Round down, then make sure every process still meets its minimum. In this scheme, bigger processes get more frames.
Example: m = 64 frames, with processes of 10, 30 and 60 pages. Here S = 100.
| Process | Size sᵢ | Exact share | Allocated (floor) |
|---|---|---|---|
| P1 | 10 | 6.4 | 6 |
| P2 | 30 | 19.2 | 19 |
| P3 | 60 | 38.4 | 38 |
That uses 63 frames. The one remaining frame stays in the free pool or goes to the process that lost the most to rounding.
def proportional(total_frames, sizes, minimum=2):
"""Floor-based proportional allocation with a per-process minimum."""
s = sum(sizes)
alloc = [max(minimum, total_frames * size // s) for size in sizes]
return alloc, total_frames - sum(alloc)
alloc, spare = proportional(64, [10, 30, 60])
print(alloc, spare) # [6, 19, 38] 1
alloc, spare = proportional(62, [10, 127])
print(alloc, spare) # [4, 57] 1The second call is the classic textbook case. A 10-page process and a 127-page process share 62 frames, and the exact shares are about 4.5 and 57.5.
Priority Allocation
Proportional allocation can use priority instead of size, or a mix of both. A common variant: when a high-priority process faults, it may take a frame from a lower-priority process. This is a global-replacement idea, covered next.
Replacement Scope
Local Replacement
A faulting process picks its victim only from its own frames. Its allocation stays fixed unless the OS changes it explicitly.
- Each process's fault rate depends only on its own behavior and its own allocation.
- A misbehaving process cannot take frames from others.
- A process that needs more frames cannot get them even if other processes hold idle frames, so memory can be wasted.
Global Replacement
A faulting process may pick any frame in memory as the victim, including frames of other processes.
- Better overall throughput, because frames flow toward the processes that need them.
- A process's fault rate depends on other processes, so the same program can run at very different speeds from run to run.
- A process that faults heavily can steal frames from others and push them into faulting too.
Comparison
| Property | Local | Global |
|---|---|---|
| Victim chosen from | Faulting process's frames | All frames |
| Frames per process | Fixed by allocation policy | Varies over time |
| Performance predictability | High | Low |
| Memory utilization | Can waste frames | Better |
| Thrashing spread | Contained to one process | Can spread to others |
| Typical use | Allocation-controlled systems | General-purpose systems |
Many general-purpose systems use global replacement, which does not remove the need for allocation policy. The OS typically keeps a pool of free frames and starts reclaiming pages when the pool falls below a low threshold. It stops when the pool rises above a high threshold. This way a fault usually finds a free frame immediately, and the page replacement work happens in the background.
Interaction With Replacement Algorithms
The replacement algorithm and the scope are independent choices. LRU with local scope runs LRU separately inside each process's frame set. LRU with global scope runs it over the combined set of frames.
This matters for tracing problems:
- Under local replacement, you trace each process separately with its own frame count. Another process's references do not affect it.
- Under global replacement, you must interleave the references in the stated order and use one shared frame set. The result for one process depends on the others.
Fixed local allocation is also where Belady's anomaly shows up for FIFO. Adding frames to one process's fixed share can raise that process's fault count.
Other Considerations
- Non-uniform memory access (NUMA): on multiprocessor machines, memory near a CPU is faster to reach. The OS should prefer to allocate frames close to the CPU where the process runs.
- Shared pages: a shared frame counts once toward physical memory but may be mapped by several processes. Allocation policies must decide who is charged for it.
- Dynamic adjustment: instead of fixing allocation forever, the OS can monitor each process's page-fault rate. If the rate is above an upper bound, give it more frames. If it is below a lower bound, take frames away. This idea is the page-fault-frequency approach, and it connects directly to the working-set model.
Worked Example: Global vs Local
Suppose 4 frames are shared by processes A and B. Under local replacement with equal allocation, each gets 2 frames. If A's working set is 3 pages and B's is 1 page, A faults repeatedly in its 2 frames while one of B's frames sits idle. Under global replacement, A's faults can claim B's idle frame, so A runs with 3 frames and B keeps the 1 it needs. Global replacement uses memory better here. If B suddenly needs 3 pages, though, B's faults take frames back from A, and both processes now thrash in 4 frames.
Exercise
A system has 100 free frames and two processes of 25 and 75 pages. (a) How many frames does each get under equal allocation and under proportional allocation? (b) Which scope would you pick if you need each process's runtime to be predictable, and why?
Show answer
(a) Equal allocation gives 50 and 50. Proportional allocation gives 25/100 × 100 = 25 frames and 75/100 × 100 = 75 frames. (b) Local replacement. Each process's fault rate then depends only on its own allocation and behavior, so other processes cannot take its frames and change its runtime.
Exercise
Give two reasons why a process needs a minimum number of frames, and say what kind of system property sets this minimum.
Show answer
First, an instruction that faults must be restarted, and all the pages it references (instruction, operands, indirect pointers) must be resident at the same time, so fewer frames than that would make progress impossible. Second, with fewer than the minimum, the process would fault on almost every instruction and spend nearly all its time in page-fault handling. The minimum is set by the computer architecture, specifically the instruction set and its addressing modes.