Chapter 5 Ppts and Notes
Chapter 5 Ppts and Notes
Chapter 5
Memory
Management
MRS A.S.KHANDAGALE
1
2
Main Memory refers to a physical memory that is the
internal memory to the computer.
The word main is used to distinguish it from external mass
storage devices such as disk drives.
Main memory is also known as RAM.
The computer is able to change only data that is in main
memory. Therefore, every program we execute and every
file we access must be copied from a storage device into
main memory.
3
Memory management
Memory management is the functionality of an operating system
which handles or manages primary memory and moves processes
back and forth between main memory and disk during execution.
Memory management keeps track of each and every memory
location, regardless of either it is allocated to some process or it is
free.
It checks how much memory is to be allocated to processes.
It decides which process will get memory at what time.
It tracks whenever some memory gets freed or unallocated and
correspondingly it updates the status.
4
• In operating systems, Memory Management is the function
responsible for allocating and managing computer’s main
memory.
• Memory Management function keeps track of the status of each
memory location, either allocated or free to ensure effective and
efficient use of Primary Memory.
• There are two Memory Management Techniques: Contiguous,
and Non-Contiguous. In Contiguous Technique, executing
process must be loaded entirely in main-memory. Contiguous
Technique can be divided into:
1.Fixed (or static) partitioning
2.Variable (or dynamic) partitioning
5
Fixed-sized (or static) Memory Partitioning
• This is the oldest and simplest technique used to put more than one
processes in the main memory.
• In Fixed Partitioning, we divide the main memory into partitions, and the
size of partitions can be different or equal.
• In the first partition, the operating system is present, and the remaining
partitions are used to store the user processes.
• In a contiguous way, we allocate the memory to the processes.
• In the fixed Partitioning:
1.There is no overlapping of partitions.
2.For the process execution, the process should be present contiguously.
Thus In this partitioning, number of partitions (non-overlapping) in
RAM are fixed but size of each partition may or may not be same.
6
Job Scheduling in Fixed Size Partitions:
As jobs enter the system ,they are put into a job queue. The job scheduler
takes into account the memory requirement of each job and the available
regions in determining which jobs are allocated memory.
When job is allocated space,it is loaded into a region.
It can the compete for CPU .When job terminates it releases its memory
regions,which job scheduler may then fill with another job from the job
queue 7
• For example a memory of 32k words might be divided into
regions of following sizes:
• Resident Monitor=12K
• Very small jobs =2K
• Average jobs = 6K
• Large jobs = 12K
• If e have three user memory regions of sizes 2K,6K,12K we
need 3 queues Q2,Q6,Q12
8
Example:- If we have three user memory regions of sizes 2K,6K,and
12K . we need three queues :- Q2,Q6,Q12
OS(Monitor=12K)
2K 1K 2K Q2
2K
2K 4K 3K Q6
6K
7K 11K 8K Q12
12K
An incoming job requiring 4K of memory would be appened to Q6,a ne job needing 8K would be
put in Q12,a job of 2K ould go in Q2.
Each queue is scheduled separately.
Since each queue has its own memory region,there is no competition between queues for
memory.Another approach is to throw all jobs into one queue.
9
OS(Monitor=12K)
2K
…. 4K 1K 7K 7K 3K 2K 5K Q
6K
12K
Another approach is to throw all jobs into one queue.
The job scheduler selects the next job to be run and aits until a memory region of that size is
available.
Suppose that we had FCFS job scheduler and regions of 2K,6K,12K.
We would first assign job1 (5K) to the 6K regions and job2 (2K) to the 2K regions.
Since ,our next job requires 3K,we need the 6K regins.Since 6K regions are being used by
job1,we must wait until job1 terminates ,then job3 will be allocated the 6K regions.
Job 4 then allocated the 12K regions and so on.
10
Fragmentation
11
Internal Fragmentation
• Internal fragmentation occurs when a process is assigned more memory than it has
requested and the wasted memory fragment is internal to process.
• As shown in the image above, the 100 MB partition is used to load only 80KB and
90KB process and the remaining 20 KB and 10KB got wasted.i.e. Internal
fragmentation
• As shown in the image above, the 4 MB partition is used to load only 3 MB process and
the remaining 1 MB got wasted.i.e. Internal fragmentation 12
External fragmentation
14
• Advantages of Fixed Partitioning –
1.Easy to implement:
Algorithms needed to implement Fixed Partitioning are easy to
implement. It simply requires putting a process into certain
partition without focussing on the emergence of Internal and
External Fragmentation.
2.Little OS overhead:
Processing of Fixed Partitioning require lesser OS overhead
15
Disadvantages of Fixed Partitioning –
Problem is How to determine the number of partitions
,and how to determine their sizes.
Problem of Internal fragmentation and External
fragmentation may occur.
Limitation on the size of the process
• If the process size is larger than the size of maximum
sized partition then that process cannot be loaded into
the memory. Therefore, a limitation can be imposed on
the process size that is it cannot be larger than the size
of the largest partition.
16
Disadvantages of Fixed Partitioning –
17
Thank You
18
Operating System
Chapter 5
Memory
Management
MRS A.S.KHANDAGALE
1
2
Dynamic(Variable) Memory
Partitioning
It is a part of Contiguous allocation technique.
With fixed partitions there is problem of
• determining the number and sizes of partitions
• internal and external fragmentation problem’
To minimize above problems variable or Dynamic partitioning is
used in which partition sizes may vary dynamically
The operating system keeps a table indicating which parts of
memory are available and which are occupied.
Initially all memory is available for user programs and is considered
as one large block of available memory,a hole.
When a job arrives and needs memory,we search for hole large
enough for this job.
If we find one ,we allocate only as much as is needed,keeping the
rest available to satisfy future requests.
3
• For example assume 256 K memory available and a resident
monitor of 40K.This situation leaves 216 for user programs
Monitor/OS (0 TO
Job Queue
39K)=40k
Job Memory Time
1 60K 10
2 100K 5
User programs(40k 3 30K 20
to 255k)=256k 4 70K 8
216k 5 50K 15
4
• Given the job queue in figure and FCFS scheduling ,
we can immediately allocate
memory to job1,job2 and job3
creating memory map.
6
Problem of Internal
Fragmentation is
removed.
And
And problem of
determining the number
and sizes of partitions is
reduced
7
Advantages of Variable Partitioning –
9
Dynamic Storage allocation
• Disk space can be viewed as a large array of disk
blocks,At any given time some of these blocks are
allocated to files and others are free
• Disk space is collection of free and used segments.
• Each segment is a contiguous set of disk blocks.
• An unallocated segment is called a Hole
• The Dynamic storage allocation problem is how to satisfy
a request of size ‘n’ from list of free holes
• The set of holes is searched to determine which hole is
best to allocate.
10
The most common strategies used to select a free hole from set of available
holes are first fit, best fit , worst fit.
1 ) First Fit :-
• Allocate the first hole (or free block) that is big enough for the
new process.
• Searching can start either at the beginning of the set of holes or
here the previous first fit search ended.
• We can stop searching as soon as We find large enough free hole
• It finishes after finding the first suitable free partition.
• First fit is generally faster
11
2 ) Best Fit :-
• Allocate the smallest hole that is big enough .
• We search the entire list ,unless the list is kept ordered by size.
• This strategy produces the smallest left over hole.
3 ) Worst Fit :-
• Allocates the largest hole.
Thus
The First fit and Best fit are better than worst fit in both time and
storage utilization.
First fit is generally faster
12
First fit algorithm allocates 17 k
,first free block from list of free
blocks enough to fulfil request
of 13k.
13
Compaction
14
Compaction
• Comapaction is method used to overcome the external
fragmentation problem
• All free blocks are brought together as one large block of
free space,
• Compaction requires dynamic relocation.
• Compaction is process of collecting all free holes and
forms a single big free hole
• The goal of compaction is to shuffle the memory contents
to place all free memory spaces together in one large
block.
15
Factors Multiprogramming With Fixed (static) Multiprogramming With
Memory partitioning Variable(Dynamic)
Memory partitioning
1.Region Size In this method memory is divided Region or partition size is not fixed ,It
into several partitions of fixed size can vary dynamically
that size never changes
2.Process Size Can not grow at runtime Can grow or shrink at run time
16
Thank You
17
Operating System
Chapter 5
Memory
Management
MRS A.S.KHANDAGALE
1
2
Free space management techniques
• Files are created and deleted frequently during the operation of computer
system.Since ,there is only a limited amount of disk space,it is necessary to reuse the
space from deleted files for new files.
• To keep track of free disk space,the file system maintains a free space list.The free
space list records all dik blocks ,which are free.
• To create a file,we search the free space list for the required amount of space and
allocate it to the new file.This space is removed from the free space list.When a file
is deleted ,its disk space is added to the free space list.
3
1) Bit Vector or Bitmap
A Bitmap or Bit Vector is series or collection of bits where each
bit corresponds to a disk block.
The bit can take two values: 0 and 1
• 0 indicates that the block is allocated and
• 1 indicates a free block.
4
The given instance of disk blocks
on the disk in Figure 1 (where
green blocks are allocated) can
be represented by a bitmap of 16
bits as:
0000111000000110.
6
Disadvantages:
• This technique requires a special hardware support to find the
first 1 in a word it is not 0.
• This technique is not useful for the larger disks.
• This technique requires extra space to store bitmap.
7
2) Linked list
• This is another technique for free space management.
• In this linked list of all the free block is maintained.
• In this, there is a head pointer which points the first free
block of the list which is kept in a special location on the
disk.
• This block contains the pointer to the next block and the
next block contain the pointer of another next and this
process is repeated.
8
In this first free block contains a pointer
to the next free disk block and so
on.Here pointer to first free block is to
block2
9
• Advantages:
Whenever a file is to be allocated a free block, the operating
system can simply allocate the first block in free space list and
move the head pointer to the next free block in the list.
No disk fragmentation as every block is utilized
• Disadvantages:
Searching the free space list will be very time consuming; each
block will have to be read from the disk, which is read very
slowly as compared to the main memory.
Not Efficient for faster access.
Pointers used here will consume disk space,Additional memory
is required therefore.
10
Thank You
11
Operating System
Chapter 5
Memory
Management
MRS A.S.KHANDAGALE
1
2
Virtual Memory
• A computer can address more memory than the amount
physically installed on the system. This extra memory is actually
called virtual memory and it is a section of a hard disk that's
set up to emulate the computer's RAM.
• The main visible advantage of this scheme is that programs can
be larger than physical memory. Virtual memory serves two
purposes.
First, it allows us to extend the use of physical memory by
using disk.
Second, it allows us to have memory protection, because each
virtual address is translated to a physical address.
3
Virtual memory
• Virtual memory is a technique which allows the execution
of processes they may not be completely in main
memory.
• Virtual memory makes programming much easier as
programmer no longer needs to worry about amount of
physical memory available.
• Virtual memory is implemented by demand paging and
segmentation.
• Allows the programs that can be larger than the physical
memory
4
• Virtual memory is separation of user logical memory from
physical memory.This separation allows large virtual
memory to be provided for programmers when small
physical memory is available.
5
6
Paging
• Paging is a memory management scheme that eliminates the
need for contiguous allocation of physical memory.
• This scheme permits the physical address space of a process
to be non – contiguous. i.e It allocate a program to be allocated
physical memory wherever it is available
• Paging is a fixed size partitioning scheme.
• In paging, secondary memory and main memory are divided
into equal fixed size partitions.
• The partitions of secondary memory are called as pages.
• The partitions of main memory are called as frames.
7
Physical Memory
Logical Memory
8
Physical(primary/RAM) memory is broken
into fixed size blocks called frames.
Logical(secondary)is also broken into blocks
of same size called pages.
When a program is to be executed ,its pages
are loaded into any available frames and the
page table is defined to translate from user
pages to memory frames.
Every address generated by the CPU is
divided into two parts:- a page number(p) and
a page offset(d).
The page number is used as index into a page
table contains base address of each page in
physical memory(Frame no).
This base address is combined with page
offset to define the physical address is sent to
memory unit 9
For example using page size of 4 words and
physical memory of 32 words
12
DisAdvantages of Paging
13
Thank You
14
Operating System
Chapter 5
Memory
Management
MRS A.S.KHANDAGALE
1
2
Demand paging
• Consider an executable program might be loaded from disk
into memory.When we want to execute a program ,we swap
it into memory. Rather than swapping the entire process into
memory, with demand-paged virtual memory,pages that are
never accessed are thus never loaded into physical memory.
• A demand paging system is similar to a paging system with
swapping,where processes reside in secondary
memory(usually a disk).
• When we want to execute a process,we swap it into
memory.Rather than swapping the entire process into
memory,however e use a lazy swapper called pager.
• A lazy swapper never swaps a page into memory unless that
page will be needed.
3
• When a process is to be swapped in , the pager guesses
which pages will be used before the process is swapped
out again.
• Instead of swapping in a whole process, the pager brings
only those necessary pages into memory. Thus it avoids
reading into memory pages that will not be used in
anyway, decreasing the sap time and the amount of
physical memory needed.
• A swapper manipulates entire processes, whereas a
pager is concerned with the individual pages of a
process. We thus use pager ,rather than swapper, in
connection with demand paging.
4
5
Advantages of Demand Paging
• Can run program larger than physical memory.
• Large virtual memory.
• More efficient use of memory.
• Unconstrained multiprogramming. There is no limit on degree of
multiprogramming.
• Portions of processes that are never called are never loaded
• No compaction is required.
• Easy to share the pages
6
Disadvantages of Demand Paging
• Table handling overhead
• processor overhead for handling page interrupts are
greater than in the case of the simple paged
management techniques.
• High cost of the complex hardware required to perform
the parallel searches
• Many system uses associative memory to speed up the
process.
7
Segmentation-Characteristics
Like Paging, Segmentation is another non-contiguous memory
allocation technique.
In segmentation, process is not divided blindly into fixed size
pages.
Rather, the process is divided into modules for better
visualization.
Segmentation is a variable size partitioning scheme.
In segmentation, secondary memory and main memory are
divided into partitions of unequal size.
The size of partitions depend on the length of modules.
The partitions of primary memory are called as segments. 8
Segmentation
A process is divided into Segments.
The chunks that a program is divided into which are not necessarily
all of the same sizes are called segments.
Segmentation gives user’s view of the process which paging does
not give. Here the user’s view is mapped to physical memory.
A segment is a logical unit such as main program,procedure,function
,object,global variables,local variables ,stack,array and so on.
A table stores the information about all such segments and is called
Segment Table.
9
• Consider a program is divided into 5 segments as-
10
• Segment Table – It maps two-dimensional Logical address into
one-dimensional Physical address. It’s each table entry has:
• Base Address: It contains the starting physical address where
the segments reside in memory.
• Limit: It specifies the length of the segment.
11
In above figure we have 5 segments numbered from 0 through 4.The
segments are actually stored in physical memory.
The segment table has a separate entry for each segment in physical
memory (the base) and the length of that segment (the limit)
12
Address generated by the CPU is divided into:
•Segment number (s),Segment offset (d)
The segment number is used as an index into
segment table
Each entry of segment table has a segment
base and a segment limit..
The offset “d” of the logical address must
between 0 and segment limit.
If it is not , a trap is generated to the
operating system
For example segment2 is 400 words long,beginning
at location 1500.Thus reference to word 53 of
segment 2 is mapped on to location 1500 +53=1553
Here offset is within limit so no trap to OS
For example segment3 is 200 words long,beginning
at location 4600.Thus reference to word 350 of
segment 3 is mapped on to location 4600 +350=
4950. So reference to 350th word whose size is 200
long would result in trap to operating system
13
Advantages of Segmentation –
No Internal fragmentation.
Segment Table consumes less space in comparison to Page table
in paging.
It provides a convenient way of organizing programs and data to
the programmer.
Segmentation allows dynamically growing segments.
14
Disadvantage of Segmentation –
• As processes are loaded and removed from the memory, the
free memory space is broken into little pieces, causing External
fragmentation.
• Increased complexity in the operating system
15
BASIS FOR
COMPARISON PAGING SEGMENTATION
Basic A page is of fixed block size. A segment is of variable size.
Fragmentation Paging may lead to internal Segmentation may lead to
fragmentation. external fragmentation.
Address The user specified address is The user specifies each address by
divided by CPU into a page two quantities a segment number
number and offset. and the offset (Segment limit).
16
Thank You
17
Operating System
Chapter 5
Memory
Management
MRS A.S.KHANDAGALE
1
2
Paging
Paging is a non-contiguous memory allocation technique.
In a paging scheme, a process is divided into several pages.
The pages are then stored in different frames of the main memory.
The basic idea behind paging is that when a process is swapped in,the
pager only loads into memory those pages that it expects the process
to need.
Paging permits a program memory to be non contiguous,thus allowing
a program to be allocated physical memory wherever it is available.
It is possible that not all pages of the program are brought into
memory.some pages are loaded into memory and some pages are
kept on the disk.
3
Page Fault-
When a page referenced by the CPU is not found in the main
memory, it is called as a page fault.
When a page fault occurs, the required page has to be fetched
from the secondary memory into the main memory.
To distinguish between the pages that are in memory and the
pages that are on the disk,a valid-invalid bit is provided.
Pages that are not loaded into memory are marked as invalid in
the page table using the invalid bit.
The bit is set to valid if the associated page is in memory.
4
Page Replacement Algorithms
• When a page fault occurs,the operating system has to choose
a page to remove from memory to make room for the page
that has to be brought in.This is known as page replacement.
• The approach of page replacement is ,if no frame is free,we
find one that is not currently being used and free it.
• Page replacement algorithm works on the string of memory
references called as reference string.
• It also needs the available number of page frames.
• Then the number of page faults determined for a particular
reference string.
5
Page Replacement algorithms are
• FIFO – (First in First Out)
• Optimal Page Replacement (OPU)
• Least Recently Used(LRU)
6
FIFO (First In First Out)
• The simplest page replacement algorithm is FIFO.
• A FIFO associates with each page time when that page
was brought into memory
• It replaces the oldest page that has been present in the
main memory for the longest time.
• It is implemented by keeping track of all the pages in a
queue.
7
• Example-1Consider page reference string 1, 3, 0, 3, 5, 6 with 3
page frames.Find number of page faults.
Initially all slots are empty, so when 1, 3, 0 came
they are allocated to the empty slots —> 3 Page
Faults.
8
Belady’s anomaly – Belady’s anomaly proves that it is
possible to have more page faults when increasing the number
of page frames while using the First in First Out (FIFO) page
replacement algorithm.
For example, if we consider reference string 3, 2, 1, 0, 3, 2, 4,
3, 2, 1, 0, 4 and 3 slots, we get 9 total page faults, but if we
increase slots to 4, we get 10 page faults.
9
FIFO Page Replacement Algorithm
10
Advantages of FIFO page replacement algorithm
1.It is simple and easy to understand & implement.
12
Operating System
Chapter 5
Memory
Management
MRS A.S.KHANDAGALE
1
2
Optimal page-replacement algorithm
(Forward Check)
• An optimal page-replacement algorithm has the lowest
page-fault rate of all algorithms.
• This algorithm would never suffer from Belady’s
anomaly.
• In this algorithm, pages are replaced which would not
be used for the longest duration of time in the
future.(Forward Check).
• Use of this algorithm guarantees lowest page fault rate
• It is difficult to implement because of future reference
3
4
• Example-2:Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2,
with 4 page frame. Find number of page fault.
5
Advantages of Optimal page-replacement algorithm
It is the best possible optimal algorithm
It gives the smallest number of page faults.
It never suffers from Belady’s Anomaly.
6
Thank You
7
Operating System
Chapter 5
Memory
Management
MRS A.S.KHANDAGALE
1
2
Least Recently Used (LRU) Page Replacement
Algorithm (backward direction check)
3
4
Advantages of Least Recently Used (LRU) Page Replacement
Algorithm
LRU is actually quite good algorithm
It never suffers from Belady’s Anomaly.
It is very feasible to implement.
5
Consider a reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7, 2. the number of frames in the memory is 3.
Find out the number of page faults respective to:
1. FIFO Page Replacement Algorithm Optimal Page Replacement Algorithm
2. Optimal Page Replacement Algorithm
3. LRU Page Replacement Algorithm
FIFO Page Replacement Algorithm
7
LRU Page Replacement Algorithm
8
Thank You