0% found this document useful (0 votes)
146 views4 pages

To Implement Optimal Page Replacement Algorithms.: Experiment 6 Aim

The document describes an experiment to implement optimal page replacement algorithms. It discusses the theory behind optimal page replacement, which replaces the page that will not be used for the maximum time in the future. The code implements optimal page replacement by tracking the pages in frames and finding the page to replace that will be accessed farthest in the future. It tracks page faults and replaces pages optimally to minimize faults.

Uploaded by

Romeo Jatt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views4 pages

To Implement Optimal Page Replacement Algorithms.: Experiment 6 Aim

The document describes an experiment to implement optimal page replacement algorithms. It discusses the theory behind optimal page replacement, which replaces the page that will not be used for the maximum time in the future. The code implements optimal page replacement by tracking the pages in frames and finding the page to replace that will be accessed farthest in the future. It tracks page faults and replaces pages optimally to minimize faults.

Uploaded by

Romeo Jatt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Tejal Borase

TY IT BATCH D
191081902
EXPERIMENT 6
AIM:
To implement Optimal Page Replacement Algorithms.
THEORY:
Optimal Page Replacement Algorithm

Optimal page replacement algorithm says that if page fault occurs then that
page should be removed that will not be used for maximum time in future.

It is also known as clairvoyant replacement algorithm or Bélády’s optimal


page replacement policy.

Advantages of Optimal Page Replacement Algorithm are as follows:

1) It is less complex and easy to implement.

2) A page is replaced with minimum fuss.

3) Simple data structures are used for this purpose.

Disadvantages of Optimal Replacement Algorithm are as follows:

1) Not all operating systems can implement this algorithm.

2) Error detection is harder.

3) Least recently used page will be replaced which may sometimes take a lot
of time.

CODE:
#include <bits/stdc++.h>
using namespace std;
const int N=100005;
Tejal Borase
TY IT BATCH D
191081902
int n;
int frame_size;
int pages[N];

void optimal_page_replacement()
{
vector<int> fr;
int page_faults = 0;
for (int i = 0; i < n; i++)
{
int k;
for (k = 0; k < fr.size(); k++)
if (fr[k] == pages[i])
break;
if (k==fr.size())
{
if (fr.size() < frame_size)
fr.push_back(pages[i]);

else
{
int index=i+1;
int res = -1, farthest = index;
for (int l = 0; l < fr.size(); l++)
{
int j;
for (j = index; j < n; j++)
{
if (fr[l] == pages[j])
{
if (j > farthest)
{
farthest = j;
res = l;
}
break;
Tejal Borase
TY IT BATCH D
191081902
}
}
if (j == n)
{
res=l;
break;
}
}
fr[res] = pages[i];
}
page_faults++;
cout<<pages[i]<<" Fault\n";
//cout<<"Reference to page "<<pages[i]<<" caused a page fault\n";
}
else
{
cout<<pages[i]<<" No page fault\n";
//cout<<"Reference to page "<<pages[i]<<" did not cause a page
fault\n";
}
}
cout<<"\nTotal Page Faults: "<<page_faults;
}

int main()
{
cout<<"Enter number of frames: ";
cin>>frame_size;

cout<<"Enter Page Reference String Length: ";


cin>>n;

cout<<"Enter Page Reference String:\n";


for(int i=0; i<n; i++)
cin>>pages[i];
optimal_page_replacement();
Tejal Borase
TY IT BATCH D
191081902
return 0;
}
Output:

CONCLUSION:
In this experiment we have successfully implemented OPTIMAL page replacement
algorithms

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