-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ex_9_8_lru.sci
70 lines (70 loc) · 2.17 KB
/
Ex_9_8_lru.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
//This Source file is written by Nikita Rath (18BLC1131)
//FUNCTION FOR LRU
function [] = lru(a,n)
printf("\tNo. of Frames \t LRU")
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
counter = counter + 1
temp(j) = counter
//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
counter = counter + 1
//Increment page faults
faults = faults + 1
frames(j) = a(i)
temp(j) = counter
flag2 = 1
break
end
end
end
//Page unavailable in frame
if(flag2 == 0) then
minimum = temp(1)
pos = 1
//Traversing through frames
for k = 2:x
//Find least recently used page
if(temp(k) < minimum) then
minimum = temp(k)
pos = k
end
end
counter = counter + 1
//Increment page faults
faults = faults + 1
frames(pos) = a(i)
temp(pos) = counter
end
end
//Total page faults
printf("\n\t\t %d \t %d ", x, faults)
x = x + 1
end
printf("\n")
endfunction