4 Memory Management

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.

Memory Management: notes

Introduction

Memory management is the operating system function that keeps track of every unit of primary memory, whether it is currently allocated to a process or free, and decides which process gets memory, how much, and for how long. The part of the operating system responsible for this is called the memory manager; it allocates memory to a process when the process requests it and reclaims that memory once the process terminates.

Monoprogramming vs. Multi-programming

In monoprogramming, only one program occupies memory at any time; the CPU sits idle whenever that program performs I/O, since there is no other program ready to run in the meantime.

In multiprogramming, memory holds more than one program at once, so when one program's I/O operation blocks it, the CPU switches to another resident program instead of sitting idle.

MonoprogrammingMultiprogramming
Only one program in memory at a timeMore than one program resident in memory
CPU sits idle during I/OCPU switches to another program during I/O, so it is used more efficiently
Needs less memoryNeeds more memory to hold several programs
Only fixed-size partitioning is meaningfulBoth fixed and variable size partitioning can be used
Simple, cheap allocation; no external fragmentationBetter CPU and resource utilization, but harder to implement and can suffer internal or external fragmentation

Modelling Multiprogramming

Multiprogramming's benefit can be modelled directly. If a process spends a fraction $p$ of its time waiting for I/O, then with $n$ independent processes resident in memory, the probability that every one of them is simultaneously waiting for I/O (leaving the CPU idle) is $p^n$, so

$$\text{CPU utilization} = 1 - p^n.$$

Worked example. With $p = 0.5$ and $n = 4$ processes in memory,

$$ \begin{aligned} \text{CPU utilization} &= 1 - p^n \ &= 1 - 0.5^4 \ &= 1 - 0.0625 \ &= 0.9375. \end{aligned} $$

Utilization rises to $93.75%$ with four processes resident, compared with only $1-p = 0.5$, or $50%$, in monoprogramming, where there is no second process to fall back on while the first is blocked. This is the formal reason multiprogramming pays off whenever $p$ (the fraction of time a typical process spends waiting for I/O) is not small.

Multiprogramming with fixed and variable partitions

Multiprogramming needs main memory divided into partitions, one per resident process, and there are two ways to create them.

Fixed (static) partitioning divides memory into a set number of partitions decided in advance; each partition holds exactly one program, so the number of programs that can be resident is bounded by the number of partitions. A job waiting to run is queued for the smallest partition large enough to hold it. Since a partition's size is fixed, any space left over inside it once a smaller job is loaded is wasted for as long as that job runs, a problem called internal fragmentation.

  • Advantages: simple to implement, low operating system overhead.
  • Disadvantages: internal fragmentation, a hard limit on how large a single process can be (bounded by the largest partition), and a hard limit on the degree of multiprogramming (bounded by the number of partitions).

Variable (dynamic) partitioning creates a partition of exactly the size a process needs at the moment it is loaded; when a process terminates or is swapped out, its memory is returned to a pool of free space from which future partitions are carved.

  • Advantages: no internal fragmentation, no fixed limit on process size or on the degree of multiprogramming.
  • Disadvantages: harder to implement, and prone to external fragmentation, where the total free memory is enough to satisfy a request but is scattered in pieces too small individually to hold it.

Compaction relocates the resident programs in memory so that the small, scattered free chunks are merged into one larger contiguous block, big enough to satisfy new requests; it is also called defragmentation, and its cost is the time spent copying every relocated program.

Relocation and Protection

Relocation is the ability to move a process around in memory without breaking its execution, which matters because branch instructions and data references inside a program are expressed as addresses that must still resolve correctly wherever the process ends up living physically. In static relocation, a program's addresses are fixed up once, before or during loading, and the program must then always run at that same memory location or be relocated again from scratch. In dynamic relocation, the mapping from the process's logical (virtual) addresses to physical addresses is computed at run time, typically by adding a relocation register's value to every logical address the CPU generates, so the process can be moved freely in physical memory without altering its code.

Protection controls which memory a process is allowed to touch, so that a bug or malicious code in one process cannot read or corrupt another process's memory or the operating system's own memory. Write protection stops instructions or data from being overwritten, and read protection keeps data and instructions private from processes that should not see them. A limit register, checked alongside the relocation register on every memory access, traps any address that falls outside the bounds legitimately owned by the running process, converting an out-of-bounds reference into an addressing-error trap rather than a silent corruption of another process's memory.

Memory management (Bitmaps & Linked-list)

Once memory (or disk) space is divided into fixed-size allocation units, the operating system needs a compact way to record which units are free. Two classic schemes exist.

A bitmap (bit vector) dedicates one bit to every allocation unit: a $0$ can mean the unit is free and a $1$ that it is allocated (or vice versa, by convention). A bitmap is simple and lets the manager find a free unit quickly by scanning for a $0$ bit, but making the allocation unit small enough to avoid wasting space inside a partition also makes the bitmap itself large, and it is a comparatively expensive structure to search whenever a large contiguous run of free units is needed.

A linked list instead threads every hole (or, on disk, every free block) together, using the first word of each free unit as a pointer to the next free unit, with the head of the list cached in memory for fast access. This uses less space than a naive bitmap when there are few holes, but finding a large contiguous run, or simply reaching a specific position in the list, means walking the list one link at a time.

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]

Segmentation is a memory management technique in which memory is divided into variable size parts. Each part is known as a segment, which can be allocated to a process. The details about each segment are stored in a table called the segment table. --- The segment table contains two main pieces of...

Open this answer on its own page →

2080 · 5 marks

Differentiate between paging and segmentation. [5]

Paging and segmentation are both memory management techniques used by operating systems, but they differ in several important ways. --- Basis Paging Segmentation --------- Block Size Page is always of fixed block size. Segment is of variable block size. Fragmentation Paging may lead to internal f...

Open this answer on its own page →

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 answer

Memory Allocation: First Fit, Next Fit, Best Fit

Given

Holes in memory order:

Hole #Size
115 MB
22 MB
310 MB
46 MB
58 MB
620 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

RequestFirst FitNext FitBest Fit
(a) 10 MBHole 1 (15 MB)Hole 1 (15 MB)Hole 3 (10 MB)
(b) 10 MBHole 3 (10 MB)Hole 3 (10 MB)Hole 1 (15 MB)
Open this answer on its own page →

2080 · 5 marks

Explain different memory allocation strategies. [5]

Memory allocation strategies are methods used by the process manager to allocate free memory partitions (holes) to processes. The main strategies are described below: --- The process manager scans the list of segments from the beginning and allocates the first free partition that is large enough ...

Open this answer on its own page →

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]

Aspect Internal Fragmentation External Fragmentation --------- Definition Wasted memory inside an allocated partition when the process is smaller than the block assigned to it Total free memory is enough, but it is split into scattered non-contiguous holes, none big enough for a request Location ...

Open this answer on its own page →

Virtual memory

2081 · 5 marks

Write short notes on: a. Virtual Memory b. Race Condition [5]

Show solved 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:

  1. A segment number is used to find the segment descriptor.
  2. A check is made whether the segment's page table is in memory; if not, a segment fault occurs.
  3. The page table entry for the requested virtual page is examined; if the page is not in memory, a page fault is triggered.
  4. If the page is in memory, the main memory address is extracted from the page table entry.
  5. The offset is added to the page origin to give the final main memory address, and the read/write takes place.

Advantages and Disadvantages:

AdvantageDisadvantage
Programs larger than physical RAM can runDisk access is slower than RAM, reducing speed
More processes can be accommodatedExcessive 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.

Open this answer on its own page →

2080 · 5 marks

When does a page fault occur? Give a structure of a page table. [5]

A page fault occurs when a program attempts to access data or code that is in its address space, but is not currently located in the system RAM (i.e., the required page is not present in physical memory). When a page fault occurs, the following sequence of events takes place: 1. The memory addres...

Open this answer on its own page →

2079 · 5 marks

Write the structure and advantages of TLB. [5]

--- TLB is a special high-speed hardware cache (associative memory) used to speed up virtual-to-physical address translation in a paging system. It stores recently used page table entries to avoid repeated access to the main memory page table. --- TLB is organized as an associative memory consist...

Open this answer on its own page →

2080.1 · 5 marks

