0% found this document useful (0 votes)
7 views75 pages

5

The document discusses memory management in operating systems, covering concepts such as logical and physical addresses, memory allocation techniques (contiguous and non-contiguous), and fragmentation issues. It explains different memory management techniques including paging, dynamic and static loading, and the advantages and disadvantages of each method. Additionally, it highlights the importance of memory protection and the role of page tables in managing memory efficiently.

Uploaded by

siddhu2004y
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)
7 views75 pages

5

The document discusses memory management in operating systems, covering concepts such as logical and physical addresses, memory allocation techniques (contiguous and non-contiguous), and fragmentation issues. It explains different memory management techniques including paging, dynamic and static loading, and the advantages and disadvantages of each method. Additionally, it highlights the importance of memory protection and the role of page tables in managing memory efficiently.

Uploaded by

siddhu2004y
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/ 75

Operating System

CHAPTER-5
Memory Management
Topics
1. Memory Management: Basic Concept.
2. Logical and Physical address map.
3. Memory Allocation: Contiguous Memory Allocation.
4. Fixed and Variable Partition.
5. Internal and External Fragmentation and Compaction.
6. Paging: Principle of Operation.
7. Page Allocation.
8. Hardware support for paging
What is Memory Management?
• Memory Management is an important characteristic of the
Operating System. This comes beneath one of
the two main capabilities of an Operating System, useful
resource management. Main memory (RAM) is
where maximum of the programs run. It is one of the most
important matters people search for whilst buying a
new cellphone or a new laptop.
What is Memory hierarchy ?
• In computer system architecture memory Hierarchy is part to arrange
the memory in a such a way it can minimize access time.
• Register < Cache < Main Memory
< Secondary Memory.

Figure - 1 Memory
Logical and physical address
• The concept of a logical address space that is bound to a separate physical

address space is central to proper memory management

– Logical address (LA) – generated by the CPU; also referred to as virtual

address

– Physical address (PA)– address seen by the memory unit


Logical and physical address
• Logical and physical addresses are the same in compile-time and
load-time address-binding schemes; logical (virtual) and physical
addresses differ in execution-time address-binding scheme

• Logical address space is the set of all logical addresses generated by


a program
• Physical address space is the set of all physical addresses generated
by a program
What is Memory hierarchy ?
• It is a hardware device which uses to map virtual address to physical
address.

Figure - 2 MMU [2]


Loading and Linking
• Linking: It is the process of linking all the modules and functions of your
program.
• Loading : loading is the process to place programs and libraries into memory
and prepares them for execution

Loading and Linking is categorized into types

• Static

• Dynamic
Static Loading and Linking
• Loading the entire program into memory before startup execution

• The program execution will be faster

• Inefficient utilization of memory

• If static loading is used accordingly static linking is used


Dynamic Loading and Linking
• Loading the program into memory on demand

• Program execution will be slower

• Efficient utilization of memory

• If dynamic loading is used accordingly we need to use dynamic linking

• The majority of OS will use dynamic loading concept


Multistep processing of user program

Figure – 3 Diagram of
Address Binding
• Address binding is the binding of instructions and data to memory addresses

• It can happen at three different stages


– Compile time: If memory location known a priori, absolute code can be
generated; must recompile code if starting location changes
– Load time: Must generate relocatable code if memory location is not
known at compile time

– Execution time: Binding delayed until run time if the process can be moved
during its execution from one memory segment to another.

o Need hardware support for address maps (e.g., base and limit registers)
Swapping
• For temporarily any process can be swapped out of memory to
backing store and take back into memory for further execution.

• Backing store : disk large enough so that can accommodate copies


of all memory images for all users.

• Roll out roll in: for priority based scheduling algorithm swapping
variant used; to load high-priority process and to get execute lower
priority process need to swapped out.

• Ready queue maintain by the system for ready -to-run processes


which have memory images on disk.
Schematic View of Swapping

Figure – 6 Schematic
view Swapping [6]
Memory Management
Action to manage the memory of computer
system using various methods like partitions,
paging, segmentation and virtual memory is
called memory management.
Contiguous Non-contiguous
• Program must exist as a • Prog divided into chunks
single block of contiguous called segment
add. • Each segment can be placed
• Sometimes it is impossible different parts by memory.
to find large enough lock. • Easier to find “holes” in
• Low overhead which segment it will fit.
Memory Management Techniques (MMT)

Figure – 7 MMT [7]


