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

Dela Cruz Optimal Algorithm

The document outlines a laboratory exercise focused on the Optimal Page Replacement Algorithm in operating systems, aiming to understand its implementation and performance comparison with other algorithms like FIFO and LRU. It includes tasks for manual tracing, program implementation, and analysis of the algorithm's impracticality in real systems due to the unpredictability of page access. Students are required to submit their manual trace, program results, and source code for the optimal page replacement simulation.
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 views5 pages

Dela Cruz Optimal Algorithm

The document outlines a laboratory exercise focused on the Optimal Page Replacement Algorithm in operating systems, aiming to understand its implementation and performance comparison with other algorithms like FIFO and LRU. It includes tasks for manual tracing, program implementation, and analysis of the algorithm's impracticality in real systems due to the unpredictability of page access. Students are required to submit their manual trace, program results, and source code for the optimal page replacement simulation.
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

Tristan Merline Dela Cruz BSCpE 4-1

Engr. Obamos November 04, 2024

School of Engineering and Technology


Operating Systems

Laboratory Exercise No.7


Fundamentals of Operating System
Objective
• To understand and implement the Optimal Page Replacement Algorithm.
• To analyze and compare the performance of the optimal page replacement
algorithm with other algorithms like FIFO and LRU.
Problem Description
In an operating system, page replacement algorithms decide which memory pages to
remove to accommodate new pages. The Optimal Page Replacement Algorithm is a
theoretical model that replaces the page that will not be needed for the longest period in
the future. Although it's challenging to implement in real-time systems, it provides a
benchmark to evaluate the efficiency of other algorithms.
Instructions:
• Explanation of the Optimal Page Replacement Algorithm:
• Explain that the algorithm predicts future page requests to decide which page to
replace, minimizing page faults.
• Discuss the limitations of using the optimal algorithm in real-world applications
but its value in comparing other algorithms' efficiencies.

Example Scenario:
Consider a sequence of page requests: [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2].
Assume a memory with a limited capacity (e.g., 3 frames).
Walk through each page request and determine which page should be replaced according
to the optimal page replacement policy.

Task 1: Manual Trace


• Given a specific page sequence and frame size, students will manually trace the
optimal page replacement algorithm.
• They should record each step, note which pages are loaded into frames, and track
the number of page faults.

Task 2: Program Implementation


Write a program that simulates the optimal page replacement algorithm.
The program should:
• Accept a list of page requests and the number of frames as input.
• Implement logic to identify which page will not be needed for the longest period
and replace it.
• Track and output the number of page faults
Tristan Merline Dela Cruz BSCpE 4-1
Engr. Obamos November 04, 2024

Task 3: Comparison with Other Algorithms

Analysis Questions:
Why is the optimal page replacement algorithm considered impractical in real systems?
The Optimal Page Replacement algorithm is considered impractical in real systems
because it requires knowing exactly which pages will be accessed in the future, which
isn’t possible in real-time scenarios. In real systems, page access is unpredictable, so
we can’t accurately predict future accesses like the algorithm needs.
How could knowing the access pattern in advance affect system performance?
If we knew access patterns ahead of time, though, it could improve system
performance. The system could load only the pages it needs, reducing page faults and
making memory use more efficient. This would speed up applications by minimizing
delays caused by loading and swapping pages.

Submit:
• The manual trace of the optimal page replacement.
• Results of the program, including the number of page faults.
• Source code for the optimal page replacement program.

7 0 1 2 0 3 0 4 2 3 0 3 2 3

7 7 7 7 - 3 - 3 - - 3 - - -
0 0 0 0 4 0
1 2 2 2 2
Tristan Merline Dela Cruz BSCpE 4-1
Engr. Obamos November 04, 2024
Tristan Merline Dela Cruz BSCpE 4-1
Engr. Obamos November 04, 2024

def optimal_page_replacement(page_requests, frame_count):


frames = []
page_faults = 0

print(f"\nPage requests: {page_requests}")


print(f"Number of frames: {frame_count}\n")

for i, page in enumerate(page_requests):


if page not in frames:
page_faults += 1
if len(frames) == frame_count:
furthest_index = -1
page_to_replace = None
for f in frames:
if f in page_requests[i+1:]:
next_use = page_requests[i+1:].index(f) + i + 1
else:
next_use = float('inf')

# Select page to replace based on furthest usage, with preference for 1


then 7 if ties exist
if next_use > furthest_index or (next_use == furthest_index and f == 1) or
(next_use == furthest_index and page_to_replace != 1 and f == 7):
furthest_index = next_use
page_to_replace = f
frames.remove(page_to_replace)
frames.append(page)
print(f"Step {i + 1}: {frames} (Page fault)")
else:
print(f"Step {i + 1}: {frames} (No page fault)")

print(f"\nTotal page faults: {page_faults}")


print(f"Final state of frames: {frames}")

def main():
print("Optimal Page Replacement Algorithm Simulation")

page_requests = input("Enter the page requests as space-separated integers (10-20


requests): ")
Tristan Merline Dela Cruz BSCpE 4-1
Engr. Obamos November 04, 2024

page_requests = list(map(int, page_requests.split()))

if not (10 <= len(page_requests) <= 20):


print("Error: Please enter between 10 and 20 page requests.")
return

frame_count = int(input("Input the number of frames (3-5): "))

if not (3 <= frame_count <= 5):


print("Error: Please enter a frame count between 3 and 5.")
return

optimal_page_replacement(page_requests, frame_count)

if __name__ == "__main__":
main()

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