0% found this document useful (0 votes)
2 views5 pages

12 B Shravan Exp11

The document outlines an experiment on page replacement policies, specifically focusing on FIFO, LRU, and Optimal algorithms. It provides code implementations for each policy to demonstrate how they handle page faults, along with their pros and cons. The conclusion states that the program was successfully executed, and it includes a numerical example for calculating page faults using the discussed algorithms.

Uploaded by

cirah69753
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)
2 views5 pages

12 B Shravan Exp11

The document outlines an experiment on page replacement policies, specifically focusing on FIFO, LRU, and Optimal algorithms. It provides code implementations for each policy to demonstrate how they handle page faults, along with their pros and cons. The conclusion states that the program was successfully executed, and it includes a numerical example for calculating page faults using the discussed algorithms.

Uploaded by

cirah69753
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/ 5

Experiment - 11

Page Replacement Policies


NAME: Shravan
Jitendra Mangale.
ROLL:NO- 12
DIVISION:B
BATCH:A
Write a program in demonstrate the concept of page replacement
Learning policies for
Objective: handling page faults eg: FIFO

Learning Students will be able to implement page replacement policies


Outcome
:
Course CSL403.5
Outcome:
Program (PO 3) Design/ development of solutions: Breadth and uniqueness of
Outcome engineering problems i.e. the extent to which problems are original and
: to which solutions have previously been identified or codified

(PO 12) Life Long Learning

Bloom's Apply, Analyze, Create


Taxonomy
Level:
Theory: 1. FIFO (First-In, First-Out)
● Concept: The oldest page in memory is replaced first.
● Implementation: Uses a queue.
● Pros: Simple to implement.
● Cons: Can lead to poor performance (e.g., Belady’s Anomaly).

2. LRU (Least Recently Used)


● Concept: Replaces the page that hasn't been used for the
longest time.
● Implementation: Stack or counter method.
● Pros: Good approximation of optimal.
● Cons: Can be costly in terms of time and hardware support.

3. Optimal Page Replacement (OPT or MIN)


● Concept: Replaces the page that will not be used for the longest
time in the future.
● Implementation: Requires future knowledge.
● Pros: Theoretical best performance.
● Cons: Not feasible in practice, only used for comparison.

Algorithm :
Data Set : Assume Appropriate Data Set
Outcome : 1) FIFO

Code:
def fifo(pages, capacity):
memory = []
page_faults = 0

for page in pages:


if page not in memory:
if len(memory) == capacity:
memory.pop(0)
memory.append(page)
page_faults += 1
return page_faults

pages = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
capacity = 4

print("FIFO Page Faults:", fifo(pages, capacity))

OUTPUT:

2) OPTIMAL

CODE:

def optimal(pages, capacity):


memory = []
page_faults = 0

for i in range(len(pages)):
if pages[i] not in memory:
if len(memory) < capacity:
memory.append(pages[i])
else:
# Find the page not used for the longest time in future
future = pages[i+1:]
index_to_replace = -1
farthest = -1
for j in range(len(memory)):
try:
next_use = future.index(memory[j])
except ValueError:
next_use = float('inf')

if next_use > farthest:


farthest = next_use
index_to_replace = j

memory[index_to_replace] = pages[i]
page_faults += 1
return page_faults

pages = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
capacity = 4
print("Optimal Page Faults:", optimal(pages, capacity))

OUTPUT:

3) LRU
CODE:
def lru(pages, capacity):
memory = []
recent = []
page_faults = 0

for page in pages:


if page not in memory:
if len(memory) < capacity:
memory.append(page)
else:
# Remove least recently used
lru_page = recent.pop(0)
memory.remove(lru_page)
memory.append(page)
page_faults += 1
else:
recent.remove(page)
recent.append(page)
return page_faults

pages = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
capacity = 4
print("Lru Page Faults:", lru(pages, capacity))

OUTPUT:

Conclusion : The given program is successfully executed

References: www.moodle.dbit.in

Numerical: Consider the page reference string 1,2,3,5,2,4,5,6,2,1,2,3,7,6,3,2,1,2,3,6.


Calculate the Page fault using 1, Optimal 2. LRU 3. FIFO algorithms for a
memory with three frames.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy