You are on page 1of 63

Unit-5

Memory Management

P.Sindhu
Asst.Professor©

Dept.of CSE

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Contents
• Partitioning
• Paging & Segmentation & Space Allocation
• Page Replacement Algorithms
• Analysis of page allocation policies
– Virtual memory
– Demand Paging

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Introduction
• 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.
• All the programs are loaded in the main memory for execution.
Sometimes complete program is loaded into the memory, but some times
a certain part or routine of the program is loaded into the main memory
only when it is called by the program, this mechanism is called Dynamic
Loading, this enhance the performance.
• Also, at times one program is dependent on some other program. In such
a case, rather than loading all the dependent programs, CPU links the
dependent programs to the main executing program when its required.
This mechanism is known as Dynamic Linking.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Swapping:-
• A process needs to be in memory for execution. But sometimes there is not enough
main memory to hold all the currently active processes in a timesharing system.
• So, excess process are kept on disk and brought in to run dynamically. Swapping is the
process of bringing in each process in main memory, running it for a while and then
putting it back to the disk.
Contiguous Memory Allocation:-
• In contiguous memory allocation each process is contained in a single contiguous
block of memory. Memory is divided into several fixed size partitions. Each
partition contains exactly one process.
• When a partition is free, a process is selected from the input queue and loaded
into it.
• The free blocks of memory are known as holes. The set of holes is searched to
determine which hole is best to allocate.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Memory Protection:-
• Memory protection is a phenomenon by which we control memory access rights on a computer. The main
aim of it is to prevent a process from accessing memory that has not been allocated to it.
• Hence prevents a bug within a process from affecting other processes, or the operating system itself, and
instead results in a segmentation fault or storage violation exception being sent to the disturbing process,
generally killing of process.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Partition Allocation (Memory Allocation) Strategies

• As available memory blocks comprised of a set of holes of various sizes that


scattered throughout the memory. Whenever a process arrives and needs
memory, the system searches the set for a hole that is large enough for the arrived
process.
• In case if the hole is too large then it splits the hole into two parts. One part is
allocated to the arrived process and the other part is returned to the set of holes.
• Whenever the process terminates, it releases the block of the memory and the
released block is then placed back into the set of holes. If the new hole is adjacent
to other holes, these adjacent holes are then merged together to form a large
single hole.
• Now at this point system may need to check whether there are processes that are
waiting for memory and whether this newly freed and merged memory can satisfy
the demands of any process that is in the waiting queue.
• The above-given procedure is a particular instance of the general dynamic storage
allocation problem, which is concerned with how to satisfy a request that is of
size n from the list of free holes.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• 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:
• Fixed (or static) partitioning
• Variable (or dynamic) partitioning

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Contiguous Memory Allocation
• Fixed Partitioning:
This is the oldest and simplest technique used to
put more than one process in the main memory.
In this partitioning, the number of partitions
(non-overlapping) in RAM is fixed but the size of
each partition may or may not be the same. As it
is a contiguous allocation, hence no spanning is
allowed. Here partitions are made before
execution or during system configure.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


As illustrated in above figure, first process is only
consuming 1MB out of 4MB in the main memory.
Hence, Internal Fragmentation in first block is (4-1)
= 3MB.
Sum of Internal Fragmentation in every block = (4-
1)+(8-7)+(8-7)+(16-14)= 3+1+1+2 = 7MB.
Suppose process P5 of size 7MB comes. But this
process cannot be accommodated in spite of
available free space because of contiguous
allocation (as spanning is not allowed). Hence,
7MB becomes part of External Fragmentation.
There are some advantages and disadvantages of
fixed partitioning.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Advantages of Fixed Partitioning –
• Easy to implement:
Algorithms needed to implement Fixed Partitioning are easy to implement. It
simply requires putting a process into a certain partition without focusing on the
emergence of Internal and External Fragmentation.

• Little OS overhead:
Processing of Fixed Partitioning requires lesser excess and indirect computational
power.

Disadvantages of Fixed Partitioning –


• Internal Fragmentation:
Main memory use is inefficient. Any program, no matter how small, occupies an
entire partition. This can cause internal fragmentation.

