-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
150 lines (122 loc) · 5.18 KB
/
main.py
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
140
141
142
143
144
145
146
147
148
149
150
import threading #Importing necessary modules
import time
diskAccessCount = 0 #Initialising variables with default values
pageTable = []
SIGCONT = False
SIGUSR1 = False
def printPageTable(): #Printing entries in Pagetable
global pageTable
for i in pageTable:
print(i)
print('\n\n')
def initialisePageTable(n): #Initialising page table with necessary values
global pageTable
for i in range(n):
pageTable.append([0,-1,0,0,0]) #Each entry from left denotes valid bit, frame allocated, dirty bit, requested, counter(Used for LRU purpose)
def freeFrame(totalFrames,usedFrameCount): #Function to check for freeframes
if(totalFrames > usedFrameCount):
return True
else:
return False
def foundNonZero(): #Checking for Non zeron value in pagetable
global pageTable
for x in pageTable:
if(x[3]!=0):
return True
return False
def OS(pages, frames): #OS function taking 2 arguments
global SIGCONT
global SIGUSR1
global pageTable
initialisePageTable(pages)
#printPageTable()
while(1):
if SIGCONT == True:
SIGCONT = False
if(foundNonZero()):
global diskAccessCount
usedFrameCount = 0
for x in pageTable: #Scanning through the page table for non-zero
if(x[3]!=0):
for y in pageTable:
if(y[0]):
usedFrameCount += 1
if(freeFrame(int(frames),int(usedFrameCount))):
x[1] = usedFrameCount #
x[0] = 1
x[2] = 0
x[3] = 0
x[4] = 0
diskAccessCount += 1
else:
min = 100000
for i in range(len(pageTable)): #Loop to find Victim page
if pageTable[i][0]:
if pageTable[i][4] <= min:
min = pageTable[i][4]
v = i
if pageTable[v][2] == 1: #Condition to check whether the victim page is dirty
time.sleep(1);
diskAccessCount += 1 #Diskaccesses has been incremented
f = pageTable[v][1] #Updation of Page Table
pageTable[v][0] = 0
pageTable[v][1] =-1
pageTable[v][2] = 0
pageTable[v][3] = 0
pageTable[v][4] = 0
time.sleep(1)
diskAccessCount += 1 #Diskaccesses has been incremented
x[1] = f
x[0] = 1
x[2] = 0
x[3] = 0
x[4] = 0
SIGUSR1 = True
continue
else:
break #exit the loop
def inRAM(pno): #checks whether the page is in RAM
global pageTable
return pageTable[pno][0]
def MMU(p, ref_str, pid): #Mentioning the PID to the requested field for that page
c = 0
global pageTable
global diskAccessCount
while True:
if len(pageTable) == p:
break
printPageTable();
global SIGCONT
global SIGUSR1
l = ref_str.split(' ')
for i in l:
request = i[0]
page = int(i[1])
if not inRAM(page): #Simulation of Page Fault
pageTable[page][3] = 1234
SIGCONT = True
while True:
if SIGUSR1 == True:
SIGUSR1 = False
break
if request == 'W': #updation of Dirty bit if it is a write access
pageTable[page][2] = 1
pageTable[page][4] = c #Printing of Updated page table
print(i)
printPageTable()
c += 1 #Counter used for the purpose of LRU replacement
SIGCONT = True
time.sleep(2)
if __name__ == "__main__":
# creating thread
t1 = threading.Thread(target=OS, args=(5,3,))
t2 = threading.Thread(target=MMU, args=(5,"R0 R1 R1 W3 R0 R2 R2 W4 R0 R2 R2 W4 R0 R2 R2 W4 R0 R1 R1 W3 R0 R1 R1 W3", 10,))
# starting thread 1
t1.start()
# starting thread 2
t2.start()
# wait until thread 1 is completely executed
t1.join()
# wait until thread 2 is completely executed
t2.join()
print("Number of disc accesses is " + str(diskAccessCount))