Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Replacement C

The document contains three implementations of page replacement algorithms: FIFO, LRU, and Optimal. Each algorithm is designed to manage memory frames and track page faults based on a reference string input by the user. The output for each algorithm includes the frames after each page reference and the total number of page faults encountered.

Uploaded by

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

Replacement C

The document contains three implementations of page replacement algorithms: FIFO, LRU, and Optimal. Each algorithm is designed to manage memory frames and track page faults based on a reference string input by the user. The output for each algorithm includes the frames after each page reference and the total number of page faults encountered.

Uploaded by

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

#include <stdio.

h>
#include <stdbool.h>
void print_frames(int frames[],int n)
{
for(int i=0;i<n;i++)
{
if(frames[i]==-1)
{
printf(" ");
}
else
{
printf("%d",frames[i]);
}
printf("\t");
}
printf("\n");
}
bool search(int key,int frames[],int n)
{
for(int i=0;i<n;i++)
{
if(frames[i]==key)
{
return true;
}
}
return false;
}
void pageReplacement(int pages[],int n,int frames[],int frameSize)
{
int pageFaults=0;
int index=0;
for(int i=0;i<n;i++)
{
printf("%d:\t",pages[i]);
if(!search(pages[i],frames,frameSize))
{
frames[index]=pages[i];
index=(index+1)%frameSize;
pageFaults++;
print_frames(frames,frameSize);
}
else
{
print_frames(frames,frameSize);
}
}

printf("Total Page Faults:%d\n",pageFaults);


}
int main()
{
int maxFrames;
printf("Enter the No. of Frames: ");
scanf("%d",&maxFrames);
int pages[100],n;
printf("Enter the reference string(-1 to end): ");
int page,i=0;
while(scanf("%d",&page)==1 && page!=-1)
{
pages[i++]=page;
}
n=i;
int frames[maxFrames];
for(int i=0;i<maxFrames;i++)
{
frames[i]=-1;
}
printf("FIFO PAGE REPLACEMENT ALGORITHM\n");
printf("-------------------------------\n");
printf("Page\tFrames\n");
printf("-------------------------------\n");
pageReplacement(pages,n,frames,maxFrames);
return 0;
}

OUTPUT
ubuntu@L5SYS20:~/cs1$ gedit fifo.c
ubuntu@L5SYS20:~/cs1$ gcc fifo.c
ubuntu@L5SYS20:~/cs1$ ./a.out
Enter the No. of Frames: 3
Enter the reference string(-1 to end): 1 2 3 4 1 2 5 1 2 3 4 5
-1
FIFO PAGE REPLACEMENT ALGORITHM
-------------------------------
Page Frames
-------------------------------
1: 1
2: 1 2
3: 1 2 3
4: 4 2 3
1: 4 1 3
2: 4 1 2
5: 5 1 2
1: 5 1 2
2: 5 1 2
3: 5 3 2
4: 5 3 4
5: 5 3 4
Total Page Faults:9
b)LRU
PROGRAM
#include <stdio.h>
#include <stdbool.h>
void print_frames(int frames[], int n) {
for (int i = 0; i < n; i++) {
if (frames[i] == -1) {
printf(" ");
} else {
printf("%d", frames[i]);
}
printf("\t");
}
printf("\n");
}
bool search(int key, int frames[], int n) {
for (int i = 0; i < n; i++) {
if (frames[i] == key) {
return true;
}
}
return false;
}
int findLRU(int time[], int n) {
int min = time[0], pos = 0;
for (int i = 1; i < n; i++) {
if (time[i] < min) {
min = time[i];
pos = i;
}
}
return pos;
}
void pageReplacement(int pages[], int n, int frames[], int frameSize) {
int pageFaults = 0;
int counter = 0;
int time[frameSize];
for (int i = 0; i < frameSize; i++) {
time[i] = 0;
}
for (int i = 0; i < n; i++) {
printf("%d:\t", pages[i]);
bool hit = false;
for (int j = 0; j < frameSize; j++)
{
if (frames[j] == pages[i]) {
hit = true;
time[j] = ++counter;
break;
}
}
if (!hit) {
int lru = findLRU(time,frameSize);
frames[lru] = pages[i];
time[lru] = ++counter;
pageFaults++;
}
print_frames(frames, frameSize);
}
printf("Total Page Faults: %d\n", pageFaults);
}
int main() {
int maxFrames;
printf("Enter the No. of Frames: ");
scanf("%d", &maxFrames);
int pages[100], n;
printf("Enter the reference string (-1 to end): ");
int page, i = 0;
while (scanf("%d", &page) == 1 && page != -1) {
pages[i++] = page;
}
n = i;
int frames[maxFrames];
for (int i = 0; i < maxFrames; i++) {
frames[i] = -1;
}
printf("LRU PAGE REPLACEMENT ALGORITHM\n");
printf("-------------------------------\n");
printf("Page\tFrames\n");
printf("-------------------------------\n");
pageReplacement(pages, n, frames, maxFrames);
return 0;
}
OUTPUT
ubuntu@L5SYS20:~/cs1$ gedit lru.c
ubuntu@L5SYS20:~/cs1$ gcc lru.c
ubuntu@L5SYS20:~/cs1$ ./a.out
Enter the No. of Frames: 3
Enter the reference string (-1 to end): 1 2 3 4 1 2 5 1 2 3 4 5
-1
LRU PAGE REPLACEMENT ALGORITHM
-------------------------------
Page Frames
-------------------------------
1: 1
2: 1 2
3: 1 2 3
4: 4 2 3
1: 4 1 3
2: 4 1 2
5: 5 1 2
1: 5 1 2
2: 5 1 2
3: 3 1 2
4: 3 4 2
5: 3 4 5
Total Page Faults: 10

