Os Ut3 N SCH
Os Ut3 N SCH
MEMORY MANAGEMENT
CHAPTER 1
BASIC OF MEMORY MANAGEMENT
Definition:
Memory management is process of managing the primary memory
This is done by the memory management modules of the operating system
called memory manager.
Functions of Memory management as follows
• Keeping track of the status of each location of the memory.
• In Multiprogramming system it decides which process gets memory first.
• Allocation of memory.
• Getting back the memory when the user finishes the job.
• Protection of program in memory from interference by other programs.
Logical
Relocation Value Main memory
+
CPU Register
Address Physical
Address
Memory allocation
• Memory allocation is defined as a process of assigning blocks of memory
to processes an request by the operating system.
Types of memory allocation
1. Contiguous memory allocation
2. Non contiguous memory allocation
During execution:
• Protection must be given to avoid user process from accessing the
operating system.
• Protection must be given to protect the processes from one another.
Logical
Addressing error
Example:
Logical address ➔346
Relocation registers ➔1000
Relocation register + logical address ➔physical address
Physical address ➔1346
Types:
1. Single partition allocation
2. Fixed sized partition allocation
3. Variable sized partitioned allocation
Types of fragmentation:
Internal fragmentation
• Internal fragmentation is wastage of memory space inside a fixed partition.
• It happens when a process of size less than the partition size in fixed
memory allocation.
PROCESS MEMORY
SIZE
P1 600K
P2 1000K
P3 300K
P4 700K
P5 500K
External fragmentation
Inability to use the free memory space created during variable partition
allocation. It is because the available free space is not contiguous.
Compaction:
It a process of moving the contents of all allocated partitions in variable
partition in one side so that they become contiguous and all free memory
space on the other side so they become contiguous.
Example:
Process Memory
size
P1 600k
P2 1000k
P3 300k
P4 700k
P5 500k
• As the fig (b) the memory has three processes p1, p4, p3 and two holes of
size 300k and 260k.
• After compaction all the free holes are compacted so that a free memory
space of 560k is available.
• Now the process p5 of size 500k can be loaded into the memory.
DEMERITS:
• Relocation hardware increases the cost of computer and slows down the
speed
• Compaction time is high
• Some memory area is still unused.
Paging:
• Paging is a memory management technique.
• In this memory management there is no problem of fragmentation
This technique uses the following principle:
• Each user process is divided into equal pieces called pages.
• The physical memory is divided into equal pieces of same size as pages
called frames
• By using a special hardware called page map table any page can be
placed in any frame.
Principle of Operation:
Let the size of the memory be 16k. In which 2k is allocated for OS.
The remaining area is divided into equal size pages.
we are using 1k pages the processes
Example:
Let the size of the memory be 16k. In which 2k is allocated for OS.
The remaining area is divided into equal size pages.
Processes are divided into pages
process P1 is divided into 4 pages
Process P2 is divided into 3 pages
process P3 is divided into 2 pages
Using page map table the pages can be placed in any frame
Let S be the size of the given process and P be the size of each page
It process requires
n =|S/P| pages
To load the above process into physical memory we need n frames.
The efficiency of the page allocation depends on the quick selection of n free
frames.
Free frames are randomly distributed in the physical memory.
Address translation:
The process of converting the logical address to physical address is called
translation.
Paging
• Paging is a memory management technique.
• In this memory management there is no problem of fragmentation
This technique uses the following principle:
• Each user process is divided into equal pieces called pages.
• The physical memory is divided into equal pieces of same size as pages
called frames
• By using a special hardware called page map table any page can be
placed in any frame.
Logical address generated by CPU has two parts:
Page number p and Page offset d
The value of d is combined with F to find the actual physical address .
The logical address = 01101011 Page number p = 011 page offset d
= 01011
Address translation:
Example:
Logical address generated by CPU has two parts:
Page number p
Page offset d
Page number p gives the page number and the offset d gives the displacement
within the page.
During execution,
The address translation has to find the corresponding frame F for given page no
p.
The value of d is combined with F to find the actual physical address of the
requested instruction by the cpu.
Normally computer use 8 bit or 16 bit or 32 bit etc addressing. So the page
number p and offset d are to be separated from this logical address.
Example:
Let Logical address be 256k ie 28k
Let Size of the page be 32k ie 25k
Let The size of addressing be 8 bits
Therefore Page number (p) = m-n= 8-5 =3 bits.
Page offset(d) = 5 bits
Therefore
The location 0 to 31 are in page 0
The location 32 to 63 are in page 1
The location 63 to 95 are in page 2
The location 95 to 128 are in page 3
Working:
The cpu generated address consists of two parts
1. Page number(p)
2. Page offset(d)
Page number:
It gives the page number requested by cpu.
Page offset:
• It contains the position of the requested instruction in the page.
• The page number is checked with the TLB entries.
• If that page is present in TLB, the corresponding physical address is
calculated using the frame number and offset.
• If the particular page is not in TLB, the page number is checked with
PMTLR.
• If it is a valid page number, the corresponding frame number is found
out from PMT.
• Using frame number and offset the physical address is calculated.
PROTECTION:
Protection is the process of protecting memory from unauthorized
access.
Sharing
• Sharing is the process of mapping a page with many other processes.
• This is achieved by keeping only one copy of that process in physical
memory and adding a special entry named ‘shared’ in PMT.
• If process P1 and P2 is in need of process P3 then process P3 is declared to
be shared.
Advantage:
1. Eliminates fragmentation.
2. It supports multiprogramming.
3. It increases the memory and processor utilization
4. Relocation overhead is eliminated.
Disadvantage:
1. PMT hardware increases the cost of the computer.
2. Inefficient usage of memory.
3. Processor time is wasted in maintaining and updating PMT.
Basic principle:
Programs size (Virtual Address Space) can exceed the available main
memory.
Implementation
• During process execution, required sections are only loaded in main
memory.
• The OS takes care of
1. Which section to bring into memory
2. When to bring the section into memory
3. Where to place the sections in the memory.
Frame no
Page no (P) Status
(F)
0 0 or 1
1 0 or 1
2 0 or 1
m-1 0 or 1
Demand paging:
It is a memory management technique. The process of moving the pages
from secondary memory to main memory on demand by CPU is called Demand
Paging.
Concepts:
• Each user process is divided into equal pieces called pages.
• Physical memory is divided into equal pieces of same size as pages
called frames
• Then by using a special hardware called page map table any page can be
placed in any frame depending on the demand by cpu.
0 0
1000
1500 LOAD 1 , 7200 1
2000
3000 2
4000 4
5000 5
6000 6 0
Operating
7000 1 system
7200 007000 2
8000 7 3 2
9000 8 4 3
10000 9 5 4
Logical address Main memory
space
Example
✓ Consider a process of size 10k (10x1 k size) stored in secondary
memory with logical address space.
✓ Let the size of main memory be 5k (5x1 k size) in which OS occupies 2k
space.
✓ The status of loaded page & location of page in main memory are given
in PMT
Page Frame
Status
no no
0 1 4 Page No: gives the no. of pages in the
1 1 2 process.
2 0 - Status: page is loaded in the memory or not
3 0 - (1 stored, 0 not stored)
4 0 - Frame no: Gives the corresponding frame
5 0 - no for the page in main memory
7 0 -
8 0 -
9 0 -
Let us assume that initially first 2 pages say page 0 and page 1 are loaded
from secondary memory into the main memory.
➢ The status bit 1 in PMT tells the corresponding pages are loaded in
the main memory and main memory locations are given by the value
in the frame number.
➢ Suppose if there is a reference to Page7, but its bit is 0, then the
page fault mechanism load the page into main memory and change
the status bit as 1
Address space
Optimal (Opt):
This algorithm replaces the page that has to be referenced after long time.
Example:
Let the reference string be 7 0 1 2 0 3 0 4
Step:
• The pointer (p) checks the value of the reference bit if it is 0 the page is
replaced and the new page in loaded
• If the reference bit in 1 the reference bit is cleared and its arrival time is
reset to the current time this concept is called second chance.
• Steps (i) & (ii) are replaced until a page with reference bit 0 or a page
which is given the second chance is found and is replaced.
Example: 7 0 1 2 0 3 0 4
Initially 3 frames are empty
Step
1
0 0 0
1 1 1
0 0 1