Memory Allocation: Contiguous Memory Allocation

• There are 2 types of memory allocation:

1. Contiguous memory allocation

2. Non – Contiguous memory allocation


Contiguous Memory Allocation
• Contiguous memory allocation is known as a memory allocation model in
which each program is allocated a single contiguous adjoining area in the
memory

• The memory manager for a Multiprogram med operating system manages


primary memory distribution based on space multiplexed sharing
• The contiguous memory allocation scheme can be implemented in
operating systems with the help of two registers, known as the base and
limit registers.
Contiguous Memory Allocation
• Partitioning of memory can be done in the following ways:

1. Multiprogramming with Fixed Partition


2. Multiprogramming with Variable Partition
Multiprogramming with Fixed Partition (MFT)
•To support the multiprogramming the main memory is dividing into multiple
partitions
•The number of partitions are fixed but the size of each partition can be different

•Each process will store into these partitions contiguously.

•Here only one process is allowed into one partition.


•Degree of Multiprogramming is equivalent to the number of partitions as many
processes can be existed into main memory at one time
0 0

500 500

600 650

700 750

800 800

900
1000 1000

MFT with equal-size MFT with unequal-size


partition partition
Multiprogramming with Fixed Partition (MFT)

• In fig.1 memory is divided into 5 equal size partitions. Each partition


of 100MB.
• In fig.2 memory is divide into 4 unequal size partitions. Partition size
is not equal.
• Operating system will load the more than one process into the
partitions.
• If no free partition is available, then any process with lower priority
can be swapped out to make room for a higher priority process

Figure - 9
Multiprogramming with Fixed Partition (MFT)
•Advantage :
-- simple to implement
Disadvantages of Fixed Partition:
1.Internal Fragmentation
2.External Fragmentation
3.Limit process size
4.Limitation on Degree of Multiprogramming
Problems with Fixed Partitioning
• Main problem with this arrangement is fragmentation.
• Fragmentation is the presence of unusable areas in the computer memory
• There are two types of Fragmentation
1. Internal Fragmentation
Difference between total space of partition and space occupied by process is
called internal fragmentation
2. External fragmentation
This kind of memory un-utilization is called External fragmentation
Internal fragmentation
0
Here p1, p2,p3,p4 is loaded into memory

Process P1 is loaded into first partition whose


500 size is 150MB and size of process P1 is
110MB ,so in partition 40MB space is waste.
650

750 This is called Internal Fragmentation, this


800 type of problem is also occurred in other
partitions.
1000
External Fragmentation
• External Fragmentation: It arises when free memory areas existing in the
system are too small to be allocated to programs

0
p1 30
P2 100
P3 150
P4 270
P5 520
P6 820
P7
990
p8
1000
Multiprogramming with Variable Partition(MVT)
•To avoid the difficulties of fixed partitions, partitions are created
dynamically.

•In Dynamic partitions , no partition is fixed into memory but the partition
are created only when process is loaded into memory

•The partitions size is exactly size of process.


•If process size is 125MB, when process will enter into memory then 125MB
size partition will be created.

Figure - 10
• There are 6 processes p1, p2,p3,p4,p5, p6 and size of user process area
is 500MB which is currently free and no any partition in main memory.

0 Size of each process is as following:

p1 – 110 MB
500 p2 – 80MB
p3 – 40MB
p4 – 120MB
p5 – 100MB
p6=150MB

1000
• When process will load than the partition will be created as per size of
process.

0 0 0

500 500 500


610
610 610
690 690
730

1000 1000 1000

[ Loading of p1 ] [ Loading of p2 ] [ Loading of p3 ]


0
0

500 500

610 610

690 690

730 730
850
850
950
1000 1000
External
[ Loading of p4 ] [ Loading of p5 ] fragmentation
Multiprogramming with Variable Partition(MVT)
• In our example, size of process P1 is 110 MB, so partition of 110 MB will be
created for process P1. Same procedure will repeat for all processes.
• Here every process will be loaded into memory dynamically and partition will be
created exactly as size of process.
• Here 5 processes can be loaded dynamically. And at last process p6 cannot be
loaded because there is not enough space in memory to load process P6, so
space of 50MB will be wasted in memory. This is called external fragmentation.

• Here the not problem of internal fragmentation. But here the problem of
external fragmentation.
Multiprogramming with Variable Partition(MVT)
Advantages of Variable Partition:
1.No internal fragmentation.
2.Efficient use of memory.
Disadvantages of Variable Partition:
1.Problem of external fragmentation.
2.Not easy to implement.
Compaction
• Compaction is one of the technique to avoid External
Fragmentation.

