Module 4
Module 4
PAGE REPLACEMENT
ALGORITHMS AND DESIGN
ISSUES
Page Replacement algorithms:
• When a page fault occurs, the operating system has to choose a page to evict (remove from memory) to make room for the
incoming page.
• If the page to be removed has been modified while in memory, it must be rewritten to the disk to bring the disk copy up to
date
• Page removal operation would be happening in cache, TLB and many more places of operating systems
Belady’s anomaly- the most unexpected result where the page fault increases with the increase in the frame
numbers.
Optimal page replacement algorithm:
• The best possible page replacement algorithm is easy to describe but impossible (difficult)to actually implement.
• Objective is very simple: replace the page that will not be used for the longest period of time.
• FIFO is looking backward and OPT is looking forward.
Example:
Consider reference string: 1,2,3,4,1,2,5,1,2,3,4,5
Frame size=3.
Reference string: 1 2 3 2 1 4 2 3 2 5 1
Frame size is 3.
The Clock Page replacement Algorithm:
• Another Approach is to keep all the page frames on
a circular list in the form of a clock.
• The hand points to the oldest page.
• When a page fault occurs, the page being pointed
to by the hand is inspected. If its R bit is 0, the
page is evicted
• the new page is inserted into the clock in its place,
and the hand is advanced one position
• If R is 1, it is cleared, and the hand is advanced to
the next page. This process is repeated until a page
is found with R = 0.
Design Issues for paging systems:
1.Global replacement vs Local replacements:
Global replacement:
• Process selects a replacement frame from the set all frames: one process
can take frame from other.
• Increased throughput
• Number of frames allotted to process might get increased.
• Allow high priority process to select frames of low priority process for
replacement.
If a global algorithm is used,
• It may be also possible to start each process up with some frames
• number of pages proportional to the process’ size
• The allocation has to be updated dynamically as the processes run.
Local replacement:
• Each process selects from only its own set of allocated frames.
• Number of frames allotted to a process doesn’t change.
2. Load Control :
• A good way to reduce the number of processes competing for memory is to swap some of them to the disk and free up all the
pages they are holding.
• For example, one process can be swapped to disk and its page frames divided up among other processes that are thrashing
• If the thrashing stops, the system can run for a while this way. If it does not stop, another process has to be swapped out, and
so on, until the thrashing stops.
• Swapping processes out to relieve the load on memory is reminiscent of two level scheduling, in which some processes are
put on disk and a short-term scheduler is used to schedule the remaining processes.
1. Global Replacement vs. Local Replacement
These are two strategies for selecting which page frame to replace when a page fault occurs.
Global Replacement:
Frame replacement is done from any frame in the system, regardless of which process owns it.
Advantages: Load control helps manage memory usage when the system is overloaded.
Higher throughput (because more frames are available for replacement).
Flexible frame allocation—if a process needs more frames, it can take frames from other processes. Why Load Control is Needed:
High-priority processes can take frames from low-priority processes. When too many processes compete for memory, thrashing happens.
Example: (Thrashing = processes spend more time swapping pages in/out of memory
Process A can take a frame from Process B if it needs more memory. than executing.)
Disadvantage: Solution: Swapping to Disk (Load Control Technique)
A process can lose its frames to other processes, causing inconsistent performance. Swap out some processes to disk to free up memory.
Local Replacement: Allocate freed frames to other processes that are thrashing.
A process can only replace its own frames. Example:
Advantages:
Fair and predictable—each process gets a fixed number of frames. If Process A is thrashing, swap out Process B to disk, and give its memory
Isolation—one process won’t affect another process's frames. frames to Process A.
Example: If thrashing continues, swap out another process until the thrashing stops.
Process A can only use its own frames for page replacement.
Disadvantage:
Less efficient than global replacement if a process needs more memory but doesn’t have enough
frames.
Page size:
• Determining the best page size requires balancing several competing factors.
• For larger page size few factors may give good results and vice versa. What is Page Size?
Page size is the amount of memory each page takes in a paging system.
It’s important to decide the right page size to balance memory usage and
• For example: consider the virtual memory size is 4MB. performance. Let's explore the factors to consider when choosing page
size.
If number of total pages are less, page faults are also If number of total pages are more, page faults are
less. Larger Page Size Smaller Page Size
also more
Fewer pages are needed. More pages are needed.
Smaller page table size. Larger page table size.
I/O time will be less I/O time will be more
More internal fragmentation (wasted space inside a page).Less internal
fragmentation.
Fewer page faults (better performance).More page faults (slower performance).
Less I/O time (faster).More I/O time (slower).
Derivation of optimum page size:
s = Process size (in bytes)
s = process size, p= page size, e= size of each page table entry. e = Size of each page table entry (in bytes)
p = Optimum page size (in bytes)
The second term (internal fragmentation) is large when the page size is large. Process size = 1 MB (or 1048576 bytes)
Each page table entry = 8 byte
The optimum page size should lie somewhere between se/p and p/2.
p=
By taking the first derivative with respect to p and equating it to zero, we get the equation 2×1048576×
8
=16777216
−se / p2 + 1/2 = 0 =4096
bytes
p = √2se p=4096bytes
Example:
For s = 1MB and e = 8 bytes. What is the optimum page size?
File System implementation: ( File system layout)
• File systems are stored on disk.
• Most disks can be divided up into one or more partitions or sectors.
• Sector 0 of the disk is called the MBR (Master Boot Record) and is
used to boot the computer.
• The end of the MBR contains the partition table. This table gives the
starting and ending addresses of each partition.
• One of the partitions in the table is marked as active.
• The first thing the MBR program does is locate the active partition,
read in its first block, which is called the boot block.
• The program in the boot block loads the operating system contained in
that partition.
• Super block contains all the key parameters about the file system and
is read into memory when the computer is booted or the file system is
first touched. (No. of files, no of blocks etc)
• information about free blocks in the file system,
• i-nodes, an array of data structures, one per file, telling all about the
file
• root directory, which contains the top of the file-system tree.
• remainder of the disk contains all the other directories and files.
Implementing files:
Various methods are in use with different operating systems some of them are discussed here.
1. Contiguous Allocation
2. Linked-List Allocation
3. Linked-List Allocation Using a Table in Memory.
Contiguous Allocation:
• The simplest allocation scheme is to store each file as a contiguous run of disk blocks.
• Thus, on a disk with 1-KB blocks, a 50-KB file would be allocated 50 consecutive blocks. With 2-KB blocks, it would be
allocated 25 consecutive blocks.
Few Advantages:
1. It is simple to implement because keeping track of where a file’s blocks are is reduced.
2. the read performance is excellent because the entire file can be read from the disk in a single operation with
simple increment. ( Only one seek is required)
Few drawbacks:
1. over the course of time, the disk becomes fragmented. The disk is not compacted on the spot to squeeze out the
hole.
2. the disk ultimately consists of files and holes.
Linked-List Allocation: ( Non- Contiguous)
• To keep each one as a linked list of disk blocks. (pointer word)
• The first word of each block is used as a pointer to the next one.
The rest of the block is for data.
Advantages:
• No space is lost to disk fragmentation (except for internal
fragmentation in the last block). The rest can be found starting
there.
• Easy to implement.
Disadvantage:
• On the other hand, although reading a file sequentially is
straightforward, random access is extremely slow.
• To get to block n, the operating system has to start at the beginning
and read the n − 1 blocks prior to it, one at a time
• pointer takes up a few bytes
Linked-List Allocation Using a Table in Memory:
• The pointer word from each disk block and put it in a table in
memory.
• chains are terminated with a special marker (e.g., −1) that is not a
valid block number.
• Such a table in main memory is called a FAT (File Allocation Table)
• The primary disadvantage of this method is that the entire table must
be in memory all the time to make it work.
i-nodes:
• last method for keeping track of which blocks belong
to which file is to associate with each file a data
structure called an i-node (index-node).
• File Attributes lists the attributes and disk addresses of
the file’s blocks.
• Given the i-node, it is then possible to find all the
blocks of the file.
• This array is usually far smaller than the space
occupied by the file table
• One problem with i-nodes is that if each one has room
for a fixed number of disk addresses, what happens
when a file grows beyond this limit?
Implementing directories:
• When a file is opened, the operating system uses the path name supplied by the user to locate the directory entry
on the disk.
• The directory entry provides the information needed to find the disk blocks.
• Depending on the system, this information may be the disk address of the entire file (with contiguous
allocation), the number of the first block (both linked-list schemes), or the number of the i-node.
Ways of handling file names
PPT is only a reference for teaching.
Referring PPT is not sufficient for Exams.