Created
October 28, 2011 04:16
-
-
Save reorx/1321604 to your computer and use it in GitHub Desktop.
os_cpu
This file contains 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
#!/usr/bin/python | |
#coding=utf8 | |
import sys | |
from utils import directSelectSort | |
class Process(object): | |
def __init__(self, id, priority, schema): | |
""" | |
Arguments:: | |
: id : : identify the process, unique | |
: priority : : reduce 1 per running, can be negative | |
: schema : : reduce 1 per running, only positive int, >= 0 | |
""" | |
self.id = id | |
self.priority = priority | |
self.schema = schema | |
# 'R' means ready (to perform next running), | |
# 'E' means end (already) | |
self.status = 'R' | |
def run(self): | |
"""Simulate occurances when & after a process runs""" | |
self.priority -= 1 | |
self.schema -= 1 | |
if self.schema <= 0: | |
self.status = 'E' | |
def __gt__(self, o): | |
return self.priority > o.priority | |
def __lt__(self, o): | |
return self.priority < o.priority | |
class Dispatcher(object): | |
"""Methods are stack-like""" | |
def __init__(self): | |
self.queue = [] | |
self.finished = [] | |
def push(self, proc): | |
self.queue.append(proc) | |
def pop(self, index): | |
self.finished.append(self.queue.pop(index)) | |
def dispatch(self): | |
directSelectSort(self.queue) | |
class Display: | |
fmt = '| {0:<12}| {1:<12}| {2:<12}| {3:<12}|' | |
sepline = ('+'+'-'*13)*4+'+' | |
solidline = '|'+' '*55+'|' | |
def stats_header(self): | |
print '' | |
print self.Display.sepline | |
print self.Display.fmt.format('Sequence', 'Priority', 'Time schema', 'Status') | |
print self.Display.sepline | |
def stats_item(self, i): | |
print self.Display.fmt.format(i.id, i.priority, i.schema, i.status) | |
print self.Display.sepline | |
def stats_comment(self, s): | |
buf = list(self.Display.solidline) | |
s = list(s) | |
assert (len(buf)-len(s))>=2 | |
offset = (len(buf) - len(s))/2 | |
for i in range(len(s)): | |
buf[i+offset] = s[i] | |
print ''.join(buf) | |
print self.Display.sepline | |
def start(self): | |
self.stats_header() | |
while self.queue: | |
self.dispatch() | |
proc = self.queue[0] | |
self.stats_item(proc) | |
proc.run() | |
if 'E' == proc.status: | |
self.stats_comment('process %s ends' % proc.id) | |
self.pop(0) | |
print '' | |
print 'finished processes:' | |
for i in self.finished: | |
print 'id:{0} priority:{1} schema:{2}'.format(i.id, i.priority, i.schema) | |
if '__main__' == __name__: | |
PROCS_NUM = 3 | |
def proc_input(id): | |
while True: | |
print 'Process %s:' % id, | |
try: | |
buf = raw_input() | |
assert len(buf.split('/')) == 2, 'exactly 2' | |
return [int(i) for i in buf.split('/')] | |
except Exception as e: | |
print e | |
# start | |
cpu = Dispatcher() | |
print 'Please input process initialize arguments by sequence' | |
print '请输入按序进程参数' | |
print 'Format: priority(int)/time(int)' | |
print '格式:优先级(整型)/时间(整型)\n' | |
for i in range(1, PROCS_NUM+1): | |
cpu.push(Process(i, *proc_input(i))) | |
print '\nEnd input, now staring' | |
print '完成输入,开始执行\n' | |
print 'Stats before starting' | |
print '执行前进程统计' | |
for i in cpu.queue: | |
print 'id:{0} priority:{1} schema:{2}'.format(i.id, i.priority, i.schema) | |
cpu.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment