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

OS&LINUX

dsa

Uploaded by

kritbarnwal5004
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)
8 views

OS&LINUX

dsa

Uploaded by

kritbarnwal5004
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/ 6

Submitted by :- Kirti Singhal

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.

Replacement Algorithms Page


Given:

 Page Reference String: [7, 2, 3, 1, 2, 5, 3, 4, 6, 7, 1, 0]


 Number of Page Frames: 4

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.

2. Least Recently Used (LRU)

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.

3. Optimal Page Replacement


In Optimal Replacement, the page that won’t be used for the longest time in the future is
replaced. This algorithm requires future knowledge of page requests, making it idealized but
useful for theoretical comparisons.

Algorithm:

1. 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 frames are full, check each page currently in memory:
 Determine when each page will next be used in the future.
 Replace the page with the furthest future access.

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

def fifo_page_replacement(pages, num_frames):

frames = []

page_faults = 0

for page in pages:

if page not in frames:

page_faults += 1
if len(frames) < num_frames:

frames.append(page)

else:

frames.pop(0)

frames.append(page)

return page_faults

def lru_page_replacement(pages, num_frames):

frames = []

page_faults = 0

for page in pages:

if page not in frames:

page_faults += 1

if len(frames) < num_frames:

frames.append(page)

else:

frames.pop(0)

frames.append(page)

else:

frames.remove(page)

frames.append(page)

return page_faults

def optimal_page_replacement(pages, num_frames):

frames = []

page_faults = 0

for i in range(len(pages)):

page = pages[i]

if page not in frames:

page_faults += 1

if len(frames) < num_frames:


frames.append(page)

else:

# Find the page to be replaced

future_uses = []

for frame_page in frames:

if frame_page in pages[i + 1:]:

future_uses.append(pages[i + 1:].index(frame_page))

else:

future_uses.append(float('inf'))

# Replace the page with the furthest future use

frames[future_uses.index(max(future_uses))] = page

return page_faults

fifo_faults = fifo_page_replacement(page_reference_string, num_frames)

lru_faults = lru_page_replacement(page_reference_string, num_frames)

optimal_faults = optimal_page_replacement(page_reference_string, num_frames)

print(f"FIFO Page Faults: {fifo_faults}")

print(f"LRU Page Faults: {lru_faults}")

print(f"Optimal Page Faults: {optimal_faults}")

Optimizing Page Replacement with AI/ML


Using AI/ML for page replacement strategies can optimize memory management by predicting
future page accesses based on observed patterns. Techniques that could be employed include:

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.

You might also like