Os Assignment3
Os Assignment3
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
# 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}")
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.
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.