-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimalPRA.c
118 lines (97 loc) · 3.82 KB
/
optimalPRA.c
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
/* C implementation of the Optimimal Page Replacement Algorithm.
In a computer operating system that used paging for virtual
memory management, page replacement algorithms decide which memory
pages to page out (swap out) or write to disk when a page of memory needs
to be allocated. Page replacemant happens when a requested page is not
in memory (page fault) and a free page cannot be used to satsify the
allocation, either because there are none, or because the number of
free pages is below some thresholds.The goal of all PRA is to reduce the
number of page faults. In an optiminal page replacemet algorithm
the OS replaces the page that will not be used for the longest period
of time in the future. The idea is simple: 1) if referenced page is
already present, increment the hit count. 2) If the page is not present,
find a page that is never referenced in the future. If such a page exists.,
replace this page with new page. If not such page exists, find a page that
is references farthest in the future. Replace this page with new page.
The time complexity of the algorithm depends on the number of page
references and the number of frames. Worst case complexity is
O(pn * fn^2) which occurs when all page references are unique and
there are no empty frames available. In this case, for each page
reference, we may have to iterate through all the frames to check if
the page is present, and then iterate through all the remaining references
to find the page that will not be needed for the longest period of time
in the future. In practice, however, the algorithm performs much better
than its worse case scenario, as it is rare to have all page references
unique, and the number of frames is usually limited. */
#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 = 1;
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);
return 0;
}