0% found this document useful (0 votes)
29 views7 pages

Unit 4.1 Os

os

Uploaded by

singhaayushker
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)
29 views7 pages

Unit 4.1 Os

os

Uploaded by

singhaayushker
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/ 7

Unit-4

Memory Management

Memory Management in Operating System


In a multiprogramming computer, the operating system resides in a part of memory and the
rest is used by multiple processes. The task of subdividing the memory among different
processes is called memory management. Memory management is a method in the
operating system to manage operations between main memory and disk during process
execution. The main aim of memory management is to achieve efficient utilization of
memory.

Why Memory Management is required:

• Allocate and de-allocate memory before and after process execution.


• To keep track of used memory space by processes.
• To minimize fragmentation issues.
• To proper utilization of main memory.
• To maintain data integrity while executing of process.

Logical Address and Physical address


Logical Address is generated by CPU while a program is running. The logical address is
virtual address as it does not exist physically, therefore, it is also known as Virtual Address.
This address is used as a reference to access the physical memory location by CPU. The
term Logical Address Space is used for the set of all logical addresses generated by a
program’s perspective.
The hardware device called Memory-Management Unit is used for mapping logical address
to its corresponding physical address.
Page address is called logical address and represented by page number and the offset.
Logical Address = Page number + page offset

Physical Address identifies a physical location of required data in a memory. The user
never directly deals with the physical address but can access by its corresponding logical
address. The user program generates the logical address and thinks that the program is
running in this logical address but the program needs physical memory for its execution,
therefore, the logical address must be mapped to the physical address by MMU before they
are used. The term Physical Address Space is used for all physical addresses corresponding
to the logical addresses in a Logical address space.
Frame address is called physical address and represented by a frame number and the offset.
Physical Address = Frame number + page offset

Locality of reference
Locality of reference refers to a phenomenon in which a computer program tends to access
same set of memory locations for a particular time period. In other words, Locality of
Reference refers to the tendency of the computer program to access instructions whose
addresses are near one another. The property of locality of reference is mainly shown by
loops and subroutine calls in a program.

1. In case of loops in program control processing unit repeatedly refers to the set of
instructions that constitute the loop.
2. In case of subroutine calls, every time the set of instructions are fetched from
memory.
3. References to data items also get localized that means same data item is
referenced again and again.
in the above figure, you can see that the CPU wants to read or fetch the data or instruction.
First, it will access the cache memory as it is near to it and provides very fast access. If the
required data or instruction is found, it will be fetched. This situation is known as a cache
hit. But if the required data or instruction is not found in the cache memory then this
situation is known as a cache miss. Now the main memory will be searched for the required
data or instruction that was being searched and if found will go through one of the two
ways:
1. First way is that the CPU should fetch the required data or instruction and use it
and that’s it but what, when the same data or instruction is required again. CPU
again has to access the same main memory location for it and we already know
that main memory is the slowest to access.
2. The second way is to store the data or instruction in the cache memory so that if
it is needed soon again in the near future it could be fetched in a much faster
way.
Types of Locality :
1. Temporal Locality –
Temporal locality means current data or instruction that is being fetched may
be needed soon. So we should store that data or instruction in the cache
memory so that we can avoid again searching in main memory for the same
data.

When CPU accesses the current main memory location for reading required data
or instruction, it also gets stored in the cache memory which is based on the fact
that same data or instruction may be needed in near future. This is known as
temporal locality. If some data is referenced, then there is a high probability that
it will be referenced again in the near future.
2. Spatial Locality –
Spatial locality means instruction or data near to the current memory
location that is being fetched, may be needed soon in the near future. This is
slightly different from the temporal locality. Here we are talking about
nearly located memory locations while in temporal locality we were talking
about the actual memory location that was being fetched.

Page Replacement Algorithms


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.
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, 3 with 3 page frames. Find
the 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.
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.
Bélády’s anomaly is the name given to the phenomenon where increasing the number of
page frames results in an increase in the number of page faults for a given memory access
pattern.
Example: Assuming a system that has no pages loaded in the memory and uses the FIFO
Page replacement algorithm. Consider the following reference string:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Case-1: If the system has 3 frames, the given reference string the using FIFO page
replacement algorithm yields a total of 9 page faults. The diagram below illustrates the
pattern of the page faults occurring in the example.

Case-2: If the system has 4 frames, the given reference string using the FIFO page
replacement algorithm yields a total of 10 page faults. The diagram below illustrates the
pattern of the page faults occurring in the example.

It can be seen from the above example that on increasing the number of frames while using
the FIFO page replacement algorithm, the number of page faults increased from 9 to 10.
Note – It is not necessary that every string reference pattern cause Belady anomaly in FIFO
but there is certain kind of string references that worsen the FIFO performance on
increasing the number of frames.
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.
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 analysed against it.
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.

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.
Overlay system
The main problem in Fixed partitioning is the size of a process has to be limited by the
maximum size of the partition, which means a process can never be span over another. In
order to solve this problem, earlier people have used some solution which is called as
Overlays.
The concept of overlays is that whenever a process is running it will not use the complete
program at the same time, it will use only some part of it. Then overlays concept says that
whatever part you required, you load it and once the part is done, then you just unload it,
means just pull it back and get the new part you required and run it.
Formally,
“The process of transferring a block of program code or other data into internal memory,
replacing what is already stored”.
Sometimes it happens that compare to the size of the biggest partition, the size of the
program will be even more, then, in that case, you should go with overlays.
So overlay is a technique to run a program that is bigger than the size of the physical
memory by keeping only those instructions and data that are needed at any given time.
Divide the program into modules in such a way that not all modules need to be in the
memory at the same time.

You might also like