0% found this document useful (0 votes)
15 views7 pages

Os Week 7 Record

The document outlines an experiment on Page Replacement Algorithms in an Operating Systems Laboratory, specifically focusing on FIFO, LRU, and Optimal algorithms. It includes C code implementations for each algorithm, detailing how pages are managed in memory and how page faults are counted. Each section provides input prompts and outputs the total number of page faults after processing the reference strings.

Uploaded by

Kolli Bavitha
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)
15 views7 pages

Os Week 7 Record

The document outlines an experiment on Page Replacement Algorithms in an Operating Systems Laboratory, specifically focusing on FIFO, LRU, and Optimal algorithms. It includes C code implementations for each algorithm, detailing how pages are managed in memory and how page faults are counted. Each section provides input prompts and outputs the total number of page faults after processing the reference strings.

Uploaded by

Kolli Bavitha
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/ 7

VNR VJIET Name of the experiment:

Name of the Laboratory: Page Replacement Algorithms


Operating Systems Laboratory Experiment No: 6

Implementation of the following Page Replacement Algorithms a) FIFO b)


LRU c) optimal

a) FIFO
#include <stdio.h>
#include <conio.h>

void main() {
int i, j, n, a[50], frame[10], avail, count = 0, no;
clrscr();
printf("Enter the number of pages: ");
scanf("%d", &n);
printf("Enter the page numbers: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter the number of frames: ");
scanf("%d", &no);
for (i = 0; i < no; i++) {
frame[i] = -1;
}
j = 0;
printf("\nRef String \t Page Frames\n");

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


printf("%d\t\t", a[i]);
avail = 0;
for (int k = 0; k < no; k++) {
if (frame[k] == a[i]) {
avail = 1;
break;
}
}

if (avail == 0) {
frame[j] = a[i];
j = (j + 1) % no;
count++;
}

for (int k = 0; k < no; k++) {


if (frame[k] != -1)
printf("%d\t", frame[k]);
else
printf("-\t");
}

printf("\n");
}

printf("\nTotal page faults: %d\n", count);


}

Output-
VNR VJIET Name of the experiment:
Name of the Laboratory: Page Replacement Algorithms
Operating Systems Laboratory Experiment No: 6

b) optimal
#include <stdio.h>
int main() {
int F, P, frames[10], a[30], temp[10];
int flag1, flag2, flag3, i, j, k, pos, max, faults = 0;

printf("Enter Number of Frames: ");


scanf("%d", &F);
printf("Enter Number of Pages: ");
scanf("%d", &P);
printf("Enter Page Reference String: ");
for (i = 0; i < P; i++) {
scanf("%d", &a[i]);
}

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


frames[i] = -1;
}

printf("\nReference String \t Page Frames\n");


for (i = 0; i < P; i++) {
printf("%d\t\t", a[i]);
flag1 = flag2 = 0;

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


if (frames[j] == a[i]) {
flag1 = flag2 = 1;
break;
}
}

if (flag1 == 0) {
for (j = 0; j < F; j++) {
if (frames[j] == -1) {
frames[j] = a[i];
flag2 = 1;
faults++;
break;
}
}
}

if (flag2 == 0) {
flag3 = 0;
for (j = 0; j < F; j++) {
temp[j] = -1;
for (k = i + 1; k < P; k++) {
if (frames[j] == a[k]) {
temp[j] = k;
break;
}
}
}

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


if (temp[j] == -1) {
pos = j;
flag3 = 1;
break;
}
}

if (flag3 == 0) {
max = temp[0];
pos = 0;
for (j = 1; j < F; j++) {
if (temp[j] > max) {
max = temp[j];
pos = j;
}
}
}

frames[pos] = a[i];
faults++;
}

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


if (frames[j] != -1)
printf("%d\t", frames[j]);
else
printf("-\t");
}
printf("\n");
}

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


return 0;
}
VNR VJIET Name of the experiment:
Name of the Laboratory: Page Replacement Algorithms
Operating Systems Laboratory Experiment No: 6

Output-

c) LRU
#include <stdio.h>
int main() {
int a[20], p[50], c = 0, c1, d, f, k = 0, n, o, t, b[20], c2[20];
int i, j, r;

printf("Enter number of pages: ");


scanf("%d", &n);

printf("Enter the reference string: ");


for (i = 0; i < n; i++)
scanf("%d", &p[i]);

printf("Enter number of frames: ");


scanf("%d", &f);

printf("\n");

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


a[i] = -1; // Initialize frames

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


c1 = 0;
for (j = 0; j < f; j++) {
if (a[j] == p[i]) {
c1 = 1;
break;
}
}

if (c1 == 0) {
if (k < f) {
a[k] = p[i];
k++;
} else {
int max = -1, pos = -1;
for (r = 0; r < f; r++) {
c2[r] = 0;
for (j = i + 1; j < n; j++) {
if (a[r] == p[j])
break;
c2[r]++;
}
if (c2[r] > max) {
max = c2[r];
pos = r;
}
}
a[pos] = p[i];
}
c++;
}

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


printf("%d\t", a[j]);
printf("\n");
}

printf("Total page faults: %d\n", c);


return 0;
}
VNR VJIET Name of the experiment:
Name of the Laboratory: Page Replacement Algorithms
Operating Systems Laboratory Experiment No: 6

Output-

You might also like