Operating Systems · Unit 4 · 8 hrs
Memory Management
Exam-focused notes for Memory Management (Operating Systems, CSC264): what the TU syllabus asks and how it has actually been tested, with 18 solved past questions from this unit.
Memory Management in short
This chapter covers memory management: how the operating system shares a limited amount of physical memory among many competing processes, from the simplest single-program layout through partitioned multiprogramming, virtual memory with paging, and segmentation.
What this unit covers
- Introduction
- Monoprogramming vs. Multi-programming
- Modelling Multiprogramming
- Multiprogramming with fixed and variable partitions
- Relocation and Protection
- Memory management (Bitmaps & Linked-list)
- Memory Allocation Strategies
- Virtual memory: Paging, Page Table, Page Table Structure, Handling Page Faults, TLB's
- Page Replacement Algorithms: FIFO, Second Chance, LRU, Optimal, LFU, Clock, WS-Clock
- Concept of Locality of Reference
- Belady's Anomaly
- Segmentation: Need of Segmentation, its Drawbacks, Segmentation with Paging(MULTICS)
Segmentation
2081 · 10 marks
Explain the translation of logical address into physical address using segment table with necessary diagram. List advantages and disadvantages of segmentation.[10]
Memory Allocation Strategies
2081 · 5 marks
Consider a swapping system in which memory consists of the following hole sizes in memory order: 15 MB, 2 MB, 10 MB, 6 MB, 8 MB and 20 MB. Which hole is taken for successive segment requests of: (a) 10 MB (b) 10 MB For first fit, next fit and best fit. [5]
Show solved answerHide answer
Memory Allocation: First Fit, Next Fit, Best Fit
Given
Holes in memory order:
| Hole # | Size |
|---|---|
| 1 | 15 MB |
| 2 | 2 MB |
| 3 | 10 MB |
| 4 | 6 MB |
| 5 | 8 MB |
| 6 | 20 MB |
Successive requests: (a) 10 MB, (b) 10 MB.
Solution
First Fit
Allocates the first hole large enough, scanning from the start each time.
(a) 10 MB: Hole 1 (15 MB) $\ge 10$ → allocate. Hole 1 leftover $= 15 - 10 = 5$ MB.
(b) 10 MB: Scan from start: Hole 1 (5) ✗, Hole 2 (2) ✗, Hole 3 (10) $\ge 10$ → allocate. Leftover $= 0$ MB.
Result: (a) Hole 1, (b) Hole 3.
Next Fit
Like First Fit but resumes scanning from the position of the last allocation.
(a) 10 MB: Start at Hole 1 (15) → allocate. Pointer at Hole 1, leftover 5 MB.
(b) 10 MB: Resume from Hole 1: (5) ✗, Hole 2 (2) ✗, Hole 3 (10) → allocate. Pointer at Hole 3.
Result: (a) Hole 1, (b) Hole 3.
(Since scanning resumes at the last hole which is now too small, next fit gives the same result as first fit here.)
Best Fit
Allocates the smallest hole that is large enough.
(a) 10 MB: Candidates $\ge 10$: Hole 1 (15, leftover 5), Hole 3 (10, leftover 0), Hole 6 (20, leftover 10). Smallest sufficient = Hole 3 (exact fit). Leftover 0 MB.
(b) 10 MB: Remaining candidates $\ge 10$: Hole 1 (15, leftover 5), Hole 6 (20, leftover 10). Smallest = Hole 1. Leftover 5 MB.
Result: (a) Hole 3, (b) Hole 1.
Final Comparison
| Request | First Fit | Next Fit | Best Fit |
|---|---|---|---|
| (a) 10 MB | Hole 1 (15 MB) | Hole 1 (15 MB) | Hole 3 (10 MB) |
| (b) 10 MB | Hole 3 (10 MB) | Hole 3 (10 MB) | Hole 1 (15 MB) |
2078 · 5 marks
Differentiate between internal and external fragmentation? Suppose that we have memory of 100 KB with 5 partitions of size 150 KB, 200 KB, 250 KB, 100 KB, and 300 KB. Where the processes A and B of size 175 KB and 125 KB will be loaded, if we used Best-Fit, and Worst-Fit Strategy? [5]
Virtual memory
2081 · 5 marks
Write short notes on: a. Virtual Memory b. Race Condition [5]
Show solved answerHide answer
Short Notes: Virtual Memory and Race Condition
a. Virtual Memory (2.5 marks)
Virtual Memory is a memory management technique in which the operating system uses a portion of the hard disk as an extension of RAM, thereby increasing the effective size of usable memory beyond the physical RAM available.
Key Concepts:
- When the kernel needs memory for a new process but RAM is full, it writes the contents of a currently unused block of memory to the hard disk. This process is called swapping out.
- When the original contents are needed again, they are read back into memory (swapped in).
- The portion of the hard disk used for this purpose is called swap space.
Working Mechanism:
- A segment number is used to find the segment descriptor.
- A check is made whether the segment's page table is in memory; if not, a segment fault occurs.
- The page table entry for the requested virtual page is examined; if the page is not in memory, a page fault is triggered.
- If the page is in memory, the main memory address is extracted from the page table entry.
- The offset is added to the page origin to give the final main memory address, and the read/write takes place.
Advantages and Disadvantages:
| Advantage | Disadvantage |
|---|---|
| Programs larger than physical RAM can run | Disk access is slower than RAM, reducing speed |
| More processes can be accommodated | Excessive swapping (thrashing) degrades performance |
In Linux, swap space can be a swap partition (faster) or a swap file within the normal file system.
b. Race Condition (2.5 marks)
A Race Condition is a situation that occurs when two or more processes (or threads) read or write some shared data concurrently, and the final result depends on the precise order in which the processes execute.
Key Points:
- Race conditions typically occur inside a critical section, where multiple threads access shared resources.
- The result of execution differs according to the order in which threads or processes run, making the output unpredictable and incorrect.
Example Scenario:
- Two processes share a common variable (e.g., a counter).
- Process A reads the value, and before it writes back the updated value, Process B also reads the same old value.
- Both processes write their result, and one update is lost, producing an incorrect final value.
Illustration:
Shared variable: count = 5
Process A: reads count (5), computes count+1 = 6
Process B: reads count (5), computes count+1 = 6 <-- reads before A writes
Process A: writes count = 6
Process B: writes count = 6 <-- should be 7, but is 6 (incorrect!)
Prevention:
Race conditions can be prevented using synchronization mechanisms such as:
- Mutex locks
- Semaphores
- TSL (Test and Set Lock) instruction -- a shared lock variable is set to 1 before entering the critical section and reset to 0 after leaving, ensuring mutual exclusion.
In summary, race conditions arise due to uncontrolled concurrent access to shared data, and must be carefully managed to ensure correctness of programs.
2080 · 5 marks
When does a page fault occur? Give a structure of a page table. [5]
2080.1 · 5 marks
Why do we need virtual memory? Describe the structure of a page table. [5]
Belady's Anomaly
2080 · 5 marks
What does Belady's anomaly mean? What are the benefits of multiprogramming over uniprogramming? [5]
Page Replacement Algorithms
2079 · 5 marks
Consider the page references 7,0,1,2,0,3,0,4,2,3,0,3,2. Find the number of page fault using OPR and FIFO, with 4 page frame. [5]
2078 · 10 marks
Why OPR is best but not practically feasible page replacement algorithm? Calculate the number of page faults for OPR, LRU and Clock page replacement algorithm for the reference string: 1, 3, 4, 2, 3, 5, 4, 3, 1, 2, 4, 6, 3, 2, 1, 4, 2. Assume the memory size is 3.[10]
Show solved answerHide answer
OPR Page Replacement and Page Fault Calculations
Given Data
- Reference String: 1, 3, 4, 2, 3, 5, 4, 3, 1, 2, 4, 6, 3, 2, 1, 4, 2
- Number of Frames: 3
Why OPR is Best but Not Practically Feasible
Optimal Page Replacement (OPR) replaces the page that will not be used for the longest period of time in the future.
Why BEST:
- Yields the minimum possible page faults for a given string and frame count.
- Used as a benchmark to evaluate other algorithms.
- Free from Belady's anomaly.
Why NOT feasible:
- Requires future knowledge of the reference string.
- The OS cannot predict which pages a process will reference next.
- Hence impossible to implement; only a theoretical reference point.
1. Optimal (OPR)
Rule: replace the page whose next use is farthest in the future.
| Step | Ref | Frames | Fault/Hit |
|---|---|---|---|
| 1 | 1 | [1] | Fault |
| 2 | 3 | [1,3] | Fault |
| 3 | 4 | [1,3,4] | Fault |
| 4 | 2 | next: 1→9, 3→5, 4→7 → replace 1 → [2,3,4] | Fault |
| 5 | 3 | [2,3,4] | Hit |
| 6 | 5 | next: 2→10, 3→8, 4→7 → replace 2 → [5,3,4] | Fault |
| 7 | 4 | [5,3,4] | Hit |
| 8 | 3 | [5,3,4] | Hit |
| 9 | 1 | next: 5→never, 3→13, 4→11 → replace 5 → [1,3,4] | Fault |
| 10 | 2 | next: 1→15, 3→13, 4→11 → replace 1 → [2,3,4] | Fault |
| 11 | 4 | [2,3,4] | Hit |
| 12 | 6 | next: 2→14, 3→13, 4→16 → replace 4 → [2,3,6] | Fault |
| 13 | 3 | [2,3,6] | Hit |
| 14 | 2 | [2,3,6] | Hit |
| 15 | 1 | next: 2→17, 3→never, 6→never → replace 3 → [2,1,6] | Fault |
| 16 | 4 | next: 2→17, 1→never, 6→never → replace 6 → [2,1,4] | Fault |
| 17 | 2 | [2,1,4] | Hit |
OPR Page Faults = 10, Hits = 7
2. LRU (Least Recently Used)
Replace the page unused for the longest time.
| Step | Ref | Frames (after) | Fault/Hit | Recency (recent→old) |
|---|---|---|---|---|
| 1 | 1 | [1] | Fault | 1 |
| 2 | 3 | [1,3] | Fault | 3,1 |
| 3 | 4 | [1,3,4] | Fault | 4,3,1 |
| 4 | 2 | evict 1 → [2,3,4] | Fault | 2,4,3 |
| 5 | 3 | [2,3,4] | Hit | 3,2,4 |
| 6 | 5 | evict 4 → [2,3,5] | Fault | 5,3,2 |
| 7 | 4 | evict 2 → [4,3,5] | Fault | 4,5,3 |
| 8 | 3 | [4,3,5] | Hit | 3,4,5 |
| 9 | 1 | evict 5 → [4,3,1] | Fault | 1,3,4 |
| 10 | 2 | evict 4 → [2,3,1] | Fault | 2,1,3 |
| 11 | 4 | evict 3 → [2,4,1] | Fault | 4,2,1 |
| 12 | 6 | evict 1 → [2,4,6] | Fault | 6,4,2 |
| 13 | 3 | evict 2 → [3,4,6] | Fault | 3,6,4 |
| 14 | 2 | evict 4 → [3,2,6] | Fault | 2,3,6 |
| 15 | 1 | evict 6 → [3,2,1] | Fault | 1,2,3 |
| 16 | 4 | evict 3 → [4,2,1] | Fault | 4,1,2 |
| 17 | 2 | [4,2,1] | Hit | 2,4,1 |
Count faults: steps 1,2,3,4,6,7,9,10,11,12,13,14,15,16 = 14 faults, Hits = 3
LRU Page Faults = 14
3. Clock (Second-Chance)
Circular list of 3 frames with a reference bit; on fault the pointer advances, clearing set bits until it finds a bit=0 to replace. On a hit, the page's reference bit is set to 1.
Let frame slots be positions 0,1,2. Pointer starts at 0.
| Step | Ref | Action | Frames (bit) | Ptr after | F/H |
|---|---|---|---|---|---|
| 1 | 1 | load pos0 | 1(1) - - | 1 | Fault |
| 2 | 3 | load pos1 | 1(1) 3(1) - | 2 | Fault |
| 3 | 4 | load pos2 | 1(1) 3(1) 4(1) | 0 | Fault |
| 4 | 2 | ptr0: 1 bit1→0, ptr1:3→0, ptr2:4→0, ptr0:1 bit0 replace | 2(1) 3(0) 4(0) | 1 | Fault |
| 5 | 3 | hit, set 3→1 | 2(1) 3(1) 4(0) | 1 | Hit |
| 6 | 5 | ptr1:3 bit1→0, ptr2:4 bit0 replace | 2(1) 3(0) 5(1) | 0 | Fault |
| 7 | 4 | ptr0:2→0, ptr1:3 bit0 replace | 2(0) 4(1) 5(1) | 2 | Fault |
| 8 | 3 | ptr2:5→0, ptr0:2 bit0 replace | 3(1) 4(1) 5(0) | 1 | Fault |
| 9 | 1 | ptr1:4→0, ptr2:5 bit0 replace | 3(1) 4(0) 1(1) | 0 | Fault |
| 10 | 2 | ptr0:3→0, ptr1:4 bit0 replace | 3(0) 2(1) 1(1) | 2 | Fault |
| 11 | 4 | ptr2:1→0, ptr0:3 bit0 replace | 4(1) 2(1) 1(0) | 1 | Fault |
| 12 | 6 | ptr1:2→0, ptr2:1 bit0 replace | 4(1) 2(0) 6(1) | 0 | Fault |
| 13 | 3 | ptr0:4→0, ptr1:2 bit0 replace | 4(0) 3(1) 6(1) | 2 | Fault |
| 14 | 2 | ptr2:6→0, ptr0:4 bit0 replace | 2(1) 3(1) 6(0) | 1 | Fault |
| 15 | 1 | ptr1:3→0, ptr2:6 bit0 replace | 2(1) 3(0) 1(1) | 0 | Fault |
| 16 | 4 | ptr0:2→0, ptr1:3 bit0 replace | 2(0) 4(1) 1(1) | 2 | Fault |
| 17 | 2 | hit, set 2→1 | 2(1) 4(1) 1(1) | 2 | Hit |
Faults: all steps except 5 and 17 = 15 faults, Hits = 2
Clock Page Faults = 15
Summary
| Algorithm | Page Faults |
|---|---|
| OPR | 10 |
| LRU | 14 |
| Clock | 15 |
(OPR gives the fewest faults, confirming it as the optimal benchmark.)
2076 · 10 marks
How Second Chance page replacement algorithm differs from FIFO page replacement policy? Discuss the concept of Belady’s anomaly with suitable example.[10]
2080.1 · 5 marks
Find the number of page fault using FIFO and LRU for the reference string 4, 7, 6, 1, 7, 6, 1, 2, 7, 2 with frame size 3. [5]
Show solved answerHide answer
Page Fault Calculation: FIFO and LRU
Given Data
- Reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7, 2
- Number of frames: 3
1. FIFO (First In First Out)
Replace the oldest loaded page when frames are full.
| Ref | Frame contents (oldest→newest) | Fault? |
|---|---|---|
| 4 | 4 | Fault |
| 7 | 4, 7 | Fault |
| 6 | 4, 7, 6 | Fault |
| 1 | 7, 6, 1 (replace 4) | Fault |
| 7 | 7, 6, 1 | Hit |
| 6 | 7, 6, 1 | Hit |
| 1 | 7, 6, 1 | Hit |
| 2 | 6, 1, 2 (replace 7) | Fault |
| 7 | 1, 2, 7 (replace 6) | Fault |
| 2 | 1, 2, 7 | Hit |
FIFO Page Faults = 6, Hits = 4
2. LRU (Least Recently Used)
Replace the page not used for the longest time.
| Ref | Frames | Recency (MRU→LRU) | Fault? |
|---|---|---|---|
| 4 | 4 | 4 | Fault |
| 7 | 4, 7 | 7, 4 | Fault |
| 6 | 4, 7, 6 | 6, 7, 4 | Fault |
| 1 | 7, 6, 1 | 1, 6, 7 (replace 4, LRU) | Fault |
| 7 | 7, 6, 1 | 7, 1, 6 | Hit |
| 6 | 7, 6, 1 | 6, 7, 1 | Hit |
| 1 | 7, 6, 1 | 1, 6, 7 | Hit |
| 2 | 7, 1, 2 | 2, 1, 6→ replace 7 (LRU) | Fault |
Recheck LRU at ref = 2 (step 8).
After processing 4,7,6,1,7,6,1 the recency order is: 1 (MRU), 6, 7 (LRU). On fault for page 2, replace 7 (LRU).
Frames now: 1, 6, 2. Recency: 2, 1, 6.
| Ref | Frames | Recency (MRU→LRU) | Fault? |
|---|---|---|---|
| 2 | 6, 1, 2 (replace 7) | 2, 1, 6 | Fault |
| 7 | 6, 2, 7 (replace 1?) | recency before: 2,1,6 → replace 6 (LRU) | Fault |
Recheck step 9 (ref = 7): recency order is 2 (MRU), 1, 6 (LRU). Replace 6 (LRU).
Frames now: 1, 2, 7. Recency: 7, 2, 1.
| Ref | Frames | Recency (MRU→LRU) | Fault? |
|---|---|---|---|
| 7 | 1, 2, 7 (replace 6) | 7, 2, 1 | Fault |
| 2 | 1, 2, 7 | 2, 7, 1 | Hit |
Corrected LRU trace:
| Ref | Frames | Fault? |
|---|---|---|
| 4 | 4 | Fault |
| 7 | 4,7 | Fault |
| 6 | 4,7,6 | Fault |
| 1 | 7,6,1 (rep 4) | Fault |
| 7 | 7,6,1 | Hit |
| 6 | 7,6,1 | Hit |
| 1 | 7,6,1 | Hit |
| 2 | 6,1,2 (rep 7) | Fault |
| 7 | 1,2,7 (rep 6) | Fault |
| 2 | 1,2,7 | Hit |
LRU Page Faults = 6, Hits = 4
Summary
| Algorithm | Page Faults | Hits |
|---|---|---|
| FIFO | 6 | 4 |
| LRU | 6 | 4 |
Both FIFO and LRU produce 6 page faults for this reference string with 3 frames.
2080.1 · 5 marks
Define working set. How does clock replacement algorithm works? [5]
Concept of Locality of Reference
2079 · 5 marks
Why do we need the concept of locality of reference? List the advantages and disadvantages of Round Robin algorithm. [5]
Monoprogramming vs. Multi-programming
2076 · 5 marks
Differentiate between multi programming and Monoprogramming. What will be the CPU utilization with 6 processes with 60% IO waiting time are in memory? [5]
Relocation and Protection
2076 · 5 marks
Why program relocation and protection is important? Explain the technique of achieving program relocation and protection. [5]
Make Unit 4 stick
Practice CSC264 with flashcards & quizzes