0% found this document useful (0 votes)
49 views6 pages

Operating System

This document contains code for implementing two page replacement algorithms: Optimal Page Replacement and FIFO Page Replacement. The Optimal Page Replacement code tracks the frames and pages arrays to determine when to replace pages based on the furthest future reference. The FIFO algorithm simply replaces the oldest page based on its position in the frames array. Source code is provided from online sources for implementing each algorithm.

Uploaded by

Sadia Sumi
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)
49 views6 pages

Operating System

This document contains code for implementing two page replacement algorithms: Optimal Page Replacement and FIFO Page Replacement. The Optimal Page Replacement code tracks the frames and pages arrays to determine when to replace pages based on the furthest future reference. The FIFO algorithm simply replaces the oldest page based on its position in the frames array. Source code is provided from online sources for implementing each algorithm.

Uploaded by

Sadia Sumi
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/ 6

OPerating system

assignment

Name : Sabiha Rahman


I’d : 201911053018
Batch : 53
OPTIMAL:
#include<stdio.h>

int main()

int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2,


flag3, i, j, k, pos, max, faults = 0;

printf("Enter number of frames: ");

scanf("%d", &no_of_frames);

printf("Enter number of pages: ");

scanf("%d", &no_of_pages);

printf("Enter page reference string: ");

for(i = 0; i < no_of_pages; ++i){

scanf("%d", &pages[i]);

for(i = 0; i < no_of_frames; ++i){

frames[i] = -1;

for(i = 0; i < no_of_pages; ++i){

flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j){

if(frames[j] == pages[i]){

flag1 = flag2 =

break;

if(flag1 == 0){

for(j = 0; j < no_of_frames; ++j){

pos = j;

flag3 = 1;
break;

if(flag3 ==0){

max = temp[0];

pos = 0;

for(j = 1; j < no_of_frames; ++j){

if(temp[j] > max){

max = temp[j];

pos = j;

frames[pos] = pages[i];

Faults++;

printf("\n");

for(j = 0; j < no_of_frames; ++j){

printf("%d\t", frames[j]);

printf("\n\nTotal Page Faults = %d", faults);

return 0;

}
Source: https://www.thecrazyprogrammer.com/2016/11/optimal-page-
replacement-algorithm-c.html?fbclid=IwAR1G2M7bBaELiyJTKLddMGou8m1-
EfiXbvsRlnUbsHFHV-ApttT6aAhguCg

FIFO:

#include<stdio.h>

int main()
{
int reference_string[10], page_faults = 0, m, n, s, pages, frames;
printf("\nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter values of Reference String:\n");
for(m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &reference_string[m]);
}
printf("\nEnter Total Number of Frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(reference_string[m] == temp[n])
{
s++;
page_faults--;
}
}
page_faults++;
if((page_faults <= frames) && (s == 0))
{
temp[m] = reference_string[m];
}
else if(s == 0)
{
temp[(page_faults - 1) % frames] = reference_string[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", page_faults);
return 0;
Source : https://www.geeksforgeeks.org/?
fbclid=IwAR2Hgxxh1rhdmmddF8DV2owDtZmF-Hx7W3a1QiPxPpP0Xx5lBT6D8Y25uH8

You might also like