c)OPTIMAL
PROGRAM

#include <stdio.h>
#include <stdbool.h>
void printFrames(int frames[], int n) {
for (int i = 0; i < n; i++) {
if (frames[i] == -1)
printf(" \t");
else
printf("%d\t", frames[i]);
}
printf("\n");
}
bool search(int key, int frames[], int n) {
for (int i = 0; i < n; i++) {
if (frames[i] == key)
return true;
}
return false;
}
int predict(int pages[], int frames[], int n, int index, int frameSize) {
int result = -1, farthest = index;
for (int i = 0; i < frameSize; i++) {
int j;
for (j = index; j < n; j++) {
if (frames[i] == pages[j]) {
if (j > farthest) {
farthest = j;
result = i;
}
break;
}
}
if (j == n)
return i;
}
return (result == -1) ? 0 : result;
}
void pageReplacementOptimal(int pages[], int n, int frames[], int frameSize) {
int pageFaults = 0;
for (int i = 0; i < n; i++) {
printf("%d:\t", pages[i]);
if (!search(pages[i], frames, frameSize)) {
int j;
for (j = 0; j < frameSize; j++) {
if (frames[j] == -1) {
frames[j] = pages[i];
break;
}
}
if (j == frameSize) {
int pos = predict(pages, frames, n, i + 1, frameSize);

frames[pos] = pages[i];
}
pageFaults++;
}
printFrames(frames, frameSize);
}
printf("Total Page Faults: %d\n", pageFaults);
}
int main() {
int maxFrames;
printf("Enter the number of Frames: ");
scanf("%d", &maxFrames);
int pages[100];
int n;
printf("Enter the reference String (-1 to end): ");
int page, i = 0;
while (scanf("%d", &page) == 1 && page != -1) {
pages[i++] = page;
}
n = i;
int frames[maxFrames];
for (i = 0; i < maxFrames; i++) {
frames[i] = -1;
}
printf("Optimal Page Replacement Algorithm\n");
printf("________________\n");
printf("Page\tFrames\n");
printf("___________\n");
pageReplacementOptimal(pages, n, frames, maxFrames);
return 0;
}

OUTPUT
alfaz@l5sys15:~/SCS1049923$ gedit OPTIMAL.c
alfaz@l5sys15:~/SCS1049923$ gcc OPTIMAL.c
alfaz@l5sys15:~/SCS1049923$ ./a.out
Enter the number of Frames: 3
Enter the reference String (-1 to end): 1 2 3 4 1 2 5 1 2 3 4 5 -1
Optimal Page Replacement Algorithm
________________
Page Frames
___________
1: 1
2: 1 2
3: 1 2 3
4: 1 2 4
1: 1 2 4
2: 1 2 4
5: 1 2 5
1: 1 2 5
2: 1 2 5

3: 3 2 5
4: 4 2 5
5: 4 2 5
Total Page Faults: 7

You might also like