# Hussain Abbas | Iraq | University Of Baghdad | Social Media : hussain7abbas
class ClockReplacement():
def __init__(self, procs:list, noOfWindows:int):
self.procs = procs
self.noOfWindows = noOfWindows
self.procsWindow = [' ']*noOfWindows
self.signsWindow = [False]*noOfWindows # Used Bits = 0
self.pointer = 0
self.pageFault = 0
def getFullResults(self):
self.pageFault = 0
for proc in self.procs:
status = ''
if proc in self.procsWindow: # if proc exist in current window then:
self.signsWindow[self.procsWindow.index(proc)] = True # current proc used bit = 1
status = '2nd_chance'
else:
if not ' ' in self.procsWindow:
status = 'page_fault'
self.pageFault += 1
while(self.signsWindow[self.pointer] == True): # while pointer used bit = 1 then
self.signsWindow[self.pointer] = False # pointer used bit = 0
self.pointer = self.pointer + 1 if self.pointer < self.noOfWindows-1 else 0 # pointer = next position
# Now the pointer is points to a proc that has used bit = 0
self.procsWindow[self.pointer] = proc # current pointer's proc = the new proc added
self.signsWindow[self.pointer] = True # current pointer used bit = 1
self.pointer = self.pointer + 1 if self.pointer < self.noOfWindows-1 else 0 # pointer = next position
self.printWindow(proc, status) # out window as a row in terminal (eventually will make table)
def printWindow(self, proc, status):
outWindow = self.procsWindow.copy()
# Add 'used bit' sign = *
for i in range(len(self.procsWindow)):
if (self.signsWindow[i] == True):
outWindow[i] = ' {}* '.format(str(self.procsWindow[i]))
else:
outWindow[i] = ' {} '.format(str(self.procsWindow[i]))
# Add pointer sign
if self.signsWindow[self.pointer] == True:
outWindow[self.pointer] = '>> {}* <<'.format(self.procsWindow[self.pointer])
else:
outWindow[self.pointer] = '>> {} <<'.format(self.procsWindow[self.pointer])
# Make nice looking row in terminal
print('proc {}'.format(proc), end=' = Window | ')
print(*outWindow, end=' | ', sep=' | ')
if status == '2nd_chance':
print('2'nd Chance')
elif status == 'page_fault':
print('Page Fault No.:', self.pageFault)
else:
print()
clock_list1 = ClockReplacement([2,4,3,1,5,1,6,2,4,5,3,1], 4)
clock_list1.getFullResults()