Memory Management MCQs for NSCT
Memory management is how the operating system shares main memory between processes. It maps each process's logical addresses to physical ones, keeps processes from touching each other's memory, and uses virtual memory to run programs larger than RAM. It is topic 8 of the Operating Systems area, which carries 5% of the NSCT according to HEC's Areas of Competencies. OS was the weakest area in 2026, with candidates averaging 33.8% accuracy, per the HEC NSCT report and APP.
Most marks here come from numericals, so this page follows Silberschatz's Operating System Concepts and gives a number for every idea.
Contiguous allocation and fragmentation
In the simplest scheme each process gets one contiguous block, guarded by a base register and a limit register. A free block is called a hole, and the OS chooses one for each request using one of three strategies:
| Strategy | Picks | Note |
|---|---|---|
| First fit | First hole big enough | Fast, and usually as good as best fit |
| Best fit | Smallest hole big enough | Leaves tiny leftover holes |
| Worst fit | Largest hole | Leaves big leftovers, but usually performs worst |
For example, with holes of 250, 600, 200, 750 and 500 KB, a 340 KB request goes to 600 under first fit, 500 under best fit and 750 under worst fit. External fragmentation means enough free memory exists in total, but not in one piece. Internal fragmentation is unused space inside an allocated block. Compaction can cure external fragmentation, but only if relocation happens at run time.
Paging
Paging splits logical memory into fixed-size pages and physical memory into frames of the same size. A page table maps each page to a frame. An address splits into a page number p and an offset d: with a 2^n-byte page, d is the low n bits.
Example: pages are 1,024 bytes, and page 3 is in frame 7. Logical address 3085 gives p = 3085 div 1024 = 3 and d = 13, so the physical address is 7 × 1024 + 13 = 7181.
Paging removes external fragmentation but leaves internal fragmentation in each process's last page. Every access would need two memory references (one for the page table, one for the data), so a TLB (translation look-aside buffer) caches recent mappings:
EAT = h × (TLB + mem) + (1 − h) × (TLB + 2 × mem)
With a 20 ns TLB, 100 ns memory and a 90% hit ratio: 0.9 × 120 + 0.1 × 220 = 130 ns. Textbooks differ here: Silberschatz's 10th edition ignores TLB time, which gives 110 ns for the same figures. So always use the convention the question states.
Large address spaces use hierarchical paging. On a 32-bit system with 4 KB pages and 4-byte entries, a single-level table is 4 MB, so it is split 10 / 10 / 12. Inverted page tables keep one entry per frame instead. Exam angle: in hex, with 4 KB pages, the last three hex digits are the offset, so 0x2ABC with page 2 in frame 5 becomes 0x5ABC. See paging and address translation for more drills.
Segmentation
Segmentation divides a program into variable-sized logical units, such as code, data, stack and heap. An address is (segment, offset), and the segment table gives each segment a base and a limit. If the offset is less than the limit, the physical address is base + offset; otherwise the hardware traps.
Example: segment 2 has base 4300 and limit 400. (2, 53) maps to 4353, but (2, 450) traps.
| Paging | Segmentation | |
|---|---|---|
| Unit size | Fixed | Variable |
| Programmer sees it? | No | Yes |
| Fragmentation | Internal | External |
Exam angle: the trap is checked against the limit before the base is added. Never add base + offset first.
Virtual memory
Virtual memory separates logical memory from physical memory, so a process can run with only part of itself loaded. Under demand paging, a page is loaded only when it is first used. A reference to a page marked invalid causes a page fault. The OS then finds a frame, reads the page from disk, updates the table and restarts the instruction.
EAT = (1 − p) × memory access + p × fault service time
With 100 ns memory, a 10 ms fault time and p = 0.0001, the EAT is about 1,100 ns, eleven times slower. Copy-on-write lets a parent and child share pages after fork() until one of them writes. Thrashing happens when processes have fewer frames than their working set, so they spend more time paging than computing. The fix is to run fewer processes, not more.
Page replacement
When no frame is free, a victim page must go:
- FIFO evicts the oldest page.
- LRU evicts the page least recently used.
- Optimal evicts the page whose next use is farthest away. It is the lowest possible fault count, but it needs to know the future.
For 1, 3, 0, 3, 5, 6, 3, 4, 1, 0, 3, 6 with 3 frames, FIFO gives 11 faults, LRU 10 and Optimal 8. Belady's anomaly means more frames can cause more faults under FIFO. On 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5, FIFO gives 9 faults with 3 frames but 10 with 4. LRU and Optimal are stack algorithms, so this cannot happen to them. Drill these on the page replacement page.
Common traps
- Mixing up the fragmentation types: paging gives internal fragmentation, while segmentation and contiguous allocation give external.
- Forgetting to add the offset after swapping in the frame number.
- Treating 10 ms as 10,000 ns. It is 10,000,000 ns.
- Assuming LRU always beats FIFO. It usually does, but not on every reference string.
- Claiming LRU shows Belady's anomaly. Only non-stack algorithms like FIFO do.
- Answering thrashing with "add more processes".
Worked examples
Understanding. Which kind of fragmentation does pure segmentation suffer from? Answer: external. Segments vary in size, so free memory breaks into holes between them. Nothing is wasted inside a segment, because each one is allocated exactly its own length.
Application. A machine has 16-bit addresses and 512-byte pages, and page 9 is in frame 3. What is the physical address of 0x1234? Answer: 1588 (0x634). 0x1234 = 4660. Page = 4660 div 512 = 9 and offset = 52. Physical address = 3 × 512 + 52 = 1588.
Analysis. A system uses two-level paging, with a 10 ns TLB, 100 ns memory and a 95% hit ratio. TLB time counts on both hits and misses. What is the EAT? Answer: 120 ns. A hit costs 10 + 100 = 110 ns. A miss costs 10 + 3 × 100 = 310 ns (two table levels plus the data). EAT = 0.95 × 110 + 0.05 × 310 = 104.5 + 15.5 = 120 ns. A high hit ratio is what makes multi-level tables affordable.
Practise
The quiz below has 100 Memory Management questions, which you can filter by tier and difficulty.
Practise: 100 questions
FAQs
What is the difference between a logical and a physical address?
A logical (virtual) address is generated by the CPU while a program runs. It is what the program sees. A physical address is the actual location in RAM. The MMU translates one to the other at run time, using a relocation register, page table or segment table. Under compile-time or load-time binding the two are the same.
Why is LRU hard to implement exactly?
True LRU must record the order of every memory reference. Hardware would need either a timestamp on each page-table entry or a stack updated on every access, both of which are costly. Real systems approximate it with reference bits, for example the second-chance (clock) algorithm. Clock behaves like FIFO when every reference bit is set.
What is the working set of a process?
It is the set of pages the process referenced in its most recent Δ (delta) memory references, a window of fixed size. It approximates the current locality. If the total working-set size of all processes exceeds the available frames, thrashing follows, so the OS should suspend a process until there is room.
Is a bigger page size always better?
No. Bigger pages mean smaller page tables, fewer TLB misses and more efficient disk I/O. But they waste more space to internal fragmentation (about half a page per process on average) and may load data that is never used. Page size is a trade-off, and modern systems often support several sizes.
Next step
Continue with File System Management MCQs, go back to the Operating Systems hub, or try a timed NSCT mock test.