Unit-4 MM 2022
Unit-4 MM 2022
Memory Management
Basic bare machine, Resident monitor, Multiprogramming with fixed partitions,
Multiprogramming with variable partitions, Protection schemes, Paging, Segmentation,
Paged segmentation, Virtual memory concepts, Demand paging, Performance of demand
paging, Page replacement algorithms, Thrashing, Cache memory organization, Locality of
reference.
The part of OS that manage this memory hierarchy is known as memory manager or
management.
Memory Management is a large array of byte, each with its own address. There are two
level of memory management as shown in Fig. 1.
Level 1 OS Memory
Contiguous Non-Contiguous
(Physical Memory) (Virtual Memory)
1. Single Contiguous 1. Paging
2. Fixed Partition 2. Segmentation
3. Variable Partition 3. Combined ( 1 & 2)
Single Contiguous
Single contiguous allocation is the simplest memory management technique. All the
computer's memory, usually with the exception of a small portion reserved for the operating
system, is available to the single application.
Advantage Disadvantage
1. OS does not provide any services or
1. It is Flexible.
resources to the user.
2. It neither requires any hardware support
2. Virtual memory benefits cannot be
nor software or OS support
taken.
10 MB
User Space
No OS
0 MB
Fig.: Bare Machine
Advantage Disadvantage
1. Minimum utilization of processor and
1. They are simple to design.
memory.
2. They require minimum hardware
support.
10 MB
Process and User
Space
2 MB
Monitor (OS)
0 MB
Fig.: Resident Monitor
Partitions allocation
Partitions allocation divides primary memory into multiple memory partitions, usually contiguous
areas of memory. Each partition might contain all the information for a specific job or task.
Memory management consists of allocating a partition to a job when it starts and un-allocating it
when the job ends.
Partitioned allocation usually requires some hardware support to prevent the jobs from
interfering with one another or with the operating system.
Partitions may be either static, that is defined at Initial Program Load (IPL) or boot time or by
the computer operator, or dynamic, that is automatically created for a specific job. Partitions
allocations are two types for multiprogramming concept.
1. Multiprogramming with fixed/static partitions
2. Multiprogramming with variable/Dynamic partitions
IBM System/360 Operating System Multiprogramming with a Fixed Number of Tasks (MFT)
is an example of static partitioning, and Multiprogramming with a Variable Number of
Tasks (MVT) is an example of dynamic.
(A) Equal Size Partitions: OS occupies some fixed equal portions of main memory as in Fig.
1. The difficulty with equal size partition is internal fragmentation. The main memory
utilization is extremely inefficient. The wasted space inside a partition called an internal
fragmentation.
8 MB
8 MB
8 MB
8 MB (OS)
(B) Unequal Size Partitions: Partitions are unequal. Assign each process to smallest
partitions within which it will fit as shown in Fig. 2. Use of unequal sized partitions
provide flexibility and relatively simple as compare to fixed partitioning schemes.
8 MB
2 MB
4 MB
8 MB
8 MB (OS)
Multiple-partition allocation
Hole – block of available memory; holes of various size are scattered throughout
memory
When a process arrives, it is allocated memory from a large enough hole
Operating system maintains information about:
a) allocated partitions and
b) free partitions (hole)
First-fit : Allocate the first hole that is big enough. The hole is them broken into
two pieces, one for process and other for unused memory.
Next/Worst-fit : Allocate the largest hole; and chooses the next available block that is
big enough must also search entire list.
|---------200---------|
(417k) 100k 500k 200k 300k 600k
|----- 417-----|-83-|----------200---------|
Next Fit
|---------200--------|
(417k) 100k 500k 200k 300k 600k
Best Fit
This algorithm chooses the free block closest to the requested size.
Internal Fragmentation
Internal fragmentation is the space wasted inside of allocated memory blocks because of
limit on the allowed sizes of allocated blocks. Allocated memory may be slightly larger than
requested memory; this size difference is memory internal to a partition, but not being used.
Paging creates Internal Fragmentation.
External Fragmentation
External Fragmentation happens when a dynamic memory allocation algorithm allocates
some memory and a small piece is left over that cannot be effectively used. If too much
external fragmentation occurs, the amount of usable memory is significantly reduced.
Segmentation suffers from External Fragmentation.
Paging
In computer operating systems, paging is one of the memory management schemes by
which a computer stores and retrieves data from the secondary storage for use in main
memory. In the paging memory-management scheme, the operating system retrieves data
from secondary storage in same-size blocks called pages. The main advantage of paging
over memory segmentation is that it allows the physical address space of a process to
be non-contiguous. Before paging came into use, systems had to fit whole programs into
storage contiguously, which caused various storage and fragmentation problems.
In paging, the main memory is divided into equal fixed size chunks that are relative small.
Each process is divided into small fixed size chunks of the same size as shown in Fig.
F
r
a
m
e
N
o
A frame holds one page of data. But, we need to know which frame is holding which
page and for this reason page table is needed as shown in Fig.
B 3
C 4
D 5
Process B swaps out of main memory exactly after allocation. Maintain page tables for process
A,B,C,D and free space as shown in Fig.
0 A0 0 A0
1 A1 1 A1
2 A2 2 A2
3 A3 3 A3
4 B0 4 D0
5 B1 5 D1
6 B2 6 D2
7 C0 7 C0
8 C1 8 C1
9 C2 9 C2
10 C3 10 C3
11 11 D3
12 12 D4
13 13
14 14
Hence, the two process in paging state change as following records as shown in Fig.
Page Frame Page Frame Page Frame Page Frame Frame No.
No. No. No. No. No. No. No. No.
0 0 0 - 0 7 0 4 13
1 1 1 - 1 8 1 5 14
2 2 2 - 2 9 2 6
3 3 3 10 3 11
4 12
Process A Page Table Process B Page Table Process C Page Table Process D Page Table Free Frame List maintained by OS
Logical Address: (Page Number, Offset)
Page Size= 2n
Example:
Consider a logical address space of 8 page of 1024 words each, mapped onto a physical
memory of 32 frame.
Solution:
Example: Consider two segments with their segment table as given below.
1 1100 3200
Solution:
Paging Segmentation
Each segment table entry (STE) points to the page table for the segment.
The size of each segment is a multiple of the page size, since the segment consists of
page.
The page field in the address gives the entry in the chosen page table and the offset
gives the offset in the page as shown in Fig.
Example: In a Paged-Segmented system, a virtual address consists of 32 bites of which 12 bits are
displacement,11 bits are segment number and 9 bits are page number. Calculate the following
(i) Page Size (ii) Maximum Segment Size (iii) Maximum Number of Pages and Segments
Solution: Size of each page is same and 9 bits are for pages given for a 32 bits virtual address.
(i) So, Page Size=212 (d=12 bits) , Size of each segment is different.
When new page is bringing into the memory, one of the existing page must be replaced
by a new page.
The replacement algorithms do the task of selecting the existing page which must be
replaced.
FIFO is the simplest page replacement algorithm, the criteria of this algorithm is “Replace
a page that page is oldest page of all the page of main memory or replace the page
that has been in memory longest.”
In LRU, “We can replace the page that has not been used for the longest period of
time.”
In LFU, “We can replace the page that has the maximum frequency count of all
page.”
Example.1: Solve 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 using FIFO, LRU and optimal page
replacement algorithms and calculate the page fault. Page Frame = 3.
Solution:
1. FIFO
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 4 4 4 0 0 0 7 7 7
0 0 0 3 3 3 2 2 2 1 1 1 0 0
1 1 1 0 0 0 3 3 3 2 2 2 1
Page Fault = 15
2. LRU
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 4 4 4 0 1 1 1
0 0 0 0 0 0 3 3 3 0 0
1 1 3 3 2 2 2 2 2 7
Page Fault = 12
3. LFU-Optimal Replacement
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 2 7
0 0 0 0 4 0 0 0
1 1 3 3 3 1 1
Page Fault = 9
Example- 2: For reference string 1,2,3,4,1,2,5,1,3,4,5. Determine Number of page faults for
number of frame = 3 using FIFO and LRU. [UPTU-2012-13]
Solution:
1. FIFO
1 2 3 4 1 2 5 1 3 4 5
1 1 1 4 4 4 5 5 5
2 2 2 1 1 1 3 3
3 3 3 2 2 2 4
Page Fault = 9
2. LRU
1 2 3 4 1 2 5 1 3 4 5
1 1 1 4 4 4 5 5 4 4
2 2 2 1 1 1 1 1 5
3 3 3 2 2 3 3 3
Page Fault = 9
Thrashing
The event of moving page from primary to secondary storage or vice-versa consumes a
lot of computer’s energy but result is very low. This situation is called Thrashing.
Increase the amount of RAM in the computer (generally the best long-term solution).
Replace programs that are memory-heavy with equivalents that use less memory.
Solution:
1. FIFO
Page Fault =
2. LRU
Page Fault =