Created
August 6, 2015 07:33
-
-
Save kalda341/2a35da95e615fd2a0b7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A1 for COMPSCI340/SOFTENG370 2015 | |
# Prepared by Robert Sheehan | |
# Modified by ... | |
# You are not allowed to use any sleep calls. | |
from threading import Lock, Event | |
from process import State | |
class ProcessStack(): | |
def __init__(self): | |
"""Construct the dispatcher.""" | |
self.stack = [] | |
self.waiting_stack = [] | |
def set_io_sys(self, io_sys): | |
self.io_sys = io_sys | |
def __len__(self): | |
return len(self.stack) | |
def next_paused(self): | |
items = [p for p in self.stack if p.state == State.paused] | |
if len(items): | |
return items.pop() | |
else: | |
return None | |
def push(process): | |
self.io_sys.allocate_window_to_process(process, len(stack)) | |
def remove(process): | |
self.io_sys.remove_window_from_process(process) | |
#Move other processes to new windows | |
self.shunt_process_windows_up(process) | |
self.process_stack.remove(process) | |
self.dispatch_next_process() | |
def shunt_process_windows_up(self, process): | |
process_position = self.process_stack.index(process) | |
for i in range(process_position+1, len(self.process_stack)): | |
process_to_move = self.process_stack[i] | |
self.io_sys.remove_window_from_process(process_to_move) | |
self.io_sys.allocate_window_to_process(process_to_move, i-1) | |
class Dispatcher(): | |
"""The dispatcher.""" | |
MAX_PROCESSES = 8 | |
MAX_RUNNING_PROCESSES = 2 | |
def __init__(self): | |
"""Construct the dispatcher.""" | |
self.process_stack = ProcessStack() | |
self.waiting_stack = [] | |
def set_io_sys(self, io_sys): | |
"""Set the io subsystem.""" | |
self.io_sys = io_sys | |
def add_process(self, process): | |
"""Add and start the process.""" | |
num_processes = len(self.process_stack) | |
if num_processes < self.MAX_PROCESSES: | |
self.io_sys.allocate_window_to_process(process, num_processes) | |
if num_processes >= self.MAX_RUNNING_PROCESSES: | |
self.process_stack[-1 * self.MAX_RUNNING_PROCESSES].wait() | |
self.process_stack.append(process) | |
process.start() | |
def dispatch_next_process(self): | |
"""Dispatch the process at the top of the stack.""" | |
paused = [p for p in self.process_stack if p.state == State.paused] | |
if len(paused): | |
process = paused.pop() | |
process.resume() | |
def to_top(self, process): | |
"""Move the process to the top of the stack.""" | |
self.io_sys.remove_window_from_process(process) | |
#Move other processes to new windows | |
self.shunt_process_windows_up(process) | |
self. | |
self.process_stack.remove(process) | |
self.process_stack.append(process) | |
process.resume() | |
def pause_system(self): | |
"""Pause the currently running process. | |
As long as the dispatcher doesn't dispatch another process this | |
effectively pauses the system. | |
""" | |
if len(self.process_stack): | |
self.process_stack[-1].pause() | |
def resume_system(self): | |
pass | |
#if len(self.process_stack): | |
#for i in self.process_stack[-1].run() | |
"""Resume running the system.""" | |
# ... | |
def wait_until_finished(self): | |
"""Hang around until all runnable processes are finished.""" | |
while len(self.process_stack): | |
pass | |
def proc_finished(self, process): | |
"""Receive notification that "proc" has finished. | |
Only called from running processes. | |
""" | |
self.io_sys.remove_window_from_process(process) | |
#Move other processes to new windows | |
self.shunt_process_windows_up(process) | |
self.process_stack.remove(process) | |
self.dispatch_next_process() | |
def shunt_process_windows_up(self, process): | |
process_position = self.process_stack.index(process) | |
for i in range(process_position+1, len(self.process_stack)): | |
process_to_move = self.process_stack[i] | |
self.io_sys.remove_window_from_process(process_to_move) | |
self.io_sys.allocate_window_to_process(process_to_move, i-1) | |
def proc_waiting(self, process): | |
"""Receive notification that process is waiting for input.""" | |
self.process_stack.remove(process) | |
self.io_sys.move_process(process, len(self.waiting_stack)) | |
self.waiting_stack.append(process) | |
process.wait(event) | |
def process_with_id(self, id): | |
"""Return the process with the id.""" | |
matching_processes = [i for i in self.process_stack if i.id == id] | |
if len(matching_processes) == 1: | |
return matching_processes[0] | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment