Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1.     """
  2.    Should simulate a context switch and provide a way of abstraction
  3.  
  4.    ASSUMES THE WORKQ IS NOT EMPTY
  5.    """
  6.     def contextSwitch(self):
  7.         #Calculate the new time slice if its RR
  8.         if(self.algorithm == "RR"):
  9.             self.nxtSlice = self.rTime + self.tSlice + self.cSwitchTime
  10.  
  11.  
  12.         #What kind of switch are we doing?
  13.         if(self.cProc is None):
  14.             #Loading a new process
  15.             time = self.cSwitchTime / 2
  16.         else:
  17.             #potentially doing either first half, or full depending on state of
  18.             #workQ
  19.             time = self.cSwitchTime
  20.  
  21.         #Simulate time cycles.
  22.         for i in range(1, time + 1):
  23.             #Step the workQ, this will handle all conflicts in timing
  24.             #Inherently since we're simulating each step instead of
  25.             #Calculating the future
  26.             self.__step_workQ()
  27.  
  28.             #We swap out processes
  29.             if(i == self.cSwitchTime / 2):
  30.                 #The workQ is empty
  31.                 if(self.workQ.isEmpty()):
  32.                     #Half of context switch
  33.                     self.cProc = None
  34.                     break
  35.  
  36.                 #Put the old proc in procPool
  37.                 #This will later be put in correct spot when self.__step_workQ() executes
  38.                 if(not self.cProc is None): self.procPool.append(self.cProc)
  39.  
  40.                 # Load the new Proc. Change state to running
  41.                 # NOTE: WHILE ALL OF THIS IS BEING DONE, WE DON'T STEP
  42.                 # THESE PROCESSES. DURING THE FIRST HALF OF CSWITCH,
  43.                 # WE ARE "lOADING" THE PROC OUT OF PROCESSOR. NOW WE ARE LOADING
  44.                 # NEW PROC IN. THE OLD PROC WILL BE STEPPED() IN THE WORKQ STEPPER
  45.                 newProc = self.workQ.dequeue()
  46.                 self.cProc = newProc
  47.                 self.cProc
  48.                 self.cProc.stateChange("RUNNING")
  49.  
  50.             #Increment the rTime
  51.             self.rTime += 1
  52.  
  53.         # Increment the context switch counter.
  54.         # Here we have completed the context switch
  55.         self.cSwitchAmt += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement