要求:
利用簡單的結(jié)構(gòu)和控制方法模擬進(jìn)程結(jié)構(gòu)墩新、進(jìn)程狀態(tài)和進(jìn)程控制。
提示:
用PCB表示整個(gè)進(jìn)程實(shí)體嫂粟,利用隨機(jī)數(shù)方法或鍵盤控制方法模擬進(jìn)程執(zhí)行中產(chǎn)生的事件踩衩,或者利用基于圖形界面的鼠標(biāo)或者鍵盤操作控制進(jìn)程管理內(nèi)容。
代碼:
MAX = 0
ram_list = []
class PCB(object):
def __init__(self, name, length):
self.name = name
self.length = length
self.start = 0
def distribution(p):
sorted(ram_list, key=lambda s: s[1])
# site = 0
for i in range(len(ram_list)): # 找到第一個(gè)空閑比他大的位置
if ram_list[i][1] >= p.length:
p.start = ram_list[i][0]
ram_list[i][0] += p.length # 空閑的起始忘后挪
ram_list[i][1] -= p.length
if ram_list[i][1] <= 2:
p.length += ram_list[i][1]
ram_list.pop(i)
global MAX
if ram_list:
MAX = max(ram_list, key=lambda s: s[1])[1]
else:
MAX = 0
# print(MAX)
# print(ram_list)
# site = i
break
def create(ready, run, p):
global name_list
l = len(name_list)
name_list[p.name] = 1
if l == len(name_list):
print('不能創(chuàng)建同名進(jìn)程徽缚!')
return
if p.length > max(ram_list, key=lambda s: s[1])[1]:
print('內(nèi)存不足憨奸!')
return
ready.append(p)
ready_to_run(ready, run)
distribution(p)
def ready_to_run(ready, run):
# print(type(run))
if not run and ready:
p = ready.pop(0)
# print(type(p))
run.append(p)
# print(ready, run)
def block(ready, blo, run):
if run:
pro = run.pop()
blo.append(pro)
ready_to_run(ready, run)
else:
print('沒有可以阻塞的進(jìn)程!')
def times_over(run, ready):
if run:
pro = run.pop()
ready.append(pro)
ready_to_run(ready, run)
else:
print('沒有正在運(yùn)行的進(jìn)程猎拨!')
def wake_up(blo, ready, run): # block[0]->ready
if blo:
pro = blo.pop(0)
ready.append(pro)
ready_to_run(ready, run)
else:
print('沒有可喚醒的進(jìn)程膀藐!')
def exit(run, ready):
sorted(ram_list, key=lambda x: x[0])
if run:
p = run.pop()
ready_to_run(ready, run)
del name_list[p.name]
site = 0
# p = (p.start, p.length)
# print(ram_list)
for i in range(len(ram_list)): # 檢查前面
end = ram_list[i][0] + ram_list[i][1]
if end == p.start:
site = i + 1
break
if end > p.start:
site = i
break
ram_list.insert(site, [p.start, p.length])
# print(site)
# print(ram_list)
global MAX
MAX = max(ram_list, key=lambda s: s[1])[1]
if site - 1 >= 0:
if ram_list[site-1][0] + ram_list[site-1][1] == p.start: # 新加的空閑開始和上一個(gè)的結(jié)束相同
ram_list[site-1][1] += p.length
ram_list.pop(site) # 合并
# print(ram_list)
MAX = max(ram_list, key=lambda s: s[1])[1]
# print(ram_list)
# print(site)
if site < len(ram_list):
if ram_list[site-1][0] + ram_list[site-1][1] == ram_list[site][0]: # 檢查下一個(gè)
ram_list[site-1][1] += ram_list[site][1]
ram_list.pop(site)
# print(ram_list)
MAX = max(ram_list, key=lambda s: s[1])[1]
return
return
if site + 1 <= len(ram_list) - 1:
if ram_list[site][0] + ram_list[site][1] == ram_list[site+1][0]: # 檢查下一個(gè)
ram_list[site][1] += ram_list[site+1][1]
ram_list.pop(site+1)
# print(ram_list)
# global MAX
MAX = max(ram_list, key=lambda s: s[1])[1]
return
else:
print('沒有可退出的進(jìn)程屠阻!')
def show(list):
s = ''
for i in list:
s += '('+i.name+','+str(i.start)+','+str(i.length)+') '
return s
if __name__ == '__main__':
ready = []
running = []
blocked = []
name_list = {}
strat, length = map(int, input('輸入開始的內(nèi)存及長度:').strip().split())
MAX = length
ram_list = [[strat, length]]
print("0.輸出進(jìn)程")
print("1.創(chuàng)建進(jìn)程")
print("2.進(jìn)入阻塞狀態(tài)")
print("3.時(shí)間片完")
print("4.喚醒")
print("5.結(jié)束進(jìn)程")
print("6.顯示內(nèi)存")
print("7.幫助")
print("8.退出")
while True:
a = int(input())
if a == 0:
print("ready:", show(ready))
print("running:", show(running))
print('blocked:', show(blocked))
if a == 1:
n, l = input("輸入進(jìn)程的名字及內(nèi)存:").strip().split()
l = int(l)
p = PCB(n, l)
create(ready, running, p)
if a == 2:
block(ready, blocked, running)
if a == 3:
times_over(running, ready)
if a == 4:
wake_up(blocked, ready, running)
if a == 5:
exit(running, ready)
if a == 6:
print(sorted(ram_list, key=lambda s: s[0]))
if a == 7:
print("0.輸出進(jìn)程")
print("1.創(chuàng)建進(jìn)程")
print("2.進(jìn)入阻塞狀態(tài)")
print("3.時(shí)間片完")
print("4.喚醒")
print("5.結(jié)束進(jìn)程")
print("6.顯示內(nèi)存")
print("7.幫助")
print("8.退出")
if a == 8:
break