-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ex_9_8_optimal.sci
90 lines (90 loc) · 2.98 KB
/
Ex_9_8_optimal.sci
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
//This Source file is written by Nikita Rath (18BLC1131)
//FUNCTION FOR OPTIMAL
function [] = op(a,n)
printf("\tNo. of Frames \t Optimal")
x = 1
//for frames 1 to 7
while(x < 8)
counter = 0
faults = 0
//Intialise all frames to -1
for i = 1:x
frames(i) = -1
end
//Traversing through pages
for i = 1:n
flag1 = 0
flag2 = 0
//Traversing through frames
for j = 1:x
//Page available in frame
if(frames(j) == a(i)) then
//Set flag1 and flag2
flag1 = 1
flag2 = 1
break
end
end
//Page unavailable in frame
if(flag1 == 0) then
//Traversing through frames
for j = 1:x
//Frame has empty space
if(frames(j) == -1) then
//Increment page faults
faults = faults + 1
frames(j) = a(i)
flag2 = 1
break
end
end
end
//Page unavailable in frame
if(flag2 == 0) then
flag3 =0
//Traversing through frames
for j = 1:x
temp(j) = -1
//Traversing through future pages
for k = i+1:n
//Page currently in frame available in future pages
if(frames(j) == a(k)) then
temp(j) = k
break
end
end
end
//Traversing through frames
for j = 1:x
//page currently in frame unavailable in future pages
if(temp(j) == -1) then
pos = j
//Set flag3
flag3 = 1
break
end
end
//Page currently in frame available in future pages
if(flag3 ==0) then
maximum = temp(1)
pos = 1
//Traversing through frames
for j = 2:x
//Find page in frame which is farther in future pages
if(temp(j) > maximum) then
maximum = temp(j)
pos = j
end
end
end
frames(pos) = a(i)
//Increment page faults
faults = faults + 1
end
end
//Total page faults
printf("\n\t\t %d \t %d ", x, faults)
x = x + 1
end
printf("\n")
endfunction