实验三:存储器管理采用可变式分区管理
采用空闲区表,并增加已分配区表{未分配区说明表、已分配区说明表(分区号、起始地址、长度、状态)}。分配算法采用最佳适应算法(内存空闲区按照尺寸大小从小到大的排列)和循环首次适应算法,实现内存的分配与回收。
学生自己设计一个进程申请序列以及进程完成后的释放顺序,实现主存的分配与回收。
进程分配时,应该考虑以下3中情况:进程申请的空间小于、大于或者等于系统空闲区的大小。回收时,应该考虑4种情况:释放区上邻、下邻、上下都邻接和都不邻接空闲区。
每次的分配与回收,都要求把记录内存使用情况的各种数据结构的变化情况以及各进程的申请、释放情况显示或打印出来。
import copy
NF_POINT = 0
def print_list(list, str):
print('######## %s ##########' %str)
for p in list:
print(p)
###################### 回收 ################################
# 通过编号来寻找该请求
def request_find(request_number, allocated_partition):
for partition in allocated_partition:
if partition['name'] == request_number:
# partition_start_size = {'partition_start': partition['partition_start'], 'size': partition['size']}
# return partition_start_size
return partition
return False
def merge(target_partition, lower_partition, upper_partition):
target_partition['partition_start'] = lower_partition['partition_start']
target_partition['size'] = lower_partition['size'] + upper_partition['size']
def do_free_partition(algorithm, request_number, free_partition, allocated_partition):
print('')
print('===============================================')
free_partition.sort(key = lambda p : p['partition_start'])
request = request_find(request_number, allocated_partition)
if request == False:
print('该作业并未分配,空闲表中不存在')
return
merged_partition = None
for partition in free_partition:
if partition['partition_start'] == request['partition_start'] + request['size'] :
if merged_partition is None:
merge(partition, request, partition)
else:
merge(partition, merged_partition, partition)
free_partition.remove(merged_partition)
merged_partition = partition
if partition['partition_start'] + partition['size'] == request['partition_start'] :
if merged_partition is None:
merge(partition, partition,request)
else:
merge(partition, partition, merged_partition)
free_partition.remove(merged_partition)
merged_partition = partition
if merged_partition is None:
free_partition.append(
{'partition_start':request['partition_start'], 'size': request['size']}
)
allocated_partition.remove(request)
if algorithm == 'BF':
free_partition.sort(key=lambda p: p['size'])
print_list(free_partition , '回收后空闲表')
# is_find = False
# low_equal = False
# high_equal = False
# for partition in free_partition:
# partition_end = partition['partition_start'] + partition['size']
# if is_find == True:
# if request_partiton_end <= partition['partition_start']:
# if request_partiton_end == partition['partition_start'] and low_equal==False:
# partition['size'] += partition['size']
# partition['partition_start'] = request_partiton_start
# elif request_partiton_end == partition['partition_start'] and low_equal==False:
# free_partition.remove(partition)
# break
# if request_partiton_start >= partition_end:
# if request_partiton_start == partition_end:
# low_equal = True
# partition['size'] += request['size']
# if
# is_find = True
# if request_partiton_start != partition['partition_start']:
# if request_partiton_end < partition['partition_start'] + partition['size']:
# print('23')
# break
###################### nf算法 ################################
def nf_request_allocation(request, free_partition, allocated_partition):
global NF_POINT
request_size = request['size']
#输出
print('')
print('')
print('目前正在分配{},大小为{}'.format(request['name'], request_size))
print_list(free_partition, '目前空闲表为:')
original = NF_POINT
while True:
if NF_POINT > len(free_partition)-1 :
NF_POINT = 0
original = 0
print('目前的循环指针为:%d'%NF_POINT)
if request_size == free_partition[NF_POINT]['size']:
allocated_partition.append(
{'name': request['name'], 'size': request['size'], 'partition_start': free_partition[NF_POINT]['partition_start']}
)
free_partition.remove(free_partition[NF_POINT])
print('========分配结束后空闲表为==========')
for p in free_partition:
print(p)
return
elif request['size'] < free_partition[NF_POINT]['size']:
allocated_partition.append(
{'name': request['name'], 'size': request['size'], 'partition_start': free_partition[NF_POINT]['partition_start']}
)
free_partition[NF_POINT]['partition_start'] += request['size']
free_partition[NF_POINT]['size'] -= request['size']
print('========分配结束后空闲表为==========')
for p in free_partition:
print(p)
return
NF_POINT = (NF_POINT + 1) % len(free_partition)
if NF_POINT == original:
break
print('没有足够的空间进行分配')
############################### bf 算法 ################################
def nf_allocation(free_partition_, allocated_partition_, request_partition_, request_free_):
free_partition = copy.deepcopy(free_partition_)
allocated_partition = copy.deepcopy(allocated_partition_)
request_partition = copy.deepcopy(request_partition_)
request_free = copy.deepcopy(request_free_)
for request in request_partition:
nf_request_allocation(request, free_partition, allocated_partition)
def bf_request_allocation(request, free_partition, allocated_partition):
#free_partition = copy.deepcopy(free_partition_)
print('')
print('')
print('目前正在分配{},大小为{}'.format(request['name'], request['size']))
print_list(free_partition, '目前空闲表为:')
free_partition.sort(key = lambda p : p['size'])
for free in free_partition:
if request['size'] == free['size']:
allocated_partition.append(
{'name': request['name'], 'size': request['size'], 'partition_start': free['partition_start']}
)
free_partition.remove(free)
print('========分配结束后分配表为==========')
for p in allocated_partition:
print(p)
print_list(free_partition, '目前空闲表为:')
return
elif request['size'] < free['size']:
allocated_partition.append(
{'name': request['name'], 'size': request['size'], 'partition_start': free['partition_start']}
)
free['partition_start'] += request['size']
free['size'] -= request['size']
print('========分配结束后分配表为==========')
for p in allocated_partition:
print(p)
print_list(free_partition, '目前空闲表为:')
return
print('没有足够的空间进行分配')
# bf 算法
def bf_allocation(free_partition_, allocated_partition_ ,request_partition_,request_free_):
free_partition = copy.deepcopy(free_partition_)
allocated_partition = copy.deepcopy(allocated_partition_)
request_partition = copy.deepcopy(request_partition_)
request_free = copy.deepcopy(request_free_)
# 进行分配
for request in request_partition:
bf_request_allocation(request, free_partition, allocated_partition)
#进行回收
request_free = ['5', '0', '2', '1', '6', '4', '3']
for request_number in request_free:
do_free_partition('BF',request_number, free_partition, allocated_partition)
if __name__ == '__main__':
free_partition = [
{'partition_start': 85, 'size':10},
{'partition_start': 100, 'size': 10},
{'partition_start': 155, 'size': 32},
{'partition_start': 275, 'size': 70},
{'partition_start': 532, 'size': 60}
]
allocated_partition = [
{'name': '0', 'size': 5, 'partition_start':95},
{'name': '1', 'size': 7, 'partition_start': 592}
]
request_partition = [
{'name': '2', 'size': 16},
{'name': '3', 'size': 16},
{'name': '4', 'size': 48},
{'name': '5', 'size': 60},
{'name': '6', 'size': 100}
]
request_free = ['5', '0', '2', '1', '6', '4', '3']
bf_allocation(free_partition, allocated_partition, request_partition, request_free)
nf_allocation(free_partition, allocated_partition, request_partition, request_free)