Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

clock replacement algorithm python

# 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()
Comment

PREVIOUS NEXT
Code Example
Python :: how to close python in terminal 
Python :: element tree no able to find tag 
Python :: ring Search List Item 
Python :: ring convert string lines to list items and convert the list to a string 
Python :: ring retrieves the list of all algorithms supported by Encrypt()/Decrypt() functions. 
Python :: DELETE c1 FROM tablename c1 INNER JOIN tablename c2 WHERE c1.id c2.id AND c1.unique_field = c2.unique_field; 
Python :: text to ascii art generator python 
Python :: how to deploy django app on heroku with mongodb 
Python :: gfxdraw circle weight 
Python :: python list insert multiple 
Python :: matplotlib plot dpi - change format to svg 
Python :: get next element while looping 
Python :: How to make exit button? 
Python :: instead of: newlist = [] for i in range(1, 100): if i % 2 == 0: newlist.append(i**2) 
Python :: python durchschnitt liste 
Python :: pip is not recognized as an internal or external command 
Python :: how to threshold filter geodataframe by column value 
Python :: python print string in red color 
Python :: '.join(s) 
Python :: python post np.array object 
Python :: python random number 1 100 
Python :: vijay 
Python :: site:www.python-kurs.eu generators 
Python :: python long numbers as string 
Python :: Pandas automatic allignment of columns 
Python :: Remove outliers with median value and Capping 
Python :: _set.filter django 
Python :: place parameters tkinter 
Python :: Python check if caps lock is pressed 
Python :: if elif else ladder in python 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =