Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def contextSwitch(self):
- old = self.processor.cProc # Assign old proc to this
- factor = None # Init this to None, must change or error
- new = -1 # New process to insert into cProc, init to -1 to catch unititialized value
- # Just loading in a new process
- if(self.processor.cProc is None):
- # Loading a new process, do half a cswitch
- factor = 1/2 # Doing half a cSwitch
- time = self.processor.cSwitchTime / 2 # We're doing half a cSwitchTime
- #Since we're doing half a cswitch, pull new process now
- if(not self.processor.workQ.isEmpty()):
- new = self.processor.workQ.dequeue()
- #We know that we're going to have to load out the current process
- else:
- if(self.processor.cProc.state == "BLOCKED"):
- self.processor.procHelper.logBurst()
- writeOutput("time {0}ms: Process {1} switching out of CPU; will block on I/O until time {2}ms {3}\n".format(
- int(self.processor.rTime), self.processor.cProc.label,
- int(self.processor.cProc.IOtimeLeft + (self.processor.rTime) + (self.processor.cSwitchTime / 2)),
- self.processor.procPrinter.getQStr()), self.processor.cProc.label, evenCPU())
- # potentially doing either first half, or full depending on state of
- # workQ
- factor = 1 # We're doing a full cSwitchTime
- time = self.processor.cSwitchTime
- #We're switching in None
- if(self.processor.workQ.isEmpty()):
- # Loading a new process
- factor = 1/2 # We're doing half a cSwitchTime
- time = self.processor.cSwitchTime / 2
- # Simulate time cycles.
- for i in range(1, int(time) + 1):
- self.processor.incrementrTime() # Increment time
- self.processor.step_workQ() # step_workQ to ensure workQ is in valid state
- # We swap out processes
- if(i == self.processor.cSwitchTime / 2):
- #We are removing a process, put it in procPool
- if not old is None:
- self.processor.procPool.append(old)
- #If new is unassigned and there is a new Process
- if(self.processor.workQ.isEmpty() and isinstance(new, int)):
- new = self.processor.workQ.dequeue()
- # Add new proc to self
- #Calculate the new time slice if its RR
- if(self.processor.algorithm == "RR" and old is not None):
- self.processor.nxtSlice = self.processor.rTime + self.processor.tSlice + i
- self.processor.cProc = new
- assert(not isinstance(self.processor.cProc, int))
- if(new is not None):
- self.processor.cProc.stateChange("RUNNING")
- # Increment the context switch counter.
- # Here we have completed the context switch
- if(self.processor.cProc is not None):
- if (self.processor.cProc.burstTimeLeft < self.processor.cProc.burstTime):
- self.processor.startBurst = self.processor.rTime
- writeOutput(
- "time {0}ms: Process {1} started using the CPU with {2}ms remaining {3}\n".format(
- int(self.processor.rTime), self.processor.cProc.label,
- self.processor.cProc.burstTimeLeft,
- self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
- else:
- self.processor.startBurst = self.processor.rTime
- writeOutput("time {0}ms: Process {1} started using the CPU {2}\n".format(self.processor.rTime, self.processor.cProc.label, self.processor.procPrinter.getQStr()), self.processor.cProc.label, evenCPU())
- #Calculate the new time slice if its RR
- if(self.processor.algorithm == "RR" and self.processor.nxtSlice <= self.processor.rTime):
- self.processor.nxtSlice = self.processor.rTime + self.processor.tSlice
- self.processor.cSwitchAmt += factor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement