OS Unit 3 Notes
OS Unit 3 Notes
Unit-3: Memory Management • The system creates the illusion that the program has full control over the machine.
Lecture 1 - Main Memory – Hardware and Control Structures However, this approach wastes CPU time when the program performs I/O operations.
1. Background 4. Multiprogramming
Memory is one of the most important components of a computer system. It consists of a large Modern operating systems use multiprogramming, where multiple processes are loaded into
array of bytes, where each byte has its own unique address. These addresses allow the CPU to memory simultaneously.
locate and access specific data or instructions stored in memory. In such systems:
When a program executes, the CPU performs a sequence of operations: • Several processes share the CPU.
1. Fetch – The CPU fetches an instruction from memory. • One process executes while others wait for I/O operations.
2. Decode – The fetched instruction is decoded to determine the required operation. This creates new challenges:
3. Operand Fetch – Required operands are fetched either from registers or memory. 1. Protection of the operating system from user processes.
4. Execute – The instruction is executed. 2. Protection of processes from each other.
5. Store Result – The result is stored back in memory or registers. Since the CPU directly accesses memory, protection must be implemented by hardware
From the perspective of the memory unit (MU), it only sees a continuous stream of memory mechanisms, not by the operating system software alone.
addresses. It does not know how these addresses were generated or which instructions caused 5. Base and Limit Registers
them.
One common hardware protection mechanism uses two registers:
2. Basic Hardware
• Base register
Before a program can execute, it must be loaded from disk into main memory. Once loaded, it
is placed in the ready queue, waiting to be scheduled by the CPU. • Limit register
The CPU can directly access only two types of storage: These registers define the logical address space available to a process.
• CPU registers Base Register - The base register contains the smallest legal physical address that a process
can access.
• Main memory
Limit Register - The limit register specifies the size of the allowed memory region.
Registers provide extremely fast access and can be accessed within one CPU clock cycle or
less. However, main memory access is slower and may require several cycles. This delay can Every memory access generated by a process must satisfy:
cause the CPU to stall. Base ≤ Address < Base + Limit
To improve performance, a cache memory is placed between CPU registers and main memory. If the address falls outside this range, the hardware generates a trap to the operating system,
Cache stores frequently used instructions and data, allowing faster access. preventing illegal memory access.
Another important requirement in modern systems is memory protection. Protection ensures Base and Limit Address Protection
that a program does not accidentally or intentionally modify memory belonging to other
programs or the operating system.
3. Uniprogramming
In early computer systems, uniprogramming was used. In this approach, only one program
runs in memory at a time.
Characteristics of uniprogramming include:
• The program always runs at the same location in physical memory.
• Since no other program exists in memory, there is no need for protection mechanisms.
Steps include: One variation of swapping is called roll-out, roll-in, which is commonly used with priority-
based scheduling algorithms. In this method, a low-priority process is swapped out of memory
1. Program contains a small statically linked function. so that a higher-priority process can be loaded and executed.
2. When the program starts, this function loads required libraries into memory. The total time required for swapping depends mainly on the amount of memory being
3. The dynamic linker resolves references to functions and variables. transferred. Since swapping involves disk I/O, the transfer time forms the major part of the
swap time.
Shared libraries are compiled using Position Independent Code (PIC) so they can be loaded
anywhere in memory. The operating system maintains a ready queue containing processes that are ready to execute
but whose memory images are stored on disk.
2. Schematic View of Swapping
In standard swapping, entire processes are moved between main memory and the backing
store.
Working Principle
1. When memory becomes full, some inactive processes are swapped out to disk.
2. This frees memory space for active processes.
3. If a swapped-out process becomes active again, it must be swapped back into memory
before execution.
This mechanism allows the operating system to oversubscribe physical memory, meaning
the total memory required by all processes can exceed the actual physical memory.
Idle or inactive processes are ideal candidates for swapping because their memory space can
be temporarily reassigned to other processes.
A process waiting for I/O cannot be swapped out easily because the I/O operation may write The interrupt vector table is usually stored in the first 1024 bytes of memory.
data to a memory location that no longer belongs to that process. Key characteristics:
To avoid this problem, some systems use double buffering, where data is first transferred to
• Contains 256 interrupt vectors
kernel space before moving to process memory. However, this increases overhead.
• Each vector is 4 bytes
2. Performance Overhead
• Stores the address of the ISR
Since disk access is much slower than memory access, swapping entire processes can
significantly reduce system performance. Address range: 000000H – 0003FFH
Because of these limitations, standard whole-process swapping is rarely used in modern 8. Relocation and Limit Registers
operating systems.
In contiguous allocation, relocation (base) registers and limit registers are used for protection
5. Swapping in Modern Systems (Linux Perspective) and address translation.
Modern operating systems like Linux use demand paging instead of whole-process Relocation Register - Stores the starting physical address of a process.
swapping.
Limit Register - Specifies the maximum logical address range that the process can access.
Instead of swapping entire processes, Linux swaps only small blocks of memory called pages
When the CPU scheduler selects a process for execution, the dispatcher loads these registers
(typically 4 KB each). This technique improves performance and reduces disk I/O overhead.
during the context switch.
6. Contiguous Memory Allocation
The MMU then dynamically converts logical addresses into physical addresses.
Contiguous allocation is one of the earliest memory allocation techniques.
9. Multiple-Partition Allocation
To improve memory utilization, memory can be divided into multiple partitions.
• Requires searching the entire memory list. 13. Fragmentation Rule (0.5N Rule)
The system allocates the largest available hole. For every N blocks allocated, about 0.5N blocks are lost due to fragmentation.
This means roughly:
Lecture 4 – Paging Paging allows logical memory to be mapped to physical memory in a non-contiguous
manner.
1. Introduction to Paging
This means that consecutive logical pages do not have to be stored in consecutive physical
Paging is a memory-management technique that allows the physical address space of a frames.
process to be non-contiguous. Similar to segmentation, paging removes the requirement that
an entire process must occupy a single continuous block of memory. Example mapping:
One of the main motivations behind paging is to eliminate the problem of external Logical Pages
fragmentation that occurs in contiguous memory allocation. Since memory is divided into Page 0
fixed-size blocks, paging ensures that free memory can always be allocated efficiently without Page 1
requiring compaction. Page 2
Paging also simplifies the management of memory on the backing store (disk). In earlier Page 3
techniques, storing memory chunks of varying sizes created complexity. Paging solves this by may be stored in physical memory frames as:
handling memory in uniform units.
Frame 5
However, while paging removes external fragmentation, it may still introduce internal Frame 2
fragmentation, where a page may not fully utilize the allocated frame. Frame 7
2. Basic Method of Paging Frame 1
The basic idea of paging is to divide both logical and physical memory into equal-sized blocks. The operating system keeps track of this mapping through the page table.
The page table is a key data structure used in paging systems to map logical pages to physical Address-Space Identifiers (ASIDs)
frames. Since address translation occurs for every memory reference, hardware support is Some TLBs store Address-Space Identifiers (ASIDs) with each entry.
required to make the translation process efficient.
ASIDs help distinguish page-table entries belonging to different processes. Without ASIDs, the
One possible implementation is to store the page table directly in CPU registers. In this TLB would have to be flushed during every context switch to prevent incorrect mappings.
approach, each page-table entry is stored in a dedicated hardware register. This method allows
extremely fast address translation because the CPU can access registers quickly. With ASIDs, entries for multiple processes can remain in the TLB simultaneously.
However, this approach is practical only when the page table is very small. Modern systems TLB Miss
may have thousands or millions of pages, making it impractical to store the entire page table in If the page number is not found in the TLB (called a TLB miss), the system:
registers.
1. Accesses the page table in memory.
To overcome this limitation, systems use a special hardware cache called the Translation
Lookaside Buffer (TLB). The TLB is a small, fast associative memory used to store recently 2. Retrieves the frame number.
used page-table entries. 3. Inserts this mapping into the TLB for faster future access.
2. Page Table Stored in Main Memory Replacement policies such as LRU or FIFO may be used when the TLB becomes full.
In most systems, the page table is stored in main memory rather than in CPU registers. Some critical entries can be wired down, meaning they remain permanently in the TLB for
Two important registers help manage the page table: fast access.
Page Table Base Register (PTBR) - The PTBR contains the starting address of the 4. Paging Hardware with TLB
page table in memory. The address translation process using a TLB works as follows:
Page Table Length Register (PTLR) - The PTLR specifies the size of the page 1. CPU generates a logical address (page number + offset).
table, indicating how many page entries exist.
2. Page number is searched in the TLB.
Two-Memory-Access Problem
3. If found, the frame number is obtained immediately.
When the page table is stored in memory, every memory reference requires two memory
accesses: 4. Physical address is formed by combining the frame number with the offset.
1. Access the page table to obtain the frame number. If the TLB does not contain the page entry:
2. Access the actual data or instruction in memory. 1. The page table is accessed in main memory.
Thus, one logical memory reference becomes two physical memory accesses, which slows 2. The mapping is inserted into the TLB.
down the system.
3. Physical address is calculated.
3. Translation Lookaside Buffer (TLB)
To improve performance, systems use the Translation Lookaside Buffer (TLB).
The TLB is a small, fast hardware cache that stores recently used page-table entries. When
the CPU generates a logical address, the system first checks whether the page number exists in
the TLB.
If the page number is found in the TLB, the frame number can be obtained quickly without
accessing the page table in memory.
Thus, memory usage is greatly reduced. Page tables can become extremely large in modern systems.
• Standard library functions To reduce memory usage, page tables can be organized hierarchically.
Instead of one large page table, the table is divided into multiple levels.
16. Linux Optimization: Huge Pages Virtual memory is a memory-management technique that allows a process to execute even
when the entire program is not loaded into physical memory. Instead of loading the complete
Modern Linux systems address TLB limitations using Huge Pages. program into RAM, only the required portions are loaded while the rest remain on secondary
Standard page size = 4 KB storage (disk).
Huge pages may be: The main advantage of virtual memory is that program size is no longer limited by the size of
physical memory. Programs can be larger than the available RAM because only a portion of
• 2 MB the program needs to be in memory at any given time.
• 1 GB Virtual memory also improves system efficiency by allowing more processes to run
Using huge pages allows a single TLB entry to cover much larger memory regions, reducing simultaneously. Since each process occupies only part of the memory, the operating system can
TLB misses. increase CPU utilization and throughput without increasing response time.
Another benefit is easy sharing of files and memory between processes, which is useful for
shared libraries and interprocess communication.
2. Motivation for Virtual Memory
In real applications, most programs do not use all their code simultaneously. Some parts of
the program are rarely executed.
Examples include:
• Error-handling routines
• Debugging functions
• Rarely used modules
• Large data structures
Loading the entire program into memory wastes space because these sections may never be
used.
Virtual memory solves this problem by allowing partial program loading. Only the portions
that are actually required during execution are loaded into memory.
This approach provides several advantages:
• Programs are not constrained by RAM size.
• Memory usage per program is reduced.
• More programs can run simultaneously.
• CPU utilization improves.
3. Separation of Logical and Physical Memory
Virtual memory separates logical memory from physical memory.
• Heap grows upward • Only one copy of the library code is loaded into memory.
This layout maximizes memory usage and leaves unused space between them. This reduces memory consumption and improves efficiency.
Shared memory can also be implemented by mapping the same physical pages into multiple
virtual address spaces.
7. Demand Paging
Demand paging is a technique where pages are loaded into memory only when they are
needed.
The unused space between heap and stack allows them to grow dynamically.
5. Sparse Address Space
Virtual memory allows sparse address spaces, where large sections of the logical address
space remain unused.
These unused regions are called holes. They allow memory to grow dynamically when
required.
Sparse address spaces are useful for: Instead of loading an entire process during program startup, the operating system loads pages
only when they are referenced.
• Dynamic memory allocation
Advantages of demand paging:
• Dynamically linked libraries
When the CPU references a page marked invalid, the system generates a page fault
interrupt.
1. Introduction to Page Replacement • Pages are replaced in the order they were loaded into memory
Page replacement algorithms are used when main memory is full and a new page needs to • The oldest page is removed first
be loaded. Since no free frames are available, the operating system must decide which This is implemented using a queue:
existing page should be removed from memory.
• New pages are added at the rear
Two important decisions are involved:
• Old pages are removed from the front
1. Frame Allocation Algorithm
Determines how many frames each process should receive. Example
Important points: FIFO may replace frequently used pages simply because they are old.
• Only page numbers are considered (not full addresses) 5. Belady’s Anomaly
• Repeated access to the same page does not cause a page fault FIFO suffers from a surprising problem called Belady’s Anomaly.
o The algorithm used Increasing the number of frames can sometimes increase the number of page faults
When a page fault occurs: With more frames, FIFO may produce more page faults.
1. Check if a free frame exists This behavior is undesirable and shows that FIFO is not always optimal.
3. If no → select a victim page using an algorithm The Optimal (OPT) algorithm replaces the page that will not be used for the longest time
in the future.
4. Replace victim page with new page
Working Principle
5. Update page table
• Look ahead in the reference string
• Replace the page that has not been used for the longest time in the past Important Result
LRU uses past behavior to predict future usage. Stack algorithms do not suffer from Belady’s Anomaly
Thus, increasing frames will always reduce or maintain page faults.
10. Comparison of Algorithms
In paging systems, memory is divided into fixed-size frames, and the OS must decide how to Each gets = 18 frames
distribute these frames among processes. Remaining = 3 frames (buffer pool)
How to allocate limited frames efficiently among multiple processes? • Ignores process size
• After that → page replacement starts • Processes: 10 pages & 127 pages