• External Fragmentation:
The total unused space (as stated above) of various partitions cannot be used to
load the processes even though there is space available but not in the contiguous
form (as spanning is not allowed).
P.SINDHU ASST.PROFESSOR(C),CSE DEPT
• Limit process size:
Process of size greater than the size of the partition in Main
Memory cannot be accommodated. The partition size cannot be
varied according to the size of the incoming process size. Hence, the
process size of 32MB in the above-stated example is invalid.

• Limitation on Degree of Multiprogramming:


Partitions in Main Memory are made before execution or during
system configure. Main Memory is divided into a fixed number of
partitions. Suppose if there are partitions in RAM and are the
number of processes, then condition must be fulfilled. Number of
processes greater than the number of partitions in RAM is invalid in
Fixed Partitioning.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• Variable Partitioning –
It is a part of Contiguous allocation technique. It is used to alleviate the
problem faced by Fixed Partitioning. In contrast with fixed partitioning,
partitions are not made before the execution or during system configure.
Various features associated with variable Partitioning-
• Initially RAM is empty and partitions are made during the run-time
according to process’s need instead of partitioning during system
configure.
• The size of partition will be equal to incoming process.
• The partition size varies according to the need of the process so that the
internal fragmentation can be avoided to ensure efficient utilization of
RAM.
• Number of partitions in RAM is not fixed and depends on the number of
incoming process and Main Memory’s size.
• There are some advantages and disadvantages of variable partitioning
over fixed partitioning as given below.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


P.SINDHU ASST.PROFESSOR(C),CSE DEPT
Advantages of Variable Partitioning –
• No Internal Fragmentation:
In variable Partitioning, space in main memory is allocated strictly according to the
need of process, hence there is no case of internal fragmentation. There will be no
unused space left in the partition.
• No restriction on Degree of Multiprogramming:
More number of processes can be accommodated due to absence of internal
fragmentation. A process can be loaded until the memory is empty.
• No Limitation on the size of the process:
In Fixed partitioning, the process with the size greater than the size of the largest
partition could not be loaded and process can not be divided as it is invalid in
contiguous allocation technique. Here, In variable partitioning, the process size
can’t be restricted since the partition size is decided according to the process size.
Disadvantages of Variable Partitioning –
• Difficult Implementation:
Implementing variable Partitioning is difficult as compared to Fixed Partitioning as
it involves allocation of memory during run-time rather than during system
configure.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• External Fragmentation:
There will be external fragmentation in spite of absence of
internal fragmentation. For example, suppose in above
example- process P1(2MB) and process P3(1MB) completed
their execution. Hence two spaces are left i.e. 2MB and
1MB. Let’s suppose process P5 of size 3MB comes. The
empty space in memory cannot be allocated as no
spanning is allowed in contiguous allocation. The rule says
that process must be contiguously present in main memory
to get executed. Hence it results in External Fragmentation.
• Now P5 of size 3 MB cannot be accommodated in spite of
required available space because in contiguous no spanning
is allowed.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


P.SINDHU ASST.PROFESSOR(C),CSE DEPT
Non-Contiguous Memory Allocation
• In non-contiguous allocation, Operating system needs to
maintain the table which is called Page Table for each
process which contains the base address of the each block
which is acquired by the process in memory space.
• In non-contiguous memory allocation, different parts of a
process is allocated different places in Main Memory.
Spanning is allowed which is not possible in other
techniques like Dynamic or Static Contiguous memory
allocation.
• That’s why paging is needed to ensure effective memory
allocation. Paging is done to remove External
Fragmentation.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• Working:
Here a process can be spanned across different spaces in main memory in
non-consecutive manner. Suppose process P of size 4KB. Consider main
memory have two empty slots each of size 2KB. Hence total free space is,
2*2= 4 KB.
• In contiguous memory allocation, process P cannot be accommodated as
spanning is not allowed.
• In contiguous allocation, space in memory should be allocated to whole
process. If not, then that space remains unallocated. But in Non-
Contiguous allocation, process can be divided into different parts and
hence filling the space in main memory.
• In this example, process P can be divided into two parts of equal size –
2KB. Hence one part of process P can be allocated to first 2KB space of
main memory and other part of process P can be allocated to second 2KB
space of main memory. Below diagram will explain in better way:

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


