-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaging.c
More file actions
140 lines (120 loc) · 3.91 KB
/
paging.c
File metadata and controls
140 lines (120 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX_PAGES 100
#define MAX_FRAMES 10
int referenceString[MAX_PAGES];
int numPages, numFrames;
// Function to find if a page exists in frames
bool isInFrames(int page, int frames[], int n) {
for (int i = 0; i < n; i++) {
if (frames[i] == page) {
return true;
}
}
return false;
}
// Function to find the index of the page in frames
int indexOfPage(int page, int frames[], int n) {
for (int i = 0; i < n; i++) {
if (frames[i] == page) {
return i;
}
}
return -1;
}
// FIFO Page Replacement Algorithm
int FIFO(int referenceString[], int numPages, int numFrames) {
int frames[MAX_FRAMES];
int pageFaults = 0;
int frameIndex = 0;
for (int i = 0; i < numPages; i++) {
if (!isInFrames(referenceString[i], frames, numFrames)) {
pageFaults++;
frames[frameIndex] = referenceString[i];
frameIndex = (frameIndex + 1) % numFrames;
}
}
return pageFaults;
}
// LRU Page Replacement Algorithm
int LRU(int referenceString[], int numPages, int numFrames) {
int frames[MAX_FRAMES];
int pageFaults = 0;
int recentUsed[MAX_FRAMES] = {0}; // to keep track of recent usage of pages
for (int i = 0; i < numPages; i++) {
if (!isInFrames(referenceString[i], frames, numFrames)) {
pageFaults++;
int min = recentUsed[0];
int minIndex = 0;
for (int j = 1; j < numFrames; j++) {
if (recentUsed[j] < min) {
min = recentUsed[j];
minIndex = j;
}
}
frames[minIndex] = referenceString[i];
recentUsed[minIndex] = i + 1;
} else {
int index = indexOfPage(referenceString[i], frames, numFrames);
recentUsed[index] = i + 1;
}
}
return pageFaults;
}
// Optimal Page Replacement Algorithm
int Optimal(int referenceString[], int numPages, int numFrames) {
int frames[MAX_FRAMES];
int pageFaults = 0;
for (int i = 0; i < numPages; i++) {
if (!isInFrames(referenceString[i], frames, numFrames)) {
pageFaults++;
int replaceIndex = -1;
for (int j = 0; j < numFrames; j++) {
int k;
for (k = i + 1; k < numPages; k++) {
if (frames[j] == referenceString[k]) {
break;
}
}
if (k == numPages) {
replaceIndex = j;
break;
}
}
if (replaceIndex == -1) {
replaceIndex = 0;
for (int j = 1; j < numFrames; j++) {
int k;
for (k = i + 1; k < numPages; k++) {
if (frames[j] == referenceString[k]) {
break;
}
}
if (k > replaceIndex) {
replaceIndex = j;
}
}
}
frames[replaceIndex] = referenceString[i];
}
}
return pageFaults;
}
int main() {
printf("Enter the number of pages in reference string: ");
scanf("%d", &numPages);
printf("Enter the reference string: ");
for (int i = 0; i < numPages; i++) {
scanf("%d", &referenceString[i]);
}
printf("Enter the number of frames: ");
scanf("%d", &numFrames);
int fifoFaults = FIFO(referenceString, numPages, numFrames);
int lruFaults = LRU(referenceString, numPages, numFrames);
int optimalFaults = Optimal(referenceString, numPages, numFrames);
printf("\nNumber of page faults for FIFO: %d\n", fifoFaults);
printf("Number of page faults for LRU: %d\n", lruFaults);
printf("Number of page faults for Optimal: %d\n", optimalFaults);
return 0;
}