python实现操作系统之动态分区存储管理
import copy
class Memory(object):
def init(self, start, end, length, state=1, ID=0):#init()方法是一种特殊的方法,被称为类的初始化方法,当创建这个类的实例时就会调用该方法
# self 代表类的实例,self 在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数
self.start = start
self.end = end
self.length = length
self.state = state # state为1:内存未分配
self.Id = ID ##ID为0是未分配,其余为任务编号
def show_memory(list):
print("分配状态 分区号 起始地址 终止地址 分区大小")
for i in range(0, len(list)):
p = list[i]
if p.state == 1:
print("%s%s%s%11.d%11.d%10.d"%('空闲'," ", p.Id, p.start, p.end, p.length))
else:
print("%s%s%s%11.d%11.d%10.d"%('已分配'," ", p.Id, p.start, p.end, p.length))
首次适应算法
def FF(work_id, work_length, list):
for i in list:
if i.Id == work_id:
print('作业已存在')
return
for i in range(0, len(list)):
p = list[i]
if p.state == 1 and p.length > work_length: # p是当前未分配内存的大小
node2 = Memory(p.start + work_length, p.end, p.length - work_length, 1, 0) # 剩下的未分配的
a = Memory(p.start, p.start + work_length - 1, work_length, state=0, ID=work_id) # a是已分配的
del list[i]
list.insert(i, node2)
list.insert(i, a)
show_memory(list)
return
if p.state == 1 and p.length == work_length:
p.state = 0
show_memory(list)
return
print("内存空间不足")
def free1(work_id, li):
for i in range(0, len(li)):
p = li[i]
if p.Id == work_id:
p.state = 1
target = i
p.Id=0
break
# 向前合并空闲块
if target - 1 > 0:
if li[target - 1].state == 1:
a = Memory(li[target - 1].start, li[target].end, li[target - 1].length + li[target].length, 1, 0)
del li[target - 1]
del li[target - 1]
li.insert(target - 1, a)
target = target - 1
# 向后合并空闲块
if target + 1 < len(li):
if li[target + 1].state == 1:
a = Memory(li[target].start, li[target + 1].end, li[target].length + li[target + 1].length, 1, 0)
del li[target]
del li[target]
li.insert(target, a)
show_memory(li)
最佳适应算法
def bubble_sort(list):
# 冒泡排序
count = len(list)
for i in range(0, count):
for j in range(i + 1, count):
if list[i].length < list[j].length:
list[i], list[j] = list[j], list[i]
return list
def BF(work_id, work_length, li):
for i in li:
if i.Id == work_id:
print('作业已存在')
return
q = copy.copy(li)
q = bubble_sort(q) # 从小到大排序,给所有已分配和未分配的排序
s = -1
ss12 = -1
for i in range(0, len(q)):
p = q[i]
if p.state == 1 and p.length > work_length: # p.state == 1 已分配的不参与分配
s = p.start # s得到起始位置
elif p.state == 1 and p.length == work_length:
ss12 = p.start
if s == -1 and ss12 == -1:
print("内存空间不足")
return
for i in range(0, len(li)):
p = li[i]
if p.start == s:
node2 = Memory(p.start + work_length, p.end, p.length - work_length, 1, 0) # 未分配
a = Memory(p.start, p.start + work_length - 1, work_length, state=0, ID=work_id)
del li[i]
li.insert(i, node2)
li.insert(i, a)
show_memory(li)
return
elif p.start == ss12:
p.state = 0
show_memory(li)
return
if name == 'main':
print("输入内存大小:")
size = int(input())
a = Memory(0, size - 1, size, state=1, ID=0)
b = []
b.append(a)
print('1:初始化')
print('2:分配空间')
print('3:回收********')
print('4:查看********')
print('*****5:退出********')
while (True):
select = input('请输入想要执行的功能:')
if select == '5':
break
elif select == '1':
print("输入内存大小:")
size = int(input())
a = Memory(0, size - 1, size, state=1, ID=0)
b = []
b.append(a)
elif select == '2':
print("1.首次适应算法:FF")
print("2.最佳适应算法:BF")
x = input("请输入分配执行的算法:")
x = float(x)
repit = 'Y'
while (repit == 'Y'):
if x == 1:
work_size = input('请输入作业id和大小:').split()
FF(work_size[0], int(work_size[1]), b)
repit = input('是否继续Y/N')
elif x == 2:
work_size = input('请输入作业id和大小:').split()
BF(work_size[0], int(work_size[1]), b)
repit = input('是否继续Y/N')
elif select == '3':
id_delete = input('请输入删除作业id:')
free1(id_delete, b)
else:
show_memory(b)
本文来自博客园,作者:{HYHr},转载请注明原文链接:https://www.cnblogs.com/dishearten/p/15602254.html