操作系统模拟实验(银行家算法)
这几个实验都是上课时候一两小时实现的,因为老师给出的参考代码有些惨不忍睹(虽然我写的也没好到哪去).因为时间有限,就没用C++.
同样是因为时间有限,仅供参考,有bug概不负责.
代码如下
# -*- coding: utf-8 -*- class Bank(object): def __init__(self): self.totalpro = 0 self.process = [] self.res_num = 4 self.res_type = ['A', 'B', 'C', 'D'] self.Resources = {'A':10, 'B':9, 'C':8, 'D':7} self.Available = {'A':10, 'B':9, 'C':8, 'D':7} self.Allocation = {} self.Need = {} def createpc(self, pno, need): if pno in self.process: print "process",pno,"exists" return 0 self.process.append(pno) self.totalpro += 1 need = self.list2dict(need) for i in need: if need[i] > self.Available[i]: print "process",pno,"need : ",i,"more than available" return 0 self.Need[pno] = need self.Allocation[pno] = dict(zip(self.res_type, [0 for i in xrange(self.res_num)])) def deletepc(self, pno): if pno not in self.process: print "process",pno,"not exists" return 0 self.free(pno) self.Need.pop(pno) self.Allocation.pop(pno) self.process.remove(pno) self.totalpro -= 1 print "delete process success" return 1 def allocate(self, pno, request): request = self.list2dict(request) for i in request: if request[i] > self.Need[pno][i]: print "process",pno,"request more than needed" return 0 else: self.Need[pno][i] -= request[i] if request[i] < self.Available[i]: self.Available[i] -= request[i] else: print "process",pno,"request : ",i,"more than available" return 0 self.Allocation[pno][i] += request[i] if not self.issafe(pno): print "not safe" self.free(pno) else: print "request safe!" def free(self,pno): for i in self.res_type: self.Available[i] += self.Allocation[pno][i] self.Need[pno][i] += self.Allocation[pno][i] self.Allocation[pno][i] = 0 def issafe(self, pno): #decide is safe print "checking safety" safe = True Finish = {} Work = dict(zip(self.res_type, [0 for i in xrange(self.res_num)])) for i in self.Available: Work[i] += self.Available[i] for p in self.process: Finish[p] = False print p,Finish[p] for p in self.process: for i in self.Need[p]: if pno != p and self.Need[p][i] < Work[i]: Work[i] += self.Allocation[p][i] Finish[p] = True for p in self.process: if pno != p and Finish[p] == False: safe = False return safe print p,Finish[p] return safe def list2dict(self,request): return dict(zip(self.res_type, request)) def show(self): print "System Resources : " for i in self.res_type: print i, ":", self.Resources[i], "\t" print "Available Resources : " for i in self.res_type: print i, ":", self.Available[i], "\t" print "Need Resources : " for pno in self.process: print pno, ":" for i in self.res_type: print i, ":" ,self.Need[pno][i], "\t" print "Allocation Resources : " for pno in self.process: print pno, ":" for i in self.res_type: print i, ":" ,self.Allocation[pno][i], "\t" def show_type(self): out = "" for i in self.res_type: out += i+' ' print out def main(): bank = Bank() print "input 0 for create a process" print "input 1 for request resources" print "input 2 for delete a process" print "input 3 for show status" while(1): act = input("what to do : ") if act == 0: print "input pno" pno = input() bank.show_type() need = map(int, raw_input().split()) bank.createpc(pno, need) elif act == 1: print "input pno" pno = input() bank.show_type() request = map(int, raw_input().split()) bank.allocate(pno, request) elif act == 2: print "input pno" pno = input() bank.delete(pno) elif act == 3: bank.show() else: print "error input number" return 0 if __name__ == "__main__": main()
先创建,再分配资源,如果不安全就把分配的资源收回….实际上应该是试分配,这里是模拟就不计较了=.=
测试代码如下
# test 1 from bank import Bank def test1(): bank = Bank() bank.createpc(1,[5,5,5,5]) bank.createpc(2,[5,5,5,5]) bank.show() bank.allocate(1,[5,5,5,5]) bank.show()
输出结果
; Test 1 System Resources : A : 10 B : 9 C : 8 D : 7 Available Resources : A : 10 B : 9 C : 8 D : 7 Need Resources : 1 : A : 5 B : 5 C : 5 D : 5 2 : A : 5 B : 5 C : 5 D : 5 Allocation Resources : 1 : A : 0 B : 0 C : 0 D : 0 2 : A : 0 B : 0 C : 0 D : 0 checking safety 1 False 2 False 1 False not safe System Resources : A : 10 B : 9 C : 8 D : 7 Available Resources : A : 10 B : 9 C : 8 D : 7 Need Resources : 1 : A : 5 B : 5 C : 5 D : 5 2 : A : 5 B : 5 C : 5 D : 5 Allocation Resources : 1 : A : 0 B : 0 C : 0 D : 0 2 : A : 0 B : 0 C : 0 D : 0
第二个
# test 2 from bank import Bank def test2(): bank = Bank() bank.createpc(1,[4,3,2,1]) bank.createpc(2,[5,5,5,5]) bank.show() bank.allocate(1,[4,3,2,1]) bank.show()
输出如下
#test 2 System Resources : A : 10 B : 9 C : 8 D : 7 Available Resources : A : 10 B : 9 C : 8 D : 7 Need Resources : 1 : A : 4 B : 3 C : 2 D : 1 2 : A : 5 B : 5 C : 5 D : 5 Allocation Resources : 1 : A : 0 B : 0 C : 0 D : 0 2 : A : 0 B : 0 C : 0 D : 0 checking safety 1 False 2 False 1 False 2 True request safe! System Resources : A : 10 B : 9 C : 8 D : 7 Available Resources : A : 6 B : 6 C : 6 D : 6 Need Resources : 1 : A : 0 B : 0 C : 0 D : 0 2 : A : 5 B : 5 C : 5 D : 5 Allocation Resources : 1 : A : 4 B : 3 C : 2 D : 1 2 : A : 0 B : 0 C : 0 D : 0
仅供参考.