操作系统模拟实验(进程调度FCFS)

2014-12-08 Mithrilwoodrat 更多博文 » 博客 » GitHub »

原文链接 http://woodrat.xyz/2014/12/08/%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%e6%a8%a1%e6%8b%9f%e5%ae%9e%e9%aa%8c%e8%bf%9b%e7%a8%8b%e8%b0%83%e5%ba%a6fcfs/
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。


简陋的FCFS实现

# -- coding: utf-8 -- class Pcb(object): def init(self): self.pid = None self.ppid = None # id of parent self.arrive_time = 0 self.need_time = 0 self.pprio = 0 #priority

def set_pid(self, pid):
    self.pid = pid
def set_parent(self, parent):
    self.ppid = parent
def set_arrive_time(self, arrive_time):
    self.arrive_time = arrive_time
def set_need_time(self, need_time):
    self.need_time = need_time

class Pnode(object): def init(self): self.node = Pcb() self.child = []

def set_node_pid(self, pid):
    self.node.set_pid(pid)
def set_node_parent(self, ppid):
    self.node.set_parent(ppid)
def set_node_arrtime(self, arrtime):
    self.node.set_arrive_time(arrtime)
def set_node_needtime(self, needtime):
    self.node.set_need_time(needtime)
def get_node_pid(self):
    return self.node.pid
def get_node_parent(self):
    return self.node.ppid
def get_node_arrtime(self):
    return self.node.arrive_time
def get_node_needtime(self):
    return self.node.need_time

class WaitList(object): def init(self): self.waitlist = {} self.starttime = 0 def addtolist(self, pid, arrtime, needtime): l = self.waitlist if pid in l: print "process already exists" else: l[arrtime] = Pnode() l[arrtime].set_node_pid(pid) l[arrtime].set_node_arrtime(arrtime) l[arrtime].set_node_needtime(needtime)

def show(self):
    l = self.waitlist
    print "pid \t\t arrive time \t\t start time"
    for p in l:
        if (p > self.starttime):
            self.starttime += p-self.starttime
        print l[p].get_node_pid(),":\t\t",l[p].get_node_arrtime(),"\t\t",self.starttime
        self.starttime += l[p].get_node_needtime()

def main(): waitlist = WaitList() print "please input process number" n = input() for i in xrange(n): pid,arrtime,needtime = map(int,raw_input("please input process id,arrive time,need time").split()) waitlist.addtolist(pid, arrtime, needtime) waitlist.show()

if name == "main": main()