Why do we need virtual memory? Describe the structure of a page table. [5]

--- Virtual memory is a memory management technique that allows a computer to execute programs that are larger than the available physical (main) memory. The key reasons we need virtual memory are: 1. Running large programs: A process does not need to be fully loaded into physical memory to execu...

Open this answer on its own page →

Belady's Anomaly

2080 · 5 marks

What does Belady's anomaly mean? What are the benefits of multiprogramming over uniprogramming? [5]

--- Belady's anomaly is the phenomenon in which increasing the number of page frames results in an increase in the number of page faults for certain memory access patterns. This is a counter-intuitive behavior because logically, more frames should mean more pages can be kept in memory, resulting ...

Open this answer on its own page →

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]

  • Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 (13 references) - Number of frames: 4 --- Replace the page not needed for the longest time in the future. Ref Frames Fault? --------------------- 7 7 F 0 7,0 F 1 7,0,1 F 2 7,0,1,2 F 0 7,0,1,2 Hit 3 replace 7 (never used again) → 3,0,1,2 F ...
Open this answer on its own page →

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 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.

StepRefFramesFault/Hit
11[1]Fault
23[1,3]Fault
34[1,3,4]Fault
42next: 1→9, 3→5, 4→7 → replace 1 → [2,3,4]Fault
53[2,3,4]Hit
65next: 2→10, 3→8, 4→7 → replace 2 → [5,3,4]Fault
74[5,3,4]Hit
83[5,3,4]Hit
91next: 5→never, 3→13, 4→11 → replace 5 → [1,3,4]Fault
102next: 1→15, 3→13, 4→11 → replace 1 → [2,3,4]Fault
114[2,3,4]Hit
126next: 2→14, 3→13, 4→16 → replace 4 → [2,3,6]Fault
133[2,3,6]Hit
142[2,3,6]Hit
151next: 2→17, 3→never, 6→never → replace 3 → [2,1,6]Fault
164next: 2→17, 1→never, 6→never → replace 6 → [2,1,4]Fault
172[2,1,4]Hit

OPR Page Faults = 10, Hits = 7


2. LRU (Least Recently Used)

Replace the page unused for the longest time.

StepRefFrames (after)Fault/HitRecency (recent→old)
11[1]Fault1
23[1,3]Fault3,1
34[1,3,4]Fault4,3,1
42evict 1 → [2,3,4]Fault2,4,3
53[2,3,4]Hit3,2,4
65evict 4 → [2,3,5]Fault5,3,2
74evict 2 → [4,3,5]Fault4,5,3
83[4,3,5]Hit3,4,5
91evict 5 → [4,3,1]Fault1,3,4
102evict 4 → [2,3,1]Fault2,1,3
114evict 3 → [2,4,1]Fault4,2,1
126evict 1 → [2,4,6]Fault6,4,2
133evict 2 → [3,4,6]Fault3,6,4
142evict 4 → [3,2,6]Fault2,3,6
151evict 6 → [3,2,1]Fault1,2,3
164evict 3 → [4,2,1]Fault4,1,2
172[4,2,1]Hit2,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.

StepRefActionFrames (bit)Ptr afterF/H
11load pos01(1) - -1Fault
23load pos11(1) 3(1) -2Fault
34load pos21(1) 3(1) 4(1)0Fault
42ptr0: 1 bit1→0, ptr1:3→0, ptr2:4→0, ptr0:1 bit0 replace2(1) 3(0) 4(0)1Fault
53hit, set 3→12(1) 3(1) 4(0)1Hit
65ptr1:3 bit1→0, ptr2:4 bit0 replace2(1) 3(0) 5(1)0Fault
74ptr0:2→0, ptr1:3 bit0 replace2(0) 4(1) 5(1)2Fault
83ptr2:5→0, ptr0:2 bit0 replace3(1) 4(1) 5(0)1Fault
91ptr1:4→0, ptr2:5 bit0 replace3(1) 4(0) 1(1)0Fault
102ptr0:3→0, ptr1:4 bit0 replace3(0) 2(1) 1(1)2Fault
114ptr2:1→0, ptr0:3 bit0 replace4(1) 2(1) 1(0)1Fault
126ptr1:2→0, ptr2:1 bit0 replace4(1) 2(0) 6(1)0Fault
133ptr0:4→0, ptr1:2 bit0 replace4(0) 3(1) 6(1)2Fault
142ptr2:6→0, ptr0:4 bit0 replace2(1) 3(1) 6(0)1Fault
151ptr1:3→0, ptr2:6 bit0 replace2(1) 3(0) 1(1)0Fault
164ptr0:2→0, ptr1:3 bit0 replace2(0) 4(1) 1(1)2Fault
172hit, set 2→12(1) 4(1) 1(1)2Hit

