操作系统模拟实验(进程调度FCFS)
简陋的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()