0% found this document useful (0 votes)
18 views

Os Assignment3

dsa

Uploaded by

kritbarnwal5004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Os Assignment3

dsa

Uploaded by

kritbarnwal5004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

OS & Linux

Assignment : 03
Submitted to : Mrs. Priyanka Rattan
Submitted by : Krit Barnwal
Roll no : 06421102022
Class : Bca(V) M2
Question : Consider a page reference string 7, 2, 3, 1, 2, 5, 3, 4, 6, 7, 1, 0 and assume a system
with four page frames using demand paging. Using an AI/ML-enabled tool or framework (such as
Python combined with libraries like NumPy and Pandas to simulate page replacement algorithms),
determine the total number of page faults that would occur for each of the following page replacement
algorithms: FIFO (First-In First-Out), LRU (Least Recently Used), and Optimal Replacement.
Describe how AI/ML can assist in optimizing page replacement strategies by analyzing page access
patterns and predicting page requirements.

Solution
To solve the problem and determine the total number of page faults for each page replacement
algorithm (FIFO, LRU, and Optimal), we'll simulate each algorithm step by step using the given page
reference string.
Page Reference String:
7, 2, 3, 1, 2, 5, 3, 4, 6, 7, 1, 0
System Configuration:
• 4 page frames (assuming a demand paging system)

Code : Code :
page_ref = [7, 2, 3, 1, 2, 5, 3, 4, 6, 7, 1, 0]
frames_count = 4

# FIFO Algorithm
def fifo(page_ref, frames_count):
frames, page_faults = [], 0
print("FIFO Algorithm:")
for page in page_ref:
if page not in frames:
if len(frames) == frames_count:
frames.pop(0)
frames.append(page)
page_faults += 1
print(f"Reference: {page}, Frames: {frames}")
return page_faults

# LRU Algorithm
def lru(page_ref, frames_count):

2
frames, page_faults = [], 0
print("\nLRU Algorithm:")
for page in page_ref:
if page not in frames:
if len(frames) == frames_count:
frames.pop(0)
frames.append(page)
page_faults += 1
else:
frames.remove(page)
frames.append(page)
print(f"Reference: {page}, Frames: {frames}")
return page_faults

# Optimal Algorithm
def optimal(page_ref, frames_count):
frames, page_faults = [], 0
print("\nOptimal Algorithm:")
for i in range(len(page_ref)):
page = page_ref[i]
if page not in frames:
if len(frames) == frames_count:
# Find the frame that is not used for the longest period in the future
farthest_use = -1
farthest_frame = None
for frame in frames:
if frame not in page_ref[i + 1:]:
farthest_frame = frame
break
else:
next_use = page_ref[i + 1:].index(frame) if frame in page_ref[i + 1:] else float('inf')
if next_use > farthest_use:

3
farthest_use = next_use
farthest_frame = frame
frames.remove(farthest_frame)
frames.append(page)
page_faults += 1
print(f"Reference: {page}, Frames: {frames}")
return page_faults

# Running the algorithms


fifo_faults = fifo(page_ref, frames_count)
lru_faults = lru(page_ref, frames_count)
optimal_faults = optimal(page_ref, frames_count)

# Printing results
print(f"\nTotal FIFO Page Faults: {fifo_faults}")
print(f"Total LRU Page Faults: {lru_faults}")
print(f"Total Optimal Page Faults: {optimal_faults}")

Theory with output


1. FIFO (First-In-First-Out)
In FIFO, the first page that enters the system is the first to be replaced. We will maintain a queue for
the pages, and when a new page needs to be loaded, we replace the page that has been in the queue the
longest.
Simulation:
• Start with an empty set of frames.
• For each reference, check if it's in the frames:
o If it's not in the frames, it's a page fault. Add the page to the frames.
o If the frame is full, replace the oldest page.

4
2. LRU (Least Recently Used)
In LRU, the least recently used page is replaced when a new page needs to be loaded. We will
maintain a record of when each page was last accessed, and replace the least recently accessed page.
Simulation:
• Track the pages in the frames and update their usage when a new page is accessed.
• If the page is not in the frames, replace the least recently used page.

5
3. Optimal Replacement
In Optimal Replacement, the page that will not be needed for the longest period in the future is
replaced. This requires knowing the entire future page reference string.
Simulation:
• For each new page, if it's not already in the frames, find the page that is not needed for the
longest time in the future and replace it.

AI/ML in Optimizing Page Replacement Strategies


AI and ML can be extremely useful in optimizing page replacement strategies by analyzing historical
page access patterns and predicting future page requirements.
1. Prediction Models:
o Machine Learning: Using algorithms like decision trees, support vector machines, or
deep learning, models can predict the likelihood of a page being needed in the near
future based on past access patterns.
o Reinforcement Learning: By treating page replacement as a sequential decision-
making problem, reinforcement learning can help find optimal policies for when to
swap pages, learning from past actions to maximize performance.
2. Analyzing Access Patterns:
o Clustering: AI can identify clusters of pages that are frequently accessed together,
leading to more efficient page grouping and replacement decisions.

6
o Time Series Forecasting: Techniques like ARIMA or neural networks can forecast
page access over time, helping to predict which pages are least likely to be needed
soon and replacing them accordingly.
3. Adaptive Strategies:
o AI/ML can adjust page replacement strategies dynamically based on the current
workload. For example, during periods of high memory pressure, algorithms could
lean towards policies that minimize overhead, while during periods of lower pressure,
they could focus on optimizing for speed or reducing page faults.
By leveraging AI/ML, the page replacement system becomes more adaptable and efficient, reducing
the number of page faults over time and optimizing memory usage in real-time systems.

You might also like