0% found this document useful (0 votes)
4 views35 pages

Module 4

The document provides an overview of memory management in operating systems, covering key concepts such as fixed and dynamic partitioning, fragmentation, paging, segmentation, and virtual memory. It discusses the advantages and disadvantages of each memory management technique, including demand paging and page replacement algorithms. The document emphasizes the importance of efficient memory utilization and the role of the operating system in managing memory allocation and protection.

Uploaded by

Khan Arkan
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)
4 views35 pages

Module 4

The document provides an overview of memory management in operating systems, covering key concepts such as fixed and dynamic partitioning, fragmentation, paging, segmentation, and virtual memory. It discusses the advantages and disadvantages of each memory management technique, including demand paging and page replacement algorithms. The document emphasizes the importance of efficient memory utilization and the role of the operating system in managing memory allocation and protection.

Uploaded by

Khan Arkan
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

AIIT

Amity Institute of Information


Technology (AIIT)
BCA, I Semester number
CSIT 150 : Principles of Operating Systems
Dr. Akash Shah
ashah@[Link]
Room No. I1: 304
1
AIIT

Module 4

2
Introduction to Memory AIIT

Management

• Memory management is a crucial function of the operating


system.
• It handles allocation and deallocation of memory space to
processes.
• Ensures efficient use of available memory.
• Provides isolation and protection between processes.

3
Memory Partition AIIT

Fixed Partitioning

• Main memory divided into fixed-size partitions.


• Simple but may cause internal fragmentation.

4
Memory Partition AIIT

Dynamic Partitioning

• Memory divided dynamically as per process


size.
• Reduces internal fragmentation but increases
external fragmentation.

5
Fragmentation AIIT

• Internal Fragmentation: Wasted space within


allocated memory.

6
Fragmentation AIIT

• External Fragmentation: Free memory


scattered in small blocks.

7
Paging AIIT

• Paging is a memory management technique used by


operating systems to manage how data is stored and
retrieved from main memory.
• It eliminates external fragmentation and allows
processes to be stored in non-contiguous memory
locations.
• In paging, both logical memory (the memory seen by a
process) and physical memory (the actual RAM) are
divided into equal-sized blocks.

8
Paging AIIT

Term Description
• Each process is
Page A fixed-size block
of logical divided into pages.
memory.
• Pages are loaded into
Frame A fixed-size block
of physical available frames in
memory (RAM). memory.
Page Table A data structure
used by the OS to • The OS keeps track
map pages to
frames.
of where each page is
located using the
page table.

9
Paging AIIT

• Each address generated by the CPU is divided into two


parts:
➢ Page Number (p): Used as an index in the page
table.
➢ Offset (d): Location within the page.
• Logical Address = (p, d)
The page table translates the page number (p) into a
frame number (f).
• Physical Address = (f × Frame Size) + d

10
Paging AIIT

Suppose:
• Logical address space = 16 KB
• Physical memory = 8 KB
• Page size = 1 KB
Then:
• Number of pages = 16 KB / 1 KB = 16 pages
• Number of frames = 8 KB / 1 KB = 8 frames
If the logical address is 2050,
• Page size = 1024 bytes
• Page number = 2050 / 1024 = 2
• Offset = 2050 % 1024 = 2
If page 2 is stored in frame 5 →
Physical address = (5 × 1024) + 2 = 5122
11
Advantages of Paging AIIT

• No External Fragmentation:
Since memory is divided into fixed-size blocks, no space
is wasted between partitions.
• Efficient Memory Utilization:
Processes can be loaded wherever free frames are
available.
• Easy Swapping:
Pages can be easily moved in and out of physical
memory.
• Supports Virtual Memory:
Allows execution of large processes that don’t fit entirely
in RAM.
12
Disadvantage of Paging AIIT

• Internal Fragmentation:
The last page of a process may not be completely filled,
causing some wasted space.
• Page Table Overhead:
Each process requires a page table, consuming extra
memory.
• Address Translation Overhead:
Accessing the page table adds extra time to address
translation (though mitigated by TLB).

