0% found this document useful (0 votes)
60 views11 pages

OS LAB A3 Kiran

The document contains C code for two page replacement algorithms: MRU and Optimal. For MRU, the code implements a page frame data structure to track page numbers and frequencies, and uses functions to find pages, free frames, and the most frequently used frame. The Optimal code tracks page hits and faults, uses an array to simulate the frame table, and replaces the frame with the furthest page in the future if no empty frames exist.

Uploaded by

Soha
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)
60 views11 pages

OS LAB A3 Kiran

The document contains C code for two page replacement algorithms: MRU and Optimal. For MRU, the code implements a page frame data structure to track page numbers and frequencies, and uses functions to find pages, free frames, and the most frequently used frame. The Optimal code tracks page hits and faults, uses an array to simulate the frame table, and replaces the frame with the furthest page in the future if no empty frames exist.

Uploaded by

Soha
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/ 11

SAP ID: 70069458

Student Name: M. Haider Wasim


Course Name: Operating System-LAB
Assignment No.: 3
Submission Date: 22nd May,2021
Question # 1
C code to implement Most Recently Used (MRU) Page Algorithm.
CODE:
#include<stdio.h>

struct node

int pno,freq;

}frames[20];

int n;

int page_found(int pno)

int fno;

for(fno=0;fno<n;fno++)

if(frames[fno].pno==pno)

return fno;

return -1;

int get_free_frame()

{ int fno;

for (fno=0; fno<=n; fno++)

if (frames[fno].pno==-1)

return(fno);

return(-1);
}

int get_mfu_frame()

int fno;

int selfno=0;

for (fno=1; fno<n; fno++)

if(frames[fno].freq>frames[selfno].freq)

selfno=fno;

return selfno;

void main()

int p_request[]={5,8,10,14,10,9,5,10,8,5,1,10,9,12,10};

int size=15;

int page_falts=0,i,j,fno;

printf("\n Page frames:"); scanf("%d",&n);

//initialize frames

for (i=0; i<n; i++)

{ frames[i].pno=-1;

frames[i].freq=0;

printf("\nPageNo PageFrames PageFault");

printf("\n---------------------------------------------------");

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

j=page_found(p_request[i]);

if(j==-1)

j=get_free_frame();

if (j==-1)

j=get_mfu_frame();

page_falts++;

frames[j].pno=p_request[i];

frames[j].freq=1;

printf("\n%4d\t ",p_request[i]);

for (fno=0; fno<n; fno++)

printf("%4d:%2d",frames[fno].pno,frames[fno].freq);

printf(" : YES");

else

printf("\n%4d\t ",p_request[i]);

frames[j].freq++;

for (fno=0; fno<n; fno++)

printf("%4d:%2d",frames[fno].pno,frames[fno].freq);

printf(" : NO");

printf("\n-------------------------------------------------------");
printf("\n PageFaults=%d",page_falts);

OUTPUT:
Question # 2
C code to implement Optimal Page Replacement Algorithm.
CODE:
#include <stdio.h>

#include<sys/types.h>

#include<unistd.h>

int main()

int no_of_frames;

int no_of_pages, frames[10];

int pages[30];

int temp[10];

int flag1;

int flag2;

int flag3;

int i;

int j;

int k;

int pos;

int max;

int hit= 0;

int 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 = 1;

hit++;

break;

if(flag1 == 0)

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

if(frames[j] == -1)

faults++;

frames[j] = pages[i];

flag2 = 1;

break;

if(flag2 == 0)

flag3 =0;

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

temp[j] = -1;

for(k = i + 1; k < no_of_pages; ++k)

if(frames[j] == pages[k])

temp[j] = k;

break;
}

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

if(temp[j] == -1)

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);

printf("\n\nTotal Page hits = %d", hit);

return 0;

}
OUTPUT:

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