OS Unit-4 23-24
OS Unit-4 23-24
Memory Management
1/93
Operating Systems - Unit-IV February 23, 2024 1 / 93
UNIT-IV Syllabus
Memory Management
Logical Vs Physical Address Space
Swapping
Contiguous Memory Allocation
Segmentation
Paging
Segmentation with Paging
Virtual Memory Management
Background
Demand Paging
Copy-on-Write
Page Replacement
Page Replacement Algorithms
Allocation of Frames
Thrashing
Virtual memory in Windows.
2/93
Operating Systems - Unit-IV February 23, 2024 2 / 93
Memory Management Background
Background
Program must be brought (from disk) into memory and placed within
a process for it to be run
Main memory and registers are only storage CPU can access directly
Memory unit only sees a stream of addresses + read requests, or
address + data and write requests
Register access in one CPU clock (or less)
Main memory can take many cycles, causing a stall
Cache sits between main memory and CPU registers
Protection of memory required to ensure correct operation
3/93
Operating Systems - Unit-IV February 23, 2024 3 / 93
Memory Management Background
A pair of base and limit registers define the logical address space
CPU must check every memory access generated in user mode to be
sure it is between base and limit for that user
4/93
Operating Systems - Unit-IV February 23, 2024 4 / 93
Memory Management Background
5/93
Operating Systems - Unit-IV February 23, 2024 5 / 93
Memory Management Background
Address Binding
6/93
Operating Systems - Unit-IV February 23, 2024 6 / 93
Memory Management Background
7/93
Operating Systems - Unit-IV February 23, 2024 7 / 93
Memory Management Background
8/93
Operating Systems - Unit-IV February 23, 2024 8 / 93
Memory Management Background
9/93
Operating Systems - Unit-IV February 23, 2024 9 / 93
Memory Management Background
10/93
Operating Systems - Unit-IV February 23, 2024 10 / 93
Memory Management Background
11/93
Operating Systems - Unit-IV February 23, 2024 11 / 93
Memory Management Background
Dynamic Linking
Swapping
Swapping (Cont.)
Does the swapped out process need to swap back in to same physical
addresses?
Depends on address binding method
Plus consider pending I/O to / from process memory space
Modified versions of swapping are found on many systems (i.e.,
UNIX, Linux, and Windows)
Swapping normally disabled
Started if more than threshold amount of memory allocated
Disabled again once memory demand reduced below threshold
14/93
Operating Systems - Unit-IV February 23, 2024 14 / 93
Memory Management Swapping
15/93
Operating Systems - Unit-IV February 23, 2024 15 / 93
Memory Management Swapping
16/93
Operating Systems - Unit-IV February 23, 2024 16 / 93
Memory Management Swapping
17/93
Operating Systems - Unit-IV February 23, 2024 17 / 93
Memory Management Swapping
18/93
Operating Systems - Unit-IV February 23, 2024 18 / 93
Memory Management Contiguous Allocation
Contiguous Allocation
19/93
Operating Systems - Unit-IV February 23, 2024 19 / 93
Memory Management Contiguous Allocation
20/93
Operating Systems - Unit-IV February 23, 2024 20 / 93
Memory Management Contiguous Allocation
21/93
Operating Systems - Unit-IV February 23, 2024 21 / 93
Memory Management Contiguous Allocation
Multiple-partition allocation
Multiple-partition allocation
Degree of multiprogramming limited by number of partitions
Variable-partition sizes for efficiency (sized to a given process’ needs)
Hole – block of available memory holes of various size are scattered
throughout memory
When a process arrives, it is allocated memory from a hole large
enough to accommodate it
Process exiting frees its partition, adjacent free partitions combined
Operating system maintains information about
allocated partitions
free partitions (hole)
22/93
Operating Systems - Unit-IV February 23, 2024 22 / 93
Memory Management Contiguous Allocation
Multiple-partition allocation
23/93
Operating Systems - Unit-IV February 23, 2024 23 / 93
Memory Management Contiguous Allocation
24/93
Operating Systems - Unit-IV February 23, 2024 24 / 93
Memory Management Contiguous Allocation
Fragmentation
25/93
Operating Systems - Unit-IV February 23, 2024 25 / 93
Memory Management Contiguous Allocation
Fragmentation (Cont.)
26/93
Operating Systems - Unit-IV February 23, 2024 26 / 93
Memory Management Segmentation
Segmentation
27/93
Operating Systems - Unit-IV February 23, 2024 27 / 93
Memory Management Segmentation
28/93
Operating Systems - Unit-IV February 23, 2024 28 / 93
Memory Management Segmentation
29/93
Operating Systems - Unit-IV February 23, 2024 29 / 93
Memory Management Segmentation
Segmentation Architecture
30/93
Operating Systems - Unit-IV February 23, 2024 30 / 93
Memory Management Segmentation
Protection
With each entry in segment table associate:
validation bit = 0 ->illegal segment
read/write/execute privileges
Protection bits associated with segments; code sharing occurs at
segment level
Since segments vary in length, memory allocation is a dynamic
storage-allocation problem
A segmentation example is shown in the following diagram
31/93
Operating Systems - Unit-IV February 23, 2024 31 / 93
Memory Management Segmentation
Segmentation Hardware
32/93
Operating Systems - Unit-IV February 23, 2024 32 / 93
Memory Management Paging
paging
34/93
Operating Systems - Unit-IV February 23, 2024 34 / 93
Memory Management Paging
Paging Hardware
35/93
Operating Systems - Unit-IV February 23, 2024 35 / 93
Memory Management Paging
36/93
Operating Systems - Unit-IV February 23, 2024 36 / 93
Memory Management Paging
Paging Example
37/93
Operating Systems - Unit-IV February 23, 2024 37 / 93
Memory Management Paging
Paging (Cont.)
38/93
Operating Systems - Unit-IV February 23, 2024 38 / 93
Memory Management Paging
Free Frames
39/93
Operating Systems - Unit-IV February 23, 2024 39 / 93
Memory Management Paging
40/93
Operating Systems - Unit-IV February 23, 2024 40 / 93
Memory Management Paging
41/93
Operating Systems - Unit-IV February 23, 2024 41 / 93
Memory Management Paging
Associative Memory
42/93
Operating Systems - Unit-IV February 23, 2024 42 / 93
Memory Management Paging
43/93
Operating Systems - Unit-IV February 23, 2024 43 / 93
Memory Management Paging
44/93
Operating Systems - Unit-IV February 23, 2024 44 / 93
Memory Management Paging
Memory Protection
45/93
Operating Systems - Unit-IV February 23, 2024 45 / 93
Memory Management Paging
46/93
Operating Systems - Unit-IV February 23, 2024 46 / 93
Memory Management Structure of page table
47/93
Operating Systems - Unit-IV February 23, 2024 47 / 93
Memory Management Structure of page table
48/93
Operating Systems - Unit-IV February 23, 2024 48 / 93
Memory Management Structure of page table
49/93
Operating Systems - Unit-IV February 23, 2024 49 / 93
Memory Management Structure of page table
Address-Translation Scheme
51/93
Operating Systems - Unit-IV February 23, 2024 51 / 93
Memory Management Structure of page table
52/93
Operating Systems - Unit-IV February 23, 2024 52 / 93
Memory Management Structure of page table
53/93
Operating Systems - Unit-IV February 23, 2024 53 / 93
Memory Management Structure of page table
55/93
Operating Systems - Unit-IV February 23, 2024 55 / 93
Memory Management Structure of page table
Rather than each process having a page table and keeping track of all
possible logical pages, track all physical pages
One entry for each real page of memory
Entry consists of the virtual address of the page stored in that real
memory location, with information about the process that owns that
page
Decreases memory needed to store each page table, but increases
time needed to search the table when a page reference occurs
Use hash table to limit the search to one — or at most a few —
page-table entries
TLB can accelerate access
But how to implement shared memory?
One mapping of a virtual address to the shared physical address
56/93
Operating Systems - Unit-IV February 23, 2024 56 / 93
Memory Management Structure of page table
57/93
Operating Systems - Unit-IV February 23, 2024 57 / 93
Virtual memory Management Background
Background
58/93
Operating Systems - Unit-IV February 23, 2024 58 / 93
Virtual memory Management Background
Background (Cont.)
59/93
Operating Systems - Unit-IV February 23, 2024 59 / 93
Virtual memory Management Background
Background (Cont.)
60/93
Operating Systems - Unit-IV February 23, 2024 60 / 93
Virtual memory Management Background
61/93
Operating Systems - Unit-IV February 23, 2024 61 / 93
Virtual memory Management Background
Virtual-address Space
62/93
Operating Systems - Unit-IV February 23, 2024 62 / 93
Virtual memory Management Background
63/93
Operating Systems - Unit-IV February 23, 2024 63 / 93
Virtual memory Management Demand paging
Demand Paging
64/93
Operating Systems - Unit-IV February 23, 2024 64 / 93
Virtual memory Management Demand paging
Basic Concepts
65/93
Operating Systems - Unit-IV February 23, 2024 65 / 93
Virtual memory Management Demand paging
Valid-Invalid Bit
66/93
Operating Systems - Unit-IV February 23, 2024 66 / 93
Virtual memory Management Demand paging
67/93
Operating Systems - Unit-IV February 23, 2024 67 / 93
Virtual memory Management Demand paging
Page Fault
68/93
Operating Systems - Unit-IV February 23, 2024 68 / 93
Virtual memory Management Demand paging
69/93
Operating Systems - Unit-IV February 23, 2024 69 / 93
Virtual memory Management Demand paging
70/93
Operating Systems - Unit-IV February 23, 2024 70 / 93
Virtual memory Management Demand paging
Instruction Restart
71/93
Operating Systems - Unit-IV February 23, 2024 71 / 93
Virtual memory Management Demand paging
73/93
Operating Systems - Unit-IV February 23, 2024 73 / 93
Virtual memory Management Demand paging
74/93
Operating Systems - Unit-IV February 23, 2024 74 / 93
Virtual memory Management Demand paging
Copy-on-Write
77/93
Operating Systems - Unit-IV February 23, 2024 77 / 93
Virtual memory Management Copy-on-Write
78/93
Operating Systems - Unit-IV February 23, 2024 78 / 93
Virtual memory Management Copy-on-Write
79/93
Operating Systems - Unit-IV February 23, 2024 79 / 93
Virtual memory Management Page replacement
Page Replacement
80/93
Operating Systems - Unit-IV February 23, 2024 80 / 93
Virtual memory Management Page replacement
81/93
Operating Systems - Unit-IV February 23, 2024 81 / 93
Virtual memory Management Page replacement
82/93
Operating Systems - Unit-IV February 23, 2024 82 / 93
Virtual memory Management Page replacement
Page Replacement
83/93
Operating Systems - Unit-IV February 23, 2024 83 / 93
Virtual memory Management Page replacement
84/93
Operating Systems - Unit-IV February 23, 2024 84 / 93
Virtual memory Management Allocation of Frames
Allocation of Frames
85/93
Operating Systems - Unit-IV February 23, 2024 85 / 93
Virtual memory Management Allocation of Frames
Fixed Allocation
86/93
Operating Systems - Unit-IV February 23, 2024 86 / 93
Virtual memory Management Allocation of Frames
Priority Allocation
87/93
Operating Systems - Unit-IV February 23, 2024 87 / 93
Virtual memory Management Allocation of Frames
88/93
Operating Systems - Unit-IV February 23, 2024 88 / 93
Virtual memory Management Allocation of Frames
89/93
Operating Systems - Unit-IV February 23, 2024 89 / 93
Virtual memory Management Thrashing
Thrashing
If a process does not have “enough” pages, the page-fault rate is very
high
Page fault to get page
Replace existing frame
But quickly need replaced frame back
This leads to:
Low CPU utilization
Operating system thinking that it needs to increase the degree of
multiprogramming Another process added to the system
Thrashing - a process is busy swapping pages in and out
90/93
Operating Systems - Unit-IV February 23, 2024 90 / 93
Virtual memory Management Thrashing
Thrashing (Cont.)
91/93
Operating Systems - Unit-IV February 23, 2024 91 / 93
Virtual memory Management Thrashing
92/93
Operating Systems - Unit-IV February 23, 2024 92 / 93
Virtual memory Management Virtual Memory in windows