13
Segmentation AIIT

Segmentation is a memory management


technique that divides a program into variable-
sized logical units called segments.
Each segment represents a logical part of the
program such as code, data, stack, or heap.

Unlike paging, which divides memory into fixed-


size pages, segmentation reflects the logical
structure of a program and allows better protection
and sharing of data.
14
Segmentation AIIT

• A segment is a logical unit of a program.


• Each segment has a name, a base address
(starting address in physical memory), and a limit
(length of the segment).
• The OS maintains a segment table for each
process to keep track of all segments.

15
Segmentation AIIT

Term Meaning
Segment
Logical part of a program (code, data,
stack, etc.)

Limit
Length or size of the segment

Base
Starting physical address of the
segment

Segment Table
Table that maps each segment to its
base and limit

16
Segment AIIT

Logical and Physical Address


A logical address generated by the CPU consists of two
parts:
• Segment Number (s) – Identifies the segment.
• Offset (d) – Specifies the location within the segment.
Logical Address = (s, d)
The segment table is used to convert this logical address
into a physical address:
Physical Address = Base(s) + d
Condition:
If d > Limit(s) → Segmentation Fault (invalid memory
access)
17
Segment AIIT

Address Translation Process


• CPU generates a logical address (s, d).
Segment number (s) is used to index into the
segment table.
• The base address and limit of that segment are
obtained.
• The offset (d) is compared with the limit.
If valid, physical address = base + d.
If invalid, segmentation fault occurs.

18
Advantages of Segment AIIT

• Logical Division:
Memory is divided into meaningful segments (code, data,
stack).
• Protection:
Each segment can have different access rights
(read/write/execute).
• Sharing:
Multiple processes can share common segments like code
libraries.
• Dynamic Growth:
Segments such as stack and heap can grow or shrink
independently.
• Ease of Relocation:
Each segment can be placed anywhere in memory because
of its base address. 19
Disadvantages of Segment AIIT

• External Fragmentation:
Because segments are variable-sized, gaps may appear
between them.
• Complex Memory Management:
Finding free memory blocks for segments is more
difficult than for fixed-size pages.
• Extra Overhead:
Each process requires a segment table, increasing
memory overhead.

20
Virtual Memory AIIT

• Virtual Memory is a memory management


technique that gives an illusion of a large,
continuous main memory to each process, even
though the physical memory (RAM) may be
limited.
• In simple terms, it allows the execution of
processes that may not be completely loaded
into main memory.

21
Virtual Memory AIIT

• Virtual Memory is a technique that allows the


execution of processes that are not completely
in main memory by using part of the secondary
storage (like a hard disk) as an extension of the
main memory.
• The part of the disk used for this purpose is
known as swap space or page file.

22
Need for Virtual Memory AIIT

Without virtual memory:


• A process must fit entirely in main memory.
• If memory is full, new processes can’t be loaded
until old ones finish.
With virtual memory:
• Processes can be partially loaded.
• The OS loads only the active parts (needed
instructions/data) of a process.
• Increases multiprogramming and CPU
utilization
23
Virtual Memory Working AIIT

• Each process is given a virtual address space


(logical memory) much larger than physical
memory.
• The OS and hardware together map virtual
addresses to physical addresses using a
mechanism called paging.
• The page table keeps track of where each virtual
page is stored (either in RAM or on disk).

24
Virtual Memory component AIIT

Component Description

Virtual Address Space The address range a process thinks it owns.

Physical Memory (RAM) Actual hardware memory available.

Secondary Storage (Disk) Used as backup for inactive pages.

Maps virtual pages to physical frames or disk


Page Table
blocks.

Hardware that translates virtual to physical


MMU (Memory Management Unit)
addresses.

25
Page Fault AIIT

A page fault occurs when a process tries to


access a page that is not currently in main
memory.
Steps:
• CPU generates a virtual address.
• MMU checks the page table.
• Page not found → page fault.
• OS loads the required page from disk into a free
frame.
• Page table is updated.
• Instruction is re-executed. 26
Advantages of VM AIIT

• Efficient Memory Utilization:


Only required pages are loaded into RAM.
• Larger Logical Memory:
Processes can use more memory than physically available.
• Isolation and Protection:
Each process has its own address space — no interference
between processes.
• Increased Multiprogramming:
More processes can be loaded simultaneously.
• Simplified Programming:
Programmers need not worry about physical memory
limitations.

27
Disadvantages of VM AIIT

• Slower Performance:
Accessing pages from disk (during page faults) is much
slower than RAM.
• Overhead:
Managing page tables and page replacement adds
computational overhead.
• Thrashing:
If too many page faults occur, CPU spends more time
swapping pages than executing — leading to drastic
performance drop.

28
Demand Paging AIIT

Definition:
• Demand Paging is a virtual memory management
technique where pages are loaded into main memory
only when they are required during execution.
• It is a type of lazy loading — pages are brought from
secondary storage to RAM on demand, not in advance.
Key Idea:
• Initially, no pages are loaded.
• When a page is needed → Page Fault occurs → OS loads
the required page into RAM.
Goal:
• Efficient memory usage and the ability to execute large
programs even with limited physical memory.
29
Demand Paging AIIT

Steps:
• CPU generates a virtual address for a page.
• MMU (Memory Management Unit) checks the
page table.
• If page is present in RAM → Access continues
normally.
• If page is not in RAM → Page Fault occurs.
• OS handles the page fault:
➢ Finds the required page on disk (swap space).
➢ Loads it into a free frame in RAM.
➢ Updates the page table.
➢ Restarts the instruction. 30
Demand Paging AIIT

Advantages Disadvantages
• Efficient use of physical • Page Fault Overhead:
memory. Accessing pages from
• Faster program startup disk is very slow.
(no need to load the • Thrashing: Too many
whole process). page faults lead to
• Enables execution of performance degradation.
large programs. • Complexity: Requires
• Supports higher degree hardware support and OS
of multiprogramming. management

31
Page Replacement Algorithm AIIT

• A Page Replacement Algorithm is used in


virtual memory systems to decide which page
to remove from main memory (RAM) when a
new page needs to be loaded and the memory
is already full.
• Since physical memory has limited frames, when
a page fault occurs and no free frame is
available, the OS replaces one existing page
based on a specific strategy — the page
replacement algorithm.
32
FIFO AIIT

Definition:
Pages in
• The page that was brought Step Page Fault
Memory
into memory first is the one
that is replaced first when a 1 1
new page is needed.
• Mechanism:
2 1, 2
• Uses a queue to keep track of
the order in which pages were
loaded. 3 1, 2, 3
• Oldest page → replaced first.
2, 3, 4 (1
• Example: 4
removed)
Assume 3 page frames and
reference string: 3, 4, 1 (2
5
[1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5] removed)

33
LRU AIIT

Definition: Pages in Page Page


Step
• Replaces the page that has not Memory Replaced Fault
been used for the longest
time. 7 7 —
• Based on the principle of
temporal locality recently used 0 7, 0 —
pages are likely to be used
again soon. 1 7, 0, 1 —
• Mechanism:
• Keep track of the time or order 2 0, 1, 2 7
of last use for each page.
• The least recently used page is
replaced first. 0 0, 1, 2 —
• Example:
For 3 frames and reference 3 1, 2, 3 0
string:
[7, 0, 1, 2, 0, 3] 34
Optimal Page Replacement Algorithm AIIT
• Definition:
• Replaces the page that will Step Pages in Memory Page Replaced Page Fault

not be used for the longest


period of time in the future. 7 7 —

• It gives the lowest possible


page fault rate but is 0 7, 0 —

theoretical, since future


references are unknown. 1 7, 0, 1 —

• Mechanism:
• When a page fault occurs, look 2 0, 1, 2 7

ahead in the reference string.


• Replace the page whose next 0 0, 1, 2 —

use is farthest in the future.


• Example: 3 0, 2, 3 1

Reference string: [7, 0, 1, 2, 0,


3], 3 frames 35

You might also like