P.SINDHU ASST.PROFESSOR(C),CSE DEPT
• But, in what manner we divide a process to allocate them into main
memory is very important to understand. Process is divided after
analyzing the number of empty spaces and their size in main memory.
Then only we divide our process. It is very time consuming process.
• Their number as well as their sizes changing every time due to execution
of already present processes in main memory.
• In order to avoid this time consuming process, we divide our process in
secondary memory in advance before reaching the main memory for its
execution.
• Every process is divided into various parts of equal size called Pages. We
also divide our main memory into different parts of equal size called
Frames. It is important to understand that:
• Size of page in process = Size of frame in memory
• Although their numbers can be different. Below diagram will make you
understand in better way: consider empty main memory having size of
each frame is 2 KB, and two processes P1 and P2 are 2 KB each.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


P.SINDHU ASST.PROFESSOR(C),CSE DEPT
• Resolvent main memory,

In conclusion we can say that, Paging allows


memory address space of a process to be non-
contiguous. Paging is more flexible as only
pages of a process are moved. It allows more
processes to reside in main memory than
Contiguous memory allocation.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Memory Allocation:-
• Memory allocation is a process by which computer programs are assigned memory or
space. It is of three types :
• First Fit: The first hole that is big enough is allocated to program.
• Best Fit: The smallest hole that is big enough is allocated to program.
• Worst Fit: The largest hole that is big enough is allocated to program.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


First Fit Allocation

• According to this strategy, allocate the first hole or first free partition to the process that is
big enough. This searching can start either from the beginning of the set of holes or from the
location where the previous first-fit search ended.
• Searching can be stopped as soon as we find a free hole that is large enough.
• Let us take a look at the example given below:
• Process P1 of size 10KB has arrived and then the first hole that is enough to meet the
requirements of size 20KB is chosen and allocated to the process.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Best Fit Allocation

• With this strategy, the smallest free partition/ hole that is big enough and meets the
requirements of the process is allocated to the process. This strategy searches the entire list
of free partitions/holes in order to find a hole whose size is either greater than or equal to
the size of the process.
• Let us take a look at the example given below:
• Process P1 of size 10KB is arrived and then the smallest hole to meet the requirements of size 10 KB is

chosen and allocated to the process .

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Worst Fit Allocation

• With this strategy, the Largest free partition/ hole that meets the requirements of the
process is allocated to the process. It is done so that the portion that is left is big enough to
be useful. This strategy is just the opposite of Worst Fit.
• This strategy searches the entire list of holes in order to find the largest hole and then
allocate the largest hole to process.
• Let us take a look at the example given below:
• Process P1 of size 10KB has arrived and then the largest hole of size 80 KB is chosen and
allocated to the process.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Next Fit Allocation

• This strategy is the modified version of the First fit because in Next Fit and in this
memory is searched for empty spaces similar to the first fit memory allocation
scheme. But it differs from the first fit as when called Next time it starts from
where it let off and not from the beginning.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Paging

• Paging permits the physical address space of a process to be non-contiguous. It is


a fixed-size partitioning scheme. In the Paging technique, the secondary memory
and main memory are divided into equal fixed-size partitions.
• Paging solves the problem of fitting memory chunks of varying sizes onto the
backing store and this problem is suffered by many memory management
schemes.
• Paging helps to avoid external fragmentation and the need for compaction.
Basic Method of Paging
• The paging technique divides the physical memory(main memory) into fixed-
size blocks that are known as Frames and also divide the logical
memory(secondary memory) into blocks of the same size that are known
as Pages.
• This technique keeps the track of all the free frames.
• The Frame has the same size as that of a Page. A frame is basically a place
where a (logical) page can be (physically) placed.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


