OS&LINUX
OS&LINUX
BCA V M2
09413702022
ASSIGNMENT -3
OS & Linux
Q1 : 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-InFirst-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.
We’ll go through each algorithm step-by-step and calculate the page faults.
1. First-In-First-Out (FIFO)
In FIFO, the oldest page in the memory is replaced when a new page is loaded. It doesn’t
consider the frequency of access or recent usage, only the order in which pages entered.
Algorithm:
1. Initialize an empty list to represent page frames and a count for page faults.
2. For each page in the reference string:
o If the page is already in frames, continue.
o If it’s not, increment the page fault counter.
o If there’s space in the frames, add the page.
o If frames are full, replace the oldest page (first-in) with the new page.
In LRU, the page that hasn't been used for the longest time is replaced. It requires tracking the
usage history of each page.
Algorithm:
1. Maintain an ordered list of page frames, with the most recently used page at the end.
2. For each page in the reference string:
o If the page is already in frames, move it to the end.
o If it’s not, increment the page fault counter.
o If frames are full, remove the page at the beginning (least recently used), then add
the new page to the end.
Algorithm:
Solution Code
Here’s Python code to simulate each of these algorithms and calculate the total page faults.
import numpy as np
import pandas as pd
page_reference_string = [7, 2, 3, 1, 2, 5, 3, 4, 6, 7, 1, 0]
num_frames = 4
frames = []
page_faults = 0
page_faults += 1
if len(frames) < num_frames:
frames.append(page)
else:
frames.pop(0)
frames.append(page)
return page_faults
frames = []
page_faults = 0
page_faults += 1
frames.append(page)
else:
frames.pop(0)
frames.append(page)
else:
frames.remove(page)
frames.append(page)
return page_faults
frames = []
page_faults = 0
for i in range(len(pages)):
page = pages[i]
page_faults += 1
else:
future_uses = []
future_uses.append(pages[i + 1:].index(frame_page))
else:
future_uses.append(float('inf'))
frames[future_uses.index(max(future_uses))] = page
return page_faults
1. Predictive Page Replacement: Machine learning models like recurrent neural networks
(RNNs) or long short-term memory (LSTM) networks can be trained on page access
sequences to predict future pages.
2. Reinforcement Learning (RL): RL models can learn an optimal page replacement
policy by trial and error. Using a reward system, RL can minimize page faults by learning
which pages to retain.
3. Clustering and Pattern Recognition: Unsupervised learning algorithms like clustering
can group similar access patterns, helping predict future page needs based on context
(e.g., user behavior or program phases).
These approaches make page replacement adaptive, reducing page faults and improving memory
management efficiency. AI-based strategies could adapt to varying workloads and improve
efficiency beyond traditional algorithms.