Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Should simulate a context switch and provide a way of abstraction
- ASSUMES THE WORKQ IS NOT EMPTY
- """
- def contextSwitch(self):
- #Calculate the new time slice if its RR
- if(self.algorithm == "RR"):
- self.nxtSlice = self.rTime + self.tSlice + self.cSwitchTime
- #What kind of switch are we doing?
- if(self.cProc is None):
- #Loading a new process
- time = self.cSwitchTime / 2
- else:
- #potentially doing either first half, or full depending on state of
- #workQ
- time = self.cSwitchTime
- #Simulate time cycles.
- for i in range(1, time + 1):
- #Step the workQ, this will handle all conflicts in timing
- #Inherently since we're simulating each step instead of
- #Calculating the future
- self.__step_workQ()
- #We swap out processes
- if(i == self.cSwitchTime / 2):
- #The workQ is empty
- if(self.workQ.isEmpty()):
- #Half of context switch
- self.cProc = None
- break
- #Put the old proc in procPool
- #This will later be put in correct spot when self.__step_workQ() executes
- if(not self.cProc is None): self.procPool.append(self.cProc)
- # Load the new Proc. Change state to running
- # NOTE: WHILE ALL OF THIS IS BEING DONE, WE DON'T STEP
- # THESE PROCESSES. DURING THE FIRST HALF OF CSWITCH,
- # WE ARE "lOADING" THE PROC OUT OF PROCESSOR. NOW WE ARE LOADING
- # NEW PROC IN. THE OLD PROC WILL BE STEPPED() IN THE WORKQ STEPPER
- newProc = self.workQ.dequeue()
- self.cProc = newProc
- self.cProc
- self.cProc.stateChange("RUNNING")
- #Increment the rTime
- self.rTime += 1
- # Increment the context switch counter.
- # Here we have completed the context switch
- self.cSwitchAmt += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement