Unit 4
Syllabus:
Memory Management: Basic bare machine, Resident monitor,
Multiprogramming with fixed partitions, Multiprogramming with variable
partitions, Protection schemes, Paging, Segmentation, Paged segmentation,
Virtual memory concepts, Demand paging, Performance of demand paging, Page
replacement algorithms, Thrashing, Cache memory organization, Locality of
reference.
Memory Management
• Memory Management means the OS controls how memory is used, allocated, and freed
for programs.
• When you run multiple programs (like Chrome, VS Code, etc.), all need memory.
The OS decides:
• Which program gets memory
• How much memory it gets
• When to remove it
Need of Memory Management
1. Efficient use of memory
2. Run multiple programs at same time (multiprogramming)
3. Avoid conflicts between programs
4. Improve system performance
Functions of Memory Management
1. Allocation of Memory
• OS gives memory to processes when needed
• Example: Opening a program → memory is allocated
2. Deallocation of Memory
• OS removes memory when process finishes
• Prevents memory waste
3. Tracking Memory Usage
• OS keeps record:
o Which part is free
o Which part is used
4. Protection
• One program should not access another program’s memory
• Ensures security and safety
5. Address Mapping
• Converts:
o Logical Address (used by program)
o into Physical Address (actual memory location)
Types of Memory Management Techniques
1. Contiguous Memory Allocation
• Memory is given in one continuous block
• Types:
o Fixed Partition
o Variable Partition
Simple but can waste memory
2. Non-Contiguous Memory Allocation
• Memory is divided and given in parts
• Types:
o Paging
o Segmentation
More efficient and flexible
Important Concepts
Paging
• Memory divided into fixed-size pages
• No external fragmentation
Segmentation
• Memory divided based on program parts
(code, data, stack)
Virtual Memory
• Uses hard disk as extra memory
• Allows running large programs even with less RAM
Demand Paging
• Pages are loaded only when needed
• Improves efficiency
Problems in Memory Management
1. Fragmentation
• Internal Fragmentation → wasted space inside block
• External Fragmentation → free space scattered
2. Thrashing
• Too much swapping of pages
• System becomes slow
Conclusion
Memory Management helps:
• Run multiple programs smoothly
• Use memory efficiently
• Protect data
• Improve system performance
Basic Bare Machine
• A bare machine is a computer system that runs directly on hardware without any
operating system support.
• No Windows, Linux, etc.
• Only hardware + user program
How it Works?
In a bare machine:
1. User writes a program
2. Program is loaded directly into memory
3. CPU executes it without any OS help
Structure
User Program → Hardware (CPU + Memory + I/O)
No OS in between
Features
• No memory management
• No file system
• No security
• No multitasking
• Direct hardware access
Limitations
1. No Protection
2. No Multiprogramming
3. No Resource Management
4. Difficult to Use
5. No Error Handling
Example
Early computers worked as bare machines:
• User loads program using punch cards
• CPU executes directly
Advantages
1. Direct control over hardware
2. No overhead of OS
Disadvantages
1. No protection
2. No multitasking
3. Hard to manage
4. Unsafe
Remember
Bare Machine:
• Simplest computer system
• No operating system
• Direct interaction with hardware
• Not suitable for modern use
Resident Monitor
• Resident Monitor is a small program that remains permanently in memory and controls
the execution of user programs.
• A basic OS-like program
• Always stays in memory
• Manages program execution automatically
Problems in Bare Machine:
• Manual program loading
• CPU idle time
• No automation
Solution: Resident Monitor automates program execution and improves efficiency
How Resident Monitor Works?
Memory is divided into two parts:
Resident Monitor User Program Area
Working
1. Monitor is loaded into memory (permanent)
2. User jobs are stored in input queue
3. Monitor picks one job
4. Loads it into memory
5. Transfers control to user program
6. After completion → control returns to monitor
7. Next job is executed
Components of Resident Monitor
1. Job Control Language (JCL)
• Used to give instructions to system
• Example: Start, Stop, Load program
2. Control Cards
• Special commands to control execution
3. Monitor Program
• Main controlling part
• Decides which job runs next
Features
1. Automatic job sequencing
2. Reduces CPU idle time
3. Improves system utilization
4. Always present in memory
Advantages
1. No need for manual intervention
2. Better CPU utilization
3. Faster execution than bare machine
4. Simple automation
Disadvantages
1. No memory protection
2. Only one job at a time (no multitasking)
3. No interaction during execution
4. Limited functionality
Multiprogramming with fixed partitions & Multiprogramming
with variable partitions
Protection Schemes
• Protection Schemes are techniques to protect memory and processes from unauthorized
or incorrect access.
Protection
Protection means ensuring that each process can access only its own memory and not others.
Simple idea:
• Program A should not access Program B’s data
• System files should be safe
Need of Protection
1. Security – Prevents unauthorized access
2. System Stability – Avoids system crash
3. Data Safety – Protects important data
4. Multi-user Support – Safe sharing of system
Basic Working
Every memory access by CPU is checked:
• If valid → Allowed
• If invalid → Blocked (trap/error)
Types of Protection Schemes
1. Base Register and Limit Register
Simplest and important method
Concept:
• Base Register → Starting address of process
• Limit Register → Size of process
Condition for valid access:
Address ≥ Base AND Address < (Base + Limit)
Example:
• Base = 1000
• Limit = 500
Valid range: 1000 to 1499
• Access 1200 → Allowed
• Access 1600 → Not allowed
2. Protection using Paging
Memory divided into pages and frames
Each page has protection bits:
• R (Read)
• W (Write)
• X (Execute)
Example:
• Page 1 → Read only
• Page 2 → Read + Write
Writing on Page 1 → Not allowed
3. Protection using Segmentation
Memory divided into logical parts:
• Code
• Data
• Stack
Each segment has:
• Base
• Limit
• Access rights
Example:
• Code → Read + Execute
• Data → Read + Write
Prevents illegal operations
Advantages
1. Prevents unauthorized access
2. Improves system security
3. Avoids crashes
4. Safe multitasking
Disadvantages
1. Increases complexity
2. Slight performance overhead
Paging
• Paging is a memory management technique in which a process is divided into fixed-
size pages, and main memory is divided into fixed-size frames, and pages are stored in
any available frame.
• Main Idea:
▪ Process → split into pages
▪ Memory → split into frames
▪ Pages can go anywhere in memory (non-contiguous)
Use of Paging
To solve problems of:
• External fragmentation
• Difficult contiguous allocation
Paging provides:
• Efficient memory usage
• Flexibility
Note: Page size = Frame size
What is an Instruction Offset?
• In memory addressing (especially in paging or segmentation), an instruction offset is
the distance (or position) of a specific instruction within a page or segment.
Basic Terms
Term Meaning
Page Part of process
Frame Part of physical memory
Page Table Maps page → frame
How Paging Works?
Step 1: Divide Process
Process P → Page0, Page1, Page2
Step 2: Divide Memory
Memory → Frame0, Frame1, Frame2, Frame3
Step 3: Allocate Pages to Frames
Page0 → Frame2
Page1 → Frame0
Page2 → Frame3
Pages are stored in different locations
Page Table
OS maintains a table:
Page No Frame No
0 2
1 0
2 3
Used for address translation
Address Translation
CPU generates Logical Address
It has:
• Page Number (p)
• Offset (d)
Logical Address = (p, d)
Using page table:
• Get Frame Number (f)
• Physical Address = (f, d)
Example:
Page = 1, Offset = 50
Page 1 → Frame 0
Physical Address = (0, 50)
Fragmentation in Paging
External Fragmentation → Not present
Hardware Support
1. Page Table Register (PTR)
• Stores address of page table
2. TLB (Translation Lookaside Buffer)
Small fast memory
Stores recent page entries
Reduces access time
Advantages
1. No external fragmentation
2. Better memory utilization
3. Easy allocation
4. Supports virtual memory
Disadvantages
1. Internal fragmentation
2. Page table overhead
3. Slight delay in address translation
TLB (Translation Lookaside Buffer)
• TLB (Translation Lookaside Buffer) is a small, fast memory used to store recent page
table entries to speed up address translation.
▪ It is like a cache for page table
▪ Helps CPU find memory locations faster
Need of TLB
Without TLB:
1. CPU checks page table in memory
2. Then accesses actual data
This takes 2 memory accesses → slow
With TLB:
• If entry is found → only 1 access needed
• Makes system faster
Where is TLB Located?
CPU → TLB → Main Memory
It is placed between CPU and main memory
How TLB Works?
1. CPU generates logical address (Page No + Offset)
2. TLB is checked first
Case 1: TLB Hit
• Page number found in TLB
• Frame number is obtained quickly
• Access memory directly
Fast access
Case 2: TLB Miss
• Page not found in TLB
• Go to page table in memory
• Update TLB
• Then access memory
Slower
TLB Entry Structure
Each entry contains:
• Page Number
• Frame Number
• Protection bits (R/W/X)
• Valid bit
Advantages of TLB
1. Very fast memory access
2. Reduces address translation time
3. Improves CPU performance
Disadvantages
1. Small size
2. Expensive
3. TLB miss causes delay
Example
Think of TLB like a shortcut note:
• You keep important page numbers on a small paper
• Instead of searching whole book
Saves time
Segmentation
• Segmentation is a memory management technique where a program is divided into
logical parts called segments instead of fixed-size blocks.
• In segmentation:
• Logical memory is divided into variable-sized segments
• Each segment has:
o Base address → starting location in physical memory
o Limit → size of the segment
• Each segment represents a meaningful unit such as:
o Code (instructions)
o Data (variables)
o Stack (function calls)
Advantages of Segmentation
1. Logical organization
2. Better protection
3. Easy sharing
Disadvantages of Segmentation
1. External Fragmentation
2. Complex memory management
Paging vs Segmentation
Feature Paging Segmentation
Basic idea Divides memory into fixed- Divides memory into variable-size
size pages segments
Unit size Fixed (e.g., 4 KB) Variable (depends on program
structure)
Address format Page Number + Offset Segment Number + Offset
User view Transparent to user Visible to user (logical division)
Fragmentation Internal fragmentation External fragmentation
Memory allocation Simple (fixed blocks) Complex (variable blocks)
Page/Segment Page table Segment table
table
Protection & Less flexible More flexible (per segment)
sharing
Thrashing
• Thrashing is a very important concept in Virtual Memory and Paging.
• Thrashing is a condition where the system spends more time swapping pages in and out
of memory than executing actual processes.
• System is busy moving pages, not doing useful work
• Performance becomes very slow
How Thrashing Happens
1. Many processes are loaded
2. Memory becomes full
3. Pages are removed frequently
4. Required pages are not in memory
5. Page fault occurs again and again
Continuous swapping = Thrashing
Example
Suppose:
• RAM = small
• Many programs running
Each program needs pages → not available → swapped repeatedly
System becomes slow like:
Opening apps takes too much time
Disadvantages
1. Very poor performance
2. High CPU idle time
3. Excessive disk usage
Remember
Thrashing:
• Happens when system is overloaded
• Causes excessive paging
• Reduces performance drastically
Cache Memory Organization
• Cache Memory Organization explains how cache memory is designed and arranged
to store and access data efficiently.
Cache Memory
Cache is a small, very fast memory placed between:
• CPU and
• Main Memory (RAM)
Purpose: To reduce memory access time and improve system speed
Need of Cache Memory
CPU is very fast
So cache acts as a bridge:
• Stores frequently used data
• CPU gets data faster
Main Idea of Cache Organization
Cache stores copies of frequently used data from main memory
It works based on:
• Locality of Reference
o Temporal (same data used again)
o Spatial (nearby data used)
Components of Cache Memory
1. Cache Lines (Blocks)
o Small units of cache
2. Tag
o Identifies which memory block is stored
3. Data
o Actual information
4. Valid Bit
o Shows whether data is valid or not
Cache Memory Organization Techniques
There are 3 main types:
1. Direct Mapping: Each block of main memory maps to only one cache line
2. Fully Associative Mapping: Any block can be placed in any cache line
3. Set-Associative Mapping
Combination of:
• Direct mapping + Fully associative
Cache divided into sets
Locality of Reference
• In OS the concept of locality of reference states that, instead of loading the entire
process in the main memory, OS can load only that number of pages in the main
memory that are frequently accessed by the CPU.
• Locality of Reference is an important concept in Memory Management.
Advantages
1. Faster execution
2. Better CPU performance
3. Efficient use of cache memory
4. Reduced memory access time