• Move all the processes towards the top or


towards the bottom to make free available
space in a single contiguous block is known
as compaction.

Figure - 14 Example of
Compaction
• Compaction is undesirable to implement because it interrupts all
the running processes in the main Memory.

• The other technique to avoid external fragmentation is to


implement non-contiguous memory allocation techniques.
Non. Contiguous Memory
allocation
OS (M/M)
2 KB
///////1 KB////////
P1 process 2 KB
(6 KB)
////1 KB/////////////
2 KB P2
process
(2 KB)

Advantages: Remove EF
Non-contiguous memory allocation
• In Operating Systems, Paging is a storage mechanism used to retrieve
processes from the secondary storage into the main memory in the form of
pages.
• The main idea behind the paging is to divide each process in the form of
pages.
• The main memory will also be divided in the form of frames.
Paging:
•Paging is a memory management technique that permits the physical
address space of a process to be noncontiguous

•Paging avoids external fragmentation and the need of compaction

•Processor always generate logical address but your process resides in


physical address
•The mapping of logical address to physical address is called paging

•Logical and physical address space are represented in form of words


•Logical and physical address is represented in form of bits
Paging
•If PA = 19 bits

then PAS =2^19 words or 512K words


•If PAS = 256M words or 2^28 words

then PA = 28 bits

•If LA = 37 bits

then LAS=2^37 words or 128G words


•If LAS = 512M words or 2^29 words
then LA = 29 bits
Basic Method of Paging
•Physical memory is divided into fixed size blocks called frames
•Logical memory is also divided into blocks of same size called pages
•To run a program of size n pages, need to find n free frames and load
program, but these n frames may be located in noncontiguous
manner.
•Set up a page table to translate logical to physical addresses.
Paging model of logical and physical
memory of a process

Page no.
Frame
no.

Process A
Example of paging
•Here process A is divided into pages page1, page2, page3, page4 and

MM is divide into 8 frames .

•Here size of each page is 100MB, and size of each frame in memory is

100mB.
Paging Concepts
•Pages are loaded into frames.
•Page size and frame size are equal, which is defined by the system
hardware.
•Process will be divided into pages according to size of process.
•All pages of process will be loaded into the frames of MM.
Logical address
Logical address is divided into two parts

•Page number (p)

•Page offset (d)

<-------------m--------------
Page no. Page offset

P d
Page number: used as an index
m-ninto page table
n

Page offset: is the displacement within the page


Physical address
Physical address is divided into two parts

•Frame number (f)

•Frame offset (d)


<--------------m--------------
Frame no. Frame
offset
f d

m-n into physical


Frame number: used as an index n memory

Frame offset: used to index into single frame


• LAS=4 GB ==2^2*2^30 =2^32
• PAS=64 MB == 2^6 * 2^20 =2^26
• No of the page size= 4 KB = 2^2 * 2^10 =2^12
• No of the pages =?
• No of the frame=?
Address Translation architecture
Page Allocation
• The main advantage of using paged allocation is, an empty page
frame can be used by any job however, need to keep track of number
of pages and where the pages of individual job are located in
memory.

• In other words memory can be used efficiently and each job runs on
its own address space. Frames, pages and the mapping between the
two is shown in the .
Example of Page Allocation
• From figure, the size of each page is
100 memory locations.

• The memory utilized by the process1


in the given figure is 350 memory
locations where it takes 4 pages to
store 350 memory locations.

Figure - 21 Example of
Advantages of Paging
• Memory allocation and de-allocation is very fast.
• It is noncontiguous memory allocation so we do not require
contiguous memory space for one process.
• No external fragmentation problem.
• Swap-in and Swap-out operations are performed fast.
Disadvantages of Paging
• This technique is suffering with internal fragmentation.

• For Example:-
If pages are 120 bytes, a process of 400 bytes requires 3
pages + for 40 bytes it requires again 1 page. So total 4
pages will be allocated.

(120-40) = 80 bytes size will be free and its internal


fragmentation.

• It requires more size of memory to store page table. As the


page number increases the page table size is increase.
Pages (Every page=120 bytes)

Page 0 == 120 bytes

Page 1 == 120 bytes

Page 2 == 120 bytes

Page3 == 120 bytes (only 40 bytes)


