0% found this document useful (0 votes)
116 views17 pages

Unit-4 MM 2022

The document discusses memory management techniques in operating systems. It describes basic approaches like resident monitors that reserve a portion of memory for the OS. It also covers multiprogramming with fixed and variable partitions that allocate memory to processes. Paging and segmentation are introduced as non-contiguous memory management techniques using virtual memory. Key aspects of memory management systems include tracking used memory, allocating and reallocating to processes, and swapping to disk when needed.

Uploaded by

Stuti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views17 pages

Unit-4 MM 2022

The document discusses memory management techniques in operating systems. It describes basic approaches like resident monitors that reserve a portion of memory for the OS. It also covers multiprogramming with fixed and variable partitions that allocate memory to processes. Paging and segmentation are introduced as non-contiguous memory management techniques using virtual memory. Key aspects of memory management systems include tracking used memory, allocating and reallocating to processes, and swapping to disk when needed.

Uploaded by

Stuti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Unit – IV

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.

1. Keep track of what parts of memory are in use.


2. Allocate memory to processes when needed.
3. Reallocate when processes are done.
4. Swapping, or paging, between main memory and disk, when disk is too small to
hold all current processes.

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

Level 2 Process Memory

Fig. 1: Level of Memory Management

Memory Management System

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.

MS-DOS is an example of a system which allocates memory in this way. An embedded


system running a single application might also use this technique.
Ex. Basic Bare Machine
It is the simplest and basic memory management system in which the user has complete
control over the memory. This is purely single user approach.

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

Ex. Resident Monitor


It is also a simple memory management system in which memory is divided in two
contiguous areas. One of them is permanently allocated to the resident portion of the OS.
The remaining memory is allocated to the process and user space.

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.

1. Multiprogramming With Fixed/Static Partitions


In fixed partitioning, main memory is divided into a number of static/fixed partition at system
generation time. A process may be loaded into partition of equal or unequal size. The
simplest scheme for memory management is to partition it into region with fixed boundaries.

(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)

Fig.1 : Equal Size Partitions

(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)

Fig.2 : Unequal Size Partitions


2. Multiprogramming With Variable/Dynamic Partitions
Main memory usually into two partitions:
 Resident operating system, usually held in low memory with interrupt vector
 User processes, held in high memory

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)

Placement Strategies/ Dynamic Storage-Allocation Problem

How to satisfy a request of size n from a list of free holes

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.

Best-fit : Allocate the free block closest to the requested size.


Example-1: Given memory partition 100k, 500k, 200k, 300k and 600k in order.
Process requiring space 200k, 417k, 112k, 426k in order.
Place the processes in memory by First Fit, Next Fit and Best Fit manner.
Solution :
 First Fit

(200k) 100k 500k 200k 300k 600k

|---------200---------|
(417k) 100k 500k 200k 300k 600k

|----- 417-----|-83-|----------200---------|

(112k) 100k 500k 200k 300k 600k

|----- 417-----|-83-|---------200k--------|---- 112----|---188---|

(426k) 100k 500k 200k 300k 600k

|----- 417-----|-83-|----------200k------ |---- 112-----|---188---|------- 426-----|--174-|

 Next Fit

 First request 200k satisfied by 200k memory hole


 Second request 417k satisfied by next suitable memory hole 600k. The left space is 183K.
 Third request 112k satisfied by remaining 183k
 Fourth request 426k satisfied by 500k memory hole.

(200k) 100k 500k 200k 300k 600k

|---------200--------|
(417k) 100k 500k 200k 300k 600k

|----------200------| |----- 417----|---183---|

(112k) 100k 500k 200k 300k 600k

|---------200k-------| |----- 417----|-112-|-71|

(426k) 100k 500k 200k 300k 600k

|----- 426-----|-74-|----------200k-----| |----- 417----|-112-|-71|

 Best Fit

 This algorithm chooses the free block closest to the requested size.

100k 500k 200k 300k 600k

|----- 426-----|-74-|----------200k------| |----- 417----|-112-|-71|


Example-2: Consider six memory partition of size 200 KB, 400 KB, 600 KB, 500 KB and 300
KB and 250 KB. These partitions need to be allocated to four process of size 357 KB, 210
KB, 468 KB, 491 KB in that order. Find out the perform the allocation of processing using
First Fit, Next Fit and Best Fit Algorithm.

Internal Structure of Hard Disk


Fragmentation
Fragmentation is the situation in which the memory becomes divided into many small
blocks that none of them are useful because they are too small to satisfy a pending request.
Fragmentation occurs in a dynamic memory allocation system when many of the free blocks
are too small to satisfy any request. There are two type of fragmentation:

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.

Paging is an important part of virtual memory implementation in most current general-


purpose operating systems, allowing them to use secondary storage for data that does not fit
into physical random-access memory (RAM).

 It is non-contiguous allocation memory management system.

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

Page No. Frame No.


0 0
1 2
2 5
3 3
4 6

Fig. Page Table Design


Example: We have 15 frames available and 4 process with following number of pages at
the show below in Fig.

Process No. Pages No.


A 4

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

(A) 15 Frame Available and (B) Process B Swaps out and


Process A,B, and C Allocated Process D Allocates

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)

Physical Address: (Frame Number, Offset)

The result of using a page size that is power of 2

Logical Address Space=2m

Page Size= 2n

Then, Page Number = m – n bits and Offset = n

Logical Address becomes: (m-n, n)

Physical Address = Page No. * Page Size + Offset = (m-n) * 2n + n

Example:

Consider a logical address space of 8 page of 1024 words each, mapped onto a physical
memory of 32 frame.

(i) How many bits are there in the logical address?


(ii) How many bits are there in the physical address?

Solution:

(i) Total Logical address space = 1024 * 8


= 210 * 23 = 213
13 bits for logical address
Page Size = 210
10 bits for offset and 3 bits for page number

(i) Total Physical address space = 1024 * 32


= 210 * 25 = 215
15 bits for physical address
Frame Size = 210
10 bits for offset and 5 bits for frame number
Segmentation
 Address space of process broken into pieces called as segmentation.
 Each segment has a name and length.
 Each process has its own segment table.
 In segmentation, a logical address consists of two parts:

Segment Number Offset into that Segment

 Segment Number used to index into segment table.


 Offset must be less than the length of the segment.
 Physical Address = Base Address + Offset if (Offset < Length of Segment)

Example: Consider two segments with their segment table as given below.

Segment Number Length Base


0 400 4300

1 1100 3200

Fig.: Segment Table

Let us suppose offset is 53.

For segment 0, Physical Address = 4300 + 53 = 4353 (As 53 < 400)

For segment 1, Physical Address = 3200 + 53 = 3253 (As 53 < 1100)


Example: Consider the following segment. [UPTU-2005-06]

Segment Number Base Length


0 219 600
1 2300 14
2 90 100
3 1327 580
4 1952 96

Find the Physical Address for the following logical address


(A) 0, 430 (B) 1, 12 (C) 2, 500 (D) 3, 400 (E) 4, 110

Solution:

(A) 0, 430 : Segment 0, Offset 430


Physical Address = Base + Offset = 219 + 430 = 649 (As Offset < LS, 430<600)

(B) 1, 12 : Segment 1, Offset 12


Physical Address = Base + Offset = 2300 + 12 = 2312 (As Offset < LS, 12<14)

(C) 2, 500 : Segment 2, Offset 500


Physical Address = Base + Offset = 90 + 500 = 590 but this is not possible because condition
is not true (As Offset < LS, 500<100)

(D) 3, 400 : Segment 3, Offset 400


Physical Address = Base + Offset = 400 + 1327 = 1727 (As Offset < LS, 400<580)

(E) 4, 110 : Segment 4, Offset 110


Physical Address = Base + Offset = 110 + 1952 = 2062 but this is not possible because
condition is not true (As Offset < LS, 1100< 96)

Difference between Paging and Segmentation

Paging Segmentation

1. Main memory is partitioned into frame. 1. Main memory is not partitioned.

2. Program is divided into pages. 2. Program is divided into segments.

3. It causes Internal Fragmentation. 3. It causes External Fragmentation.

4. Present memory as a single dimension 4. Memory can be referenced in two dimension

5. Logical Address comprises of 5. Logical Address comprises of

Page No. Offset Segment No. Offset


Segmentation with Paging

Paging and Segmentation implemented simultaneously.

 A virtual address become a triple (Seg, Page, Offset).

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

Fig.: Segmentation with Paging

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

S=11 P=9 D=12

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.

(ii) Maximum Segment size=29 (P 9 bits) , (No of pages in the segment)

(iii) Maximum Number of Pages = Size of logical address = 232 = 220


Size of each page 212

Maximum Number of Segments = Size of logical address = 232 = 223


Size of Max. Segment 29
Page Replacement Algorithms
 The page replacement is done by swapping the required page from secondary storage to
main memory and vice-versa.

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

 The following are page replacement algorithms:

1. First-In-First-Out Algorithm (FIFO)


2. Least-Recently-Used Algorithm (LRU)
3. Least-Frequently-Used Algorithm (LFU) or Optimal Replacement

1. First-In-First-Out Algorithm (FIFO)

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

2. Least-Recently-Used Algorithm (LRU)

In LRU, “We can replace the page that has not been used for the longest period of
time.”

3. Least-Frequently-Used Algorithm (LFU)

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.

 A process is in thrashing, if it is spending more time in paging instead of their execution.

 High Paging activity is called thrashing.

 Frequently paging techniques is called thrashing.

What are measuring taken to avoid thrashing

 Increase the amount of RAM in the computer (generally the best long-term solution).

 Decrease the number of programs being run on the computer.

 Replace programs that are memory-heavy with equivalents that use less memory.

 Assign working priorities to programs, i.e. low, normal, and high.


Example- 3: Consider reference string 1,3,0,3,5,6,3 with 3 page frame find the Number of
page faults

Solution:

1. FIFO

Page Fault =

2. LRU

Page Fault =

You might also like