Faults: all steps except 5 and 17 = 15 faults, Hits = 2

Clock Page Faults = 15


Summary

AlgorithmPage Faults
OPR10
LRU14
Clock15

(OPR gives the fewest faults, confirming it as the optimal benchmark.)

Open this answer on its own page →

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]

--- In FIFO (First-In-First-Out) page replacement, the page that has been in memory the longest is replaced first, regardless of how frequently or recently it has been used. Key characteristics of FIFO: - Pages are maintained in a queue; the oldest page (front of queue) is replaced when a page fa...

Open this answer on its own page →

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 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.

RefFrame contents (oldest→newest)Fault?
44Fault
74, 7Fault
64, 7, 6Fault
17, 6, 1 (replace 4)Fault
77, 6, 1Hit
67, 6, 1Hit
17, 6, 1Hit
26, 1, 2 (replace 7)Fault
71, 2, 7 (replace 6)Fault
21, 2, 7Hit

FIFO Page Faults = 6, Hits = 4


2. LRU (Least Recently Used)

Replace the page not used for the longest time.

RefFramesRecency (MRU→LRU)Fault?
444Fault
74, 77, 4Fault
64, 7, 66, 7, 4Fault
17, 6, 11, 6, 7 (replace 4, LRU)Fault
77, 6, 17, 1, 6Hit
67, 6, 16, 7, 1Hit
17, 6, 11, 6, 7Hit
27, 1, 22, 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.

RefFramesRecency (MRU→LRU)Fault?
26, 1, 2 (replace 7)2, 1, 6Fault
76, 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.

RefFramesRecency (MRU→LRU)Fault?
71, 2, 7 (replace 6)7, 2, 1Fault
21, 2, 72, 7, 1Hit

Corrected LRU trace:

RefFramesFault?
44Fault
74,7Fault
64,7,6Fault
17,6,1 (rep 4)Fault
77,6,1Hit
67,6,1Hit
17,6,1Hit
26,1,2 (rep 7)Fault
71,2,7 (rep 6)Fault
21,2,7Hit

LRU Page Faults = 6, Hits = 4


Summary

AlgorithmPage FaultsHits
FIFO64
LRU64

Both FIFO and LRU produce 6 page faults for this reference string with 3 frames.

Open this answer on its own page →

2080.1 · 5 marks

Define working set. How does clock replacement algorithm works? [5]

--- The working set of a process is the set of pages that a process is currently using (actively referencing) within a defined time window. Formally, the working set W(t, Δ) is defined as: W(t, Δ) = the set of pages referenced by a process during the time interval (t - Δ, t) where: - t = current ...

Open this answer on its own page →

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]

--- Locality of Reference refers to the tendency of a processor to access the same set of memory locations repetitively over a short period of time. It is the basis for the design of cache memory and virtual memory systems. 1. Basis for Virtual Memory: Virtual memory systems load only a portion o...

Open this answer on its own page →

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]

  • Number of processes: $n = 6$ - I/O wait fraction: $p = 60% = 0.60$ --- Feature Monoprogramming Multiprogramming --------- Definition Only one program resides and executes in memory at a time Several programs reside in memory simultaneously and the CPU switches among them Memory Use Single part...
Open this answer on its own page →

Relocation and Protection

2076 · 5 marks

Why program relocation and protection is important? Explain the technique of achieving program relocation and protection. [5]

Program Relocation is important because: - In a multiprogramming environment, multiple processes must reside in memory simultaneously. A program cannot always be loaded at the same fixed memory address, so it must be relocated to whatever memory space is available at load time or run time. - With...

Open this answer on its own page →