要求:
利用简单的结构和控制方法模拟进程结构、进程状态和进程控制。
提示:
用PCB表示整个进程实体,利用随机数方法或键盘控制方法模拟进程执行中产生的事件,或者利用基于图形界面的鼠标或者键盘操作控制进程管理内容。
代码:
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)): # 找到第一个空闲比他大的位置
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('不能创建同名进程!')
return
if p.length > max(ram_list, key=lambda s: s[1])[1]:
print('内存不足!')
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('没有可以阻塞的进程!')
def times_over(run, ready):
if run:
pro = run.pop()
ready.append(pro)
ready_to_run(ready, run)
else:
print('没有正在运行的进程!')
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('没有可唤醒的进程!')
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: # 新加的空闲开始和上一个的结束相同
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]: # 检查下一个
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]: # 检查下一个
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('没有可退出的进程!')
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('输入开始的内存及长度:').strip().split())
MAX = length
ram_list = [[strat, length]]
print("0.输出进程")
print("1.创建进程")
print("2.进入阻塞状态")
print("3.时间片完")
print("4.唤醒")
print("5.结束进程")
print("6.显示内存")
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("输入进程的名字及内存:").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.输出进程")
print("1.创建进程")
print("2.进入阻塞状态")
print("3.时间片完")
print("4.唤醒")
print("5.结束进程")
print("6.显示内存")
print("7.帮助")
print("8.退出")
if a == 8:
break