(120-40)=80
So. 80 bytes it’s free space in memory= IF
Memory Protection
• Memory protection in paging must be required. That is frame
allocated to the one page cannot be reallocated to other page
meanwhile one page is allocated to it.

• This memory protection can be performed by using one


special bit associated with each frame, and stored in page
table, that is Protection Bit.

• Page table has index starting from 0 to maximum allowed page


in memory.
Memory Protection
• But we are not using all pages at a time so to find out which
page is used and write now present in memory and which is
not we are using one special bit that is known as Valid/Invalid
bit as shown in fig.

• Instead of using valid/invalid bit to determine the use of the


page, some system uses Page Table Length Register (PTLR)
which indicates the actual size of the page table and from it
we can know the last valid address of page
Memory Protection

Figure – 23 Valid(v) or
Inverted Page Table
Global page table P 0 Fo
A 1 No
G
Page Process 2 f2
Frame E
no ID
No. No. 3 No
Page table of P1
0 P0 P1

P1 P2 0 no
CPU P|d 1 F | d 1 f1
2 P2 P1 2 No
M/M 3 f4
Page table of P2
3 P1 P3

0 no
P3 P2
4 1 f3
2 f5
3 No
Page table of P3
Inverted Page Table Architecture
Cont.…
Paging Vs Segmantation
• Add()

Add()
Main()
F3
Mul()

F4
Sub()
Stack()

paging Segmentation
Segmentation
• Segmentation is a memory-management scheme that supports
user view of memory and contains segment.

• In fig. we can see that memory is


divided into multiple segments like,
subroutine, stack, code, heap, etc.

Figure – 24 Memory
Segmentation
• Users or programmers specify each address by 2 quantities:
1) a segment name
2) an offset. (contrast to the paging)

• For easiest way of implementation, segments are numbered and


referred by segment no. rather than name.
Segmentation
• So logical address consists of 2 tuple:
<segment-number, offset>
Segment
1 Segment offse
name
t
Logical Address Space
- Collection of segments

• Here, Segment-number is used to identify a segment.offset is an


original location within a segment.
Segmentation

Figure – 25 Memory
S | D
0 OS
Segment size
BA
S no Size 1500 S5

3300 200 1800


S1
0
1800 400
1 2200 S4
<=
2700 600 + 2300
2 S3

3 2300 400
2700 S2
4 2200 100
3300 S0
trap
1500 300
5 3500
M/M
d<=Size

Segmentation table
Example of Segmentation

Figure – 26 Memory
Hardware Implementation of Segmentation

Figure – 27 Address
Difference between Segmentation and Paging
Segmentation Paging
1 Program is divided into variable size Program is divided into fix size of pages.
segments.
2 Segmentation is slower than paging. Paging is faster than segmentation.

3 Segmentation is visible to the user. Paging is invisible to the user.

4 Segmentation eliminates internal Paging suffers from internal


fragmentation. fragmentation.
5 Segmentation suffers from external There is no external fragmentation.
fragmentation.
6 OS maintain a list of free holes in OS must maintain a free frame list
main memory.
Advantages of Segmentation
• Segmentation is useful for memory management.
• Using segmentation users can partition their programs into
number of subtask i.e. modules which are operate
independently from each another.
• Segments increases data sharing between two processes.
• Segmentation allows to extend the address ability of a
processor
• Segmentation provides facility to separate the memory areas
for heap, code, stack, data, etc.
Disadvantages of Segmentation
• In segmentation external fragmentation is mostly present.

• Costly algorithm.

• Segmentation finds free memory area big enough.

• When we use paging it generates list of free pages.

• Implementation is very complex.


Difference between Physical and Virtual memory
References
[7]
https://www.geeksforgeeks.org/implementing-non-contiguous-memory-manageme
nt-techniques/
[8]Operating Systems: Internals and Design Principles, by William Stallings.
[9]Operating Systems: Design and Implementation, Textbook by Andrew S.
Tanenbaum.
[10]Operating System Concepts by Galvin
[11]Modern Operating Systems, Andrew S. Tanenbaum
[12]Operating system concepts by Abraham Silberschatz.Peter B Galvin.Gerg
[13]OPERATING SYSTEMS MEMORY MANAGEMENT by Jerry Breecher.
References
[14]Operating System Generations. Tutorialspoint.
https://www.tutorialspoint.com/operating-system-generations
[15]Image source www.google.com
www.paruluniversity.ac.in

You might also like