def enqueue(self, proc, updateburst=True): """ Adds process to back of ready queue and updates PCB status/location """ if not self.active: # No processes in CPU, process goes straight to CPU # No need to prompt for time proc.set_proc_loc(self._dev_name) self.active = proc else: if updateburst: # Prompt for time since last interrupt elapsed = io.get_valid_int("Time since last interrupt") # Update burst time for current process self.active.update_burst_time(elapsed) # Insert into ready queue proc.set_proc_loc("ready") PriorityQueue.enqueue(self, proc) # Move active process to ready queue if it now has a higher # remaining burst time left than any process in the ready queue if PriorityQueue.head(self) < self.active: p = PriorityQueue.dequeue(self) self.active.set_proc_loc("ready") p.set_proc_loc("CPU") PriorityQueue.enqueue(self, self.active) self.active = p print proc.status()
class DiskDrive(PriorityQueue): """ Initializes new disk drive with device name and two empty queues to implement FLOOK disk scheduling """ def __init__(self, dname, cyl): self._dev_type = "Disk Drive" self._dev_name = dname self._cylinders = cyl # Two priority queues to implement FSCAN. Q2 is frozen self._q1 = PriorityQueue() self._q2 = PriorityQueue(True) ## Methods to check/return device properties def get_num_cylinders(self): return self._cylinders def is_device_name(self, query_name): return True if self._dev_name == query_name else False def get_dev_name(self): return self._dev_name def is_device_type(self, query_type): return True if self._dev_type == query_type else False def get_dev_type(self): return self._dev_type def contains(self, pid): return (self._q1.contains(pid) or self._q2.contains(pid)) ## Scheduling methods def enqueue(self, proc): """ Enqueue processes to unfrozen queue. Update process location. If frozen queue is empty, unfreeze and freeze other queue """ if self._q1.is_frozen(): #Q1 is frozen, add to Q2 proc.set_proc_loc(self._dev_name) self._q2.enqueue(proc) if self._q1.empty(): self._q2.freeze() self._q1.unfreeze() else: #Q2 frozen, add to Q1 proc.set_proc_loc(self._dev_name) self._q1.enqueue(proc) if self._q2.empty(): self._q1.freeze() self._q2.unfreeze() def dequeue(self): """ Remove and return process at head of frozen queue. Clear any parameters passed when queued. Only dequeue processes from whichever queue is frozen. If dequeuing empties queue, freeze queue and unfreeze other queue """ if self._q1.is_frozen(): proc = self._q1.dequeue() if self._q1.empty(): self._q2.freeze() self._q1.unfreeze() else: proc = self._q2.dequeue() if self._q2.empty(): self._q1.freeze() self._q2.unfreeze() proc.clear_params() return proc def terminate(self, pid): if self._q1.contains(pid): self._q1.terminate(pid) if self._q1.is_frozen() and self._q1.empty(): self._q1.unfreeze() self._q2.freeze() elif self._q2.contains(pid): self._q2.terminate(pid) if self._q2.is_frozen() and self._q2.empty(): self._q2.unfreeze() self._q1.freeze() else: raise IndexError ## Methods to print device in human readable form to console def __repr__(self): return self._dev_name + " (" + self._dev_type.lower() + ")" def __str__(self): """ Returns device name and type as a string """ return self._dev_type + " " + self._dev_name def snapshot(self): """ Prints active processes in disk drive queue, in order they will be processed """ print io.snapshot_header(self._dev_name) if self._q1.empty() and self._q2.empty(): print '{:^78}'.format("EMPTY: No processes in queue") else: if self._q1.is_frozen(): print io.snapshot_header("PROCESSING [FROZEN]", "-") self._q1.snapshot() print io.snapshot_header("NEW REQUESTS", "-") self._q2.snapshot() else: print io.snapshot_header("PROCESSING [FROZEN]", "-") self._q2.snapshot() print io.snapshot_header("NEW REQUESTS", "-") self._q1.snapshot()