Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1.     def contextSwitch(self):
  2.  
  3.  
  4.         old = self.processor.cProc      # Assign old proc to this
  5.         factor = None                   # Init this to None, must change or error
  6.         new = -1                      # New process to insert into cProc, init to -1 to catch unititialized value
  7.  
  8.  
  9.         # Just loading in a new process
  10.         if(self.processor.cProc is None):
  11.             # Loading a new process, do half a cswitch
  12.             factor = 1/2                            # Doing half a cSwitch
  13.             time = self.processor.cSwitchTime / 2   # We're doing half a cSwitchTime
  14.  
  15.             #Since we're doing half a cswitch, pull new process now
  16.             if(not self.processor.workQ.isEmpty()):
  17.                 new = self.processor.workQ.dequeue()
  18.  
  19.         #We know that we're going to have to load out the current process
  20.         else:
  21.             if(self.processor.cProc.state == "BLOCKED"):
  22.                 self.processor.procHelper.logBurst()
  23.                 writeOutput("time {0}ms: Process {1} switching out of CPU; will block on I/O until time {2}ms {3}\n".format(
  24.                     int(self.processor.rTime), self.processor.cProc.label,
  25.                     int(self.processor.cProc.IOtimeLeft + (self.processor.rTime) + (self.processor.cSwitchTime / 2)),
  26.                     self.processor.procPrinter.getQStr()), self.processor.cProc.label, evenCPU())
  27.             # potentially doing either first half, or full depending on state of
  28.             # workQ
  29.             factor = 1      # We're doing a full cSwitchTime
  30.             time = self.processor.cSwitchTime
  31.  
  32.         #We're switching in None
  33.         if(self.processor.workQ.isEmpty()):
  34.                 # Loading a new process
  35.                 factor = 1/2    # We're doing half a cSwitchTime
  36.                 time = self.processor.cSwitchTime / 2
  37.  
  38.  
  39.         # Simulate time cycles.
  40.         for i in range(1, int(time) + 1):
  41.             self.processor.incrementrTime()     # Increment time
  42.             self.processor.step_workQ()         # step_workQ to ensure workQ is in valid state
  43.  
  44.             # We swap out processes
  45.             if(i == self.processor.cSwitchTime / 2):
  46.                 #We are removing a process, put it in procPool
  47.                 if not old is None:
  48.                     self.processor.procPool.append(old)
  49.  
  50.                 #If new is unassigned and there is a new Process
  51.                 if(self.processor.workQ.isEmpty() and isinstance(new, int)):
  52.                     new = self.processor.workQ.dequeue()
  53.  
  54.  
  55.                 # Add new proc to self
  56.                 #Calculate the new time slice if its RR
  57.                 if(self.processor.algorithm == "RR" and old is not None):
  58.                     self.processor.nxtSlice = self.processor.rTime + self.processor.tSlice + i
  59.  
  60.  
  61.                 self.processor.cProc = new
  62.                 assert(not isinstance(self.processor.cProc, int))
  63.  
  64.                 if(new is not None):
  65.                     self.processor.cProc.stateChange("RUNNING")
  66.  
  67.  
  68.         # Increment the context switch counter.
  69.         # Here we have completed the context switch
  70.         if(self.processor.cProc is not None):
  71.             if (self.processor.cProc.burstTimeLeft < self.processor.cProc.burstTime):
  72.                 self.processor.startBurst = self.processor.rTime
  73.                 writeOutput(
  74.                     "time {0}ms: Process {1} started using the CPU with {2}ms remaining {3}\n".format(
  75.                         int(self.processor.rTime), self.processor.cProc.label,
  76.                         self.processor.cProc.burstTimeLeft,
  77.                         self.processor.procPrinter.getQStr()), self.processor.rTime, evenCPU())
  78.  
  79.             else:
  80.                 self.processor.startBurst = self.processor.rTime
  81.                 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())
  82.  
  83.  
  84.         #Calculate the new time slice if its RR
  85.         if(self.processor.algorithm == "RR" and self.processor.nxtSlice <= self.processor.rTime):
  86.             self.processor.nxtSlice = self.processor.rTime + self.processor.tSlice
  87.  
  88.         self.processor.cSwitchAmt += factor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement