Example #1
0
 def snapshot(self):
     """ Prints processes in ready queue, plus active process in CPU with headers """
     print io.snapshot_header("ready")
     PriorityQueue.snapshot(self)
     if self.active:
         print " ACTIVE IN CPU (Est time remaining: {0:}) ".format(
             str(self.active.next_est_burst)).center(78, "=")
         self.active.headers()
         print io.ruler()
         self.active.snapshot()
     else:
         print "\n" + "No active process in the CPU".center(78)
Example #2
0
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()