•Each process is mainly divided into parts where
the size of each part is the same as the page size.
There is a possibility that the size of the last part
may be less than the page size.
•Pages of a process are brought into the main
memory only when there is a requirement
otherwise they reside in the secondary storage.
One page of a process is mainly stored in one of
the frames of the memory. Also, the pages can be
stored at different locations of the memory but
always the main priority is to find contiguous
frames.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Translation of Logical Address into Physical Address
Before moving on further there are some important points to note:
• The CPU always generates a logical address.
• In order to access the main memory always a physical address is needed.
• The logical address generated by CPU always consists of two parts:
– Page Number(p)
– Page Offset (d)
where,
• Page Number is used to specify the specific page of the process from which
the CPU wants to read the data. and it is also used as an index to the page
table.
• and Page offset is mainly used to specify the specific word on the page that
the CPU wants to read.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Page Table in OS
• The Page table mainly contains the base address of each page in the Physical memory. The
base address is then combined with the page offset in order to define the physical memory
address which is then sent to the memory unit.
• Thus page table mainly provides the corresponding frame number (base address of the
frame) where that page is stored in the main memory.
• As we have told you above that the frame number is combined with the page offset and
forms the required physical address.
• So, The physical address consists of two parts:
– Page offset(d)
– Frame Number(f)
where,
• The Frame number is used to indicate the specific frame where the required page is stored.
• and Page Offset indicates the specific word that has to be read from that page.
• The Page size (like the frame size) is defined with the help of hardware. It is important to note
here that the size of the page is typically the power of 2 that varies between 512 bytes and
16 MB per page and it mainly depends on the architecture of the computer.
• If the size of logical address space is 2 raised to the power m and page size is 2 raised to the
power n addressing units then the high order m-n bits of logical address designates the page
number and the n low-order bits designate the page offset.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


P.SINDHU ASST.PROFESSOR(C),CSE DEPT
• The above diagram indicates the translation of the Logical address into the Physical
address. The PTBR in the above diagram means page table base register and it
basically holds the base address for the page table of the current process.
• The PTBR is mainly a processor register and is managed by the operating system.
Commonly, each process running on a processor needs its own logical address
space.
• But there is a problem with this approach and that is with the time required to
access a user memory location. Suppose if we want to find the location i, we must
first find the index into the page table by using the value in the PTBR offset by the
page number for I. And this task requires memory access. It then provides us the
frame number which is combined with the page offset in order to produce the
actual address. After that, we can then access the desired place in the memory.
• With the above scheme, two memory accesses are needed in order to access a
byte( one for the page-table entry and one for byte). Thus memory access is
slower by a factor of 2 and in most cases, this scheme slowed by a factor of 2.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Translation of look-aside buffer(TLB)
• There is the standard solution for the above problem that is to use a special, small, and fast-lookup hardware
cache that is commonly known as Translation of look-aside buffer(TLB).
• TLB is associative and high-speed memory.
• Each entry in the TLB mainly consists of two parts: a key(that is the tag) and a value.
• When associative memory is presented with an item, then the item is compared with all keys simultaneously. In
case if the item is found then the corresponding value is returned.
• The search with TLB is fast though the hardware is expensive.
• The number of entries in the TLB is small and generally lies in between 64 and 1024.
• TLB is used with Page Tables in the following ways:
• The TLB contains only a few of the page-table entries. Whenever the logical address is generated by the CPU then
its page number is presented to the TLB.
• If the page number is found, then its frame number is immediately available and is used in order to access the
memory. The above whole task may take less than 10 percent longer than would if an unmapped memory
reference were used.
• In case if the page number is not in the TLB (which is known as TLB miss), then a memory reference to the Page
table must be made.
• When the frame number is obtained it can be used to access the memory. Additionally, page number and frame
number is added to the TLB so that they will be found quickly on the next reference.
• In case if the TLB is already full of entries then the Operating system must select one for replacement.
• TLB allows some entries to be wired down, which means they cannot be removed from the TLB. Typically TLB
entries for the kernel code are wired down.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


P.SINDHU ASST.PROFESSOR(C),CSE DEPT
Advantages of Paging
• Given below are some advantages of the Paging technique in the operating system:
• Paging mainly allows to storage of parts of a single process in a non-contiguous fashion.
• With the help of Paging, the problem of external fragmentation is solved.
• Paging is one of the simplest algorithms for memory management.
Disadvantages of Paging
• Disadvantages of the Paging technique are as follows:
• In Paging, sometimes the page table consumes more memory.
• Internal fragmentation is caused by this technique.
• There is an increase in time taken to fetch the instruction since now two memory accesses are
required.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Paging Hardware
• Every address generated by CPU mainly consists of two parts:
– Page Number(p)
– Page Offset (d)
where,
• Page Number is used as an index into the page table that generally contains the
base address of each page in the physical memory.
• Page offset is combined with base address in order to define the physical memory
address which is then sent to the memory unit.
• If the size of logical address space is 2 raised to the power m and page size is 2
raised to the power n addressing units then the high order m-n bits of logical
address designates the page number and the n low-order bits designate the page
offset.
• The logical address is as follows:

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


P.SINDHU ASST.PROFESSOR(C),CSE DEPT
Paging Example
• In order to implement paging, one of the simplest methods is to implement the page table as a set of
registers. As the size of registers is limited but the size of the page table is usually large thus page table is
kept in the main memory.
• There is no External fragmentation caused due to this scheme; Any frame that is free can be allocated to
any process that needs it. But the internal fragmentation is still there.
• If any process requires n pages then at least n frames are required.
• The first page of the process is loaded into the first frame that is listed on the free-frame list, and then
the frame number is put into the page table.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• The frame table is a data structure that keeps the information of which frames are allocated or which
frames are available and many more things. This table mainly has one entry for each physical page frame.

•The Operating system maintains


a copy of the page table for each
process in the same way as it
maintains the copy of the
instruction counter and register
contents. Also, this copy is used
to translate logical addresses to
physical addresses whenever the
operating system maps a logical
address to a physical address
manually.
•This copy is also used by the CPU
dispatcher in order to define the
hardware page table whenever a
process is to be allocated to the
CPU. P.SINDHU ASST.PROFESSOR(C),CSE DEPT
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.
There are types of segmentation:
• Virtual memory segmentation –
Each process is divided into a number of segments, not all of which are
resident at any one point in time.
• Simple segmentation –
Each process is divided into a number of segments, all of which are loaded
into memory at run time, though not necessarily contiguously.
• There is no simple relationship between logical addresses and physical
addresses in segmentation. A table stores the information about all such
segments and is called Segment Table.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


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

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• Translation of Two dimensional Logical Address to one dimensional
Physical Address.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Address generated by the CPU is divided into:
• Segment number (s): Number of bits required to represent the
segment.
• Segment offset (d): Number of bits required to represent the size
of the segment.
Advantages of Segmentation –
• No Internal fragmentation.
• Segment Table consumes less space in comparison to Page table in
paging.
Disadvantage of Segmentation –
• As processes are loaded and removed from the memory, the free
memory space is broken into little pieces, causing External
fragmentation.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Page Replacement Algorithms

• In an operating system that uses paging for memory management,


a page replacement algorithm is needed to decide which page
needs to be replaced when new page comes in.
• Page Fault – A page fault happens when a running program
accesses a memory page that is mapped into the virtual address
space, but not loaded in physical memory.
• Since actual physical memory is much smaller than virtual memory,
page faults happen. In case of page fault, Operating System might
have to replace one of the existing pages with the newly needed
page.
• Different page replacement algorithms suggest different ways to
decide which page to replace. The target for all algorithms is to
reduce the number of page faults.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


1. First In First Out (FIFO) –
This is the simplest page replacement algorithm. In this algorithm, the operating
system keeps track of all pages in the memory in a queue, the oldest page is in the
front of the queue. When a page needs to be replaced page in the front of the queue
is selected for removal.
Example-1Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames. Find
number of page faults.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• Initially all slots are empty, so when 1, 3, 0 came they are allocated
to the empty slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the
oldest page slot i.e 1. —>1 Page Fault.
6 comes, it is also not available in memory so it replaces the oldest
page slot i.e 3 —>1 Page Fault.
Finally when 3 come it is not available so it replaces 0 1 page fault
• 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.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


2. Optimal Page replacement –
In this algorithm, pages are replaced which would not be used for the longest duration of time in
the future.
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.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• Initially all slots are empty, so when 7 0 1 2 are allocated to the
empty slots —> 4 Page faults
0 is already there so —> 0 Page fault.
when 3 came it will take the place of 7 because it is not used for the
longest duration of time in the future.—>1 Page fault.
0 is already there so —> 0 Page fault..
4 will takes place of 1 —> 1 Page Fault.
• Now for the further page reference string —> 0 Page fault because
they are already available in the memory.
Optimal page replacement is perfect, but not possible in practice as
the operating system cannot know future requests. The use of
Optimal Page replacement is to set up a benchmark so that other
replacement algorithms can be analyzed against it.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


