四种算法共同实现的存储器的分配与回收实验代码
时间: 2024-04-30 09:19:45 浏览: 136
天津理工大学操作系统存储器的分配与回收算法实现实验报告.doc
5星 · 资源好评率100%
由于存储器的分配与回收涉及到多种算法,在这里我们只给出一种可能的实现代码,即采用了首次适应算法、最佳适应算法、最坏适应算法和循环首次适应算法的存储器分配与回收实验代码。
代码如下:
```python
import random
class Memory:
def __init__(self, size):
self.size = size
self.memory = [(0, size)]
self.free = size
def allocate(self, size, algorithm):
if size > self.free:
print("Error: Not enough free memory")
return -1
index = algorithm(self.memory, size)
if index == -1:
print("Error: Allocation failed")
return -1
start, end = self.memory[index]
if end - start == size:
self.memory.pop(index)
else:
self.memory[index] = (start + size, end)
self.free -= size
return start
def deallocate(self, start):
for i, (s, e) in enumerate(self.memory):
if start < s:
if i > 0 and start == self.memory[i-1][1]:
self.memory[i-1] = (self.memory[i-1][0], e)
self.memory.pop(i)
else:
self.memory.insert(i, (start, start))
break
elif start == s:
if i > 0 and start == self.memory[i-1][1]:
self.memory[i-1] = (self.memory[i-1][0], e)
self.memory.pop(i)
else:
self.memory[i] = (start, e)
break
elif start < e:
if start + 1 == e:
self.memory[i] = (s, start)
elif start + 1 < e:
self.memory.insert(i, (s, start))
self.memory.insert(i+1, (start+1, e))
break
self.free = sum(e-s for s, e in self.memory)
def first_fit(memory, size):
for i, (start, end) in enumerate(memory):
if end - start >= size:
return i
return -1
def best_fit(memory, size):
best_index = -1
best_size = float('inf')
for i, (start, end) in enumerate(memory):
if end - start >= size and end - start < best_size:
best_index = i
best_size = end - start
return best_index
def worst_fit(memory, size):
worst_index = -1
worst_size = -1
for i, (start, end) in enumerate(memory):
if end - start >= size and end - start > worst_size:
worst_index = i
worst_size = end - start
return worst_index
def next_fit(memory, size):
start_index = -1
for i, (start, end) in enumerate(memory):
if start_index == -1 and end - start >= size:
start_index = i
elif start_index != -1 and end - start >= size:
if i >= start_index:
return i
else:
return start_index
if start_index != -1:
return start_index
else:
return -1
memory = Memory(100)
for i in range(10):
size = random.randint(1, 10)
start = memory.allocate(size, first_fit)
print("Allocate size {} at start {}".format(size, start))
print(memory.memory)
for i in range(5):
start = random.randint(0, 99)
memory.deallocate(start)
print("Deallocate start {}".format(start))
print(memory.memory)
for i in range(10):
size = random.randint(1, 10)
start = memory.allocate(size, best_fit)
print("Allocate size {} at start {}".format(size, start))
print(memory.memory)
for i in range(5):
start = random.randint(0, 99)
memory.deallocate(start)
print("Deallocate start {}".format(start))
print(memory.memory)
for i in range(10):
size = random.randint(1, 10)
start = memory.allocate(size, worst_fit)
print("Allocate size {} at start {}".format(size, start))
print(memory.memory)
for i in range(5):
start = random.randint(0, 99)
memory.deallocate(start)
print("Deallocate start {}".format(start))
print(memory.memory)
for i in range(10):
size = random.randint(1, 10)
start = memory.allocate(size, next_fit)
print("Allocate size {} at start {}".format(size, start))
print(memory.memory)
for i in range(5):
start = random.randint(0, 99)
memory.deallocate(start)
print("Deallocate start {}".format(start))
print(memory.memory)
```
在这段代码中,我们定义了一个 Memory 类,其中包含了存储器的大小、存储器的分配情况以及空闲空间的大小等信息。在 allocate 方法中,我们传入了一个算法参数,用来指定使用哪种算法进行存储器的分配。在 deallocate 方法中,我们对存储器的释放进行了实现。最后,我们定义了四种算法:首次适应算法、最佳适应算法、最坏适应算法和循环首次适应算法,用来作为 allocate 方法中的算法参数。
接下来,我们通过调用 Memory 类中的方法,来进行存储器的分配与回收实验。在这个实验中,我们先使用首次适应算法来进行存储器分配,然后使用 deallocate 方法来释放一些空间。接着,我们使用最佳适应算法、最坏适应算法和循环首次适应算法,分别进行存储器的分配与回收实验。在每次分配和回收后,我们都会打印出当前存储器的使用情况。
阅读全文