3. Least Recently Used –
In this algorithm page will be replaced which is least recently used.
Example-3Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4
page frames. Find number of page faults.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• Initially all slots are empty, so when 7 0 1 2 are
allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it
is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0
Page fault because they are already available in
the memory.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Virtual Memory
• Virtual Memory is a storage allocation scheme in which secondary
memory can be addressed as though it were part of the main
memory. The addresses a program may use to reference memory
are distinguished from the addresses the memory system uses to
identify physical storage sites, and program-generated addresses
are translated automatically to the corresponding machine
addresses.
• The size of virtual storage is limited by the addressing scheme of
the computer system and the amount of secondary memory is
available not by the actual number of the main storage locations.
• It is a technique that is implemented using both hardware and
software. It maps memory addresses used by a program, called
virtual addresses, into physical addresses in computer memory.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• All memory references within a process are logical addresses that
are dynamically translated into physical addresses at run time. This
means that a process can be swapped in and out of the main
memory such that it occupies different places in the main memory
at different times during the course of execution.
• A process may be broken into a number of pieces and these pieces
need not be continuously located in the main memory during
execution. The combination of dynamic run-time address
translation and use of page or segment table permits this.
• If these characteristics are present then, it is not necessary that all
the pages or segments are present in the main memory during
execution. This means that the required pages need to be loaded
into memory whenever required. Virtual memory is implemented
using Demand Paging or Demand Segmentation.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Demand Paging
The process of loading the page into memory on demand (whenever page fault occurs) is known
as demand paging.
• The process includes the following steps :

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• If the CPU tries to refer to a page that is currently not available in the main
memory, it generates an interrupt indicating a memory access fault.
• The OS puts the interrupted process in a blocking state. For the execution
to proceed the OS must bring the required page into the memory.
• The OS will search for the required page in the logical address space.
• The required page will be brought from logical address space to physical
address space. The page replacement algorithms are used for the
decision-making of replacing the page in physical address space.
• The page table will be updated accordingly.
• The signal will be sent to the CPU to continue the program execution and
it will place the process back into the ready state.
• Hence whenever a page fault occurs these steps are followed by the
operating system and the required page is brought into memory.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Advantages :
• More processes may be maintained in the main memory: Because
we are going to load only some of the pages of any particular
process, there is room for more processes. This leads to more
efficient utilization of the processor because it is more likely that at
least one of the more numerous processes will be in the ready state
at any particular time.
• A process may be larger than all of the main memory: One of the
most fundamental restrictions in programming is lifted. A process
larger than the main memory can be executed because of demand
paging. The OS itself loads pages of a process in the main memory
as required.
• It allows greater multiprogramming levels by using less of the
available (primary) memory for each process.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Page Fault Service Time :
The time taken to service the page fault is called
page fault service time. The page fault service
time includes the time taken to perform all the
above six steps.
• Let Main memory access time is: m
• Page fault service time is: s
• Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


Main Memory-
• Physical Address Space = Size of main memory
• Size of main memory = Total number of frames x Page
size
• Frame size = Page size
• If number of frames in main memory = 2X, then
number of bits in frame number = X bits
• If Page size = 2X Bytes, then number of bits in page
offset = X bits
• If size of main memory = 2X Bytes, then number of bits
in physical address = X bits

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


For Process-
• Virtual Address Space = Size of process
• Number of pages the process is divided =
Process size / Page size
• If process size = 2X bytes, then number of bits
in virtual address space = X bits

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


For Page Table-
• Size of page table = Number of entries in page
table x Page table entry size
• Number of entries in pages table = Number of
pages the process is divided
• Page table entry size = Number of bits in frame
number + Number of bits used for optional fields
if any

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


• In general, if the given address consists of ‘n’ bits,
then using ‘n’ bits, 2n locations are possible.
• Then, size of memory = 2n x Size of one location.
• If the memory is byte-addressable, then size of
one location = 1 byte.
• Thus, size of memory = 2n bytes.
• If the memory is word-addressable where 1 word
= m bytes, then size of one location = m bytes.
• Thus, size of memory = 2n x m bytes.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


This formula is valid only when there is single level paging and
there are no page faults.

P.SINDHU ASST.PROFESSOR(C),CSE DEPT


In a multilevel paging scheme using TLB with a possibility of page fault,
effective access time is given by-

P.SINDHU ASST.PROFESSOR(C),CSE DEPT

You might also like