-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemo_test.py
248 lines (197 loc) · 8.01 KB
/
memo_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# coding=utf-8
import copy
class PageTable:
def __init__(self, max):
# frame number represent the virtual page's location in physical memory
# the table has a k_v as (page, [frame_number, validation])
self.table = [[-1, -1, -1, -1, -1, None] for i in range(max)]#块号、状态位、入序、访问、修改位、外存地址
def delete(self, page_num): # 清空页表中一页
self.table[page_num]=[-1, -1, -1, -1, -1, None]
def transform(self, page):
if page < len(self.table):
index = self.table[page][0] # 块号
return index
else:
return -2
class Page:
def __init__(self, page_size=50, content=None, is_allocated=-1, pid=-1):
self.is_allocated = is_allocated
self.pid = pid
self.content = content
self.page_size = page_size
def clr(self):
self.pid=-1
self.is_allocated=-1
self.content=None
def getline(self,num):
cont=self.content.split(';')
return cont[num]
def getonechar(self,num):
if len(self.content)>num:
return self.content[num]
return False
class MemoryManager:
def __init__(self,file_module, page_size=50, command_size=10, physical_page=50, schedule='FIFO'):
# 是否分配、pid、字符串1-3
self.physical_memory = [Page() for i in range(physical_page)] #物理内存
self.ps = page_size
self.cs = command_size
self.pn = physical_page
self.page_tables = {}
self.schedule = schedule
self.physical_rate = [0]
self.physical_history = [copy.deepcopy(self.physical_memory)]
self.allocated = 0
self.page_fault = 0
self.page_access = 0
self.pidlist=[]
self.sizelist=[]
self.file_module = file_module
def alloc(self, pid, size,filename):
s = size
if s+self.allocated > self.pn:
self.pidlist.append(pid)
self.sizelist.append(size)
return -1
else:
self.allocated += size
self.page_fault += size
file_fcb = self.file_module.get_fcb(filename)#文件接口
if file_fcb:
disk_loc = file_fcb.disk_loc
print("disk_loc", disk_loc)
else:
# 返回错误码
return -2 # 文件未找到
psize=len(disk_loc)
if pid in self.page_tables.keys(): # 已经有页表
ptable = self.page_tables[pid]
else: # 创建页表
ptable = PageTable(psize)
self.page_tables[pid] = ptable
for i in range(psize):
ptable.table[i][5]=disk_loc[i]
for i in range(self.pn):
if self.physical_memory[i].is_allocated == -1: # 该页未分配
self.physical_memory[i].pid = pid
self.physical_memory[i].is_allocated = 2
print(size)
print(s)
ptable.table[size - s][0] = i
ptable.table[size - s][1] = 1
ptable.table[size - s][2] = 1
self.Fage(size - s, ptable)
self.physical_memory[i].content=self.file_module.disk.read_block(self.file_module.disk.data_base + ptable.table[size-s][5])
print(self.physical_memory[i].content)
# print(self.physical_memory[i].content)
s = s-1
# 额外添加,不知道对不对
if size - s >= psize:
break
if s == 0:
break
return psize
def free(self, pid):
status = 0
ptable=self.page_tables[pid]
for i in range(len(ptable.table)):
if ptable.table[i][1] == 1:
status = 1
b = ptable.table[i][0]
if ptable.table[i][4] == 1: # 有修改,写回外存,给块号和内容
self.file_module.disk.write_block(ptable.table[i][0], self.physical_memory[ptable.table[i][0]].content)
self.physical_memory[b].clr()
self.allocated=self.allocated-1
ptable.table.clear()
self.page_tables.pop(pid)
if status == 0:
return False
return True
def access(self, pid, address):
self.page_access += 1
page=10*int(address[0])+int(address[1])
page_offset = 100*int(address[2])+10*int(address[3])+int(address[4]) # 页内偏移
ptable = self.page_tables[pid]
block = ptable.transform(page)
if block == -2:
print("ERROR ADDRESS !!!!")
return -2
elif block ==-1: #缺页中断
if self.schedule == 'LRU':
block=self.LRU(ptable)
self.Lage(page, ptable)
elif self.schedule == 'FIFO':
block=self.FIFO(ptable)
self.Fage(page, ptable)
self.physical_memory[block].is_allocated = 2
self.physical_memory[block].content = self.file_module.disk.read_block(ptable.table[page][5]) # 接口获取页内信息
ptable.table[page][0]=block
ptable.table[page][1] = 1
ptable.table[page][4] = -1
elif block>=0:
if self.schedule == 'LRU':
self.Lage(block,ptable)
print("缺页中断")
return self.physical_memory[block].getonechar(page_offset)
def page_PC(self, pid, address):
print("address")
print(address)
self.page_access += 1
page= int(address[:2])
page_offset = 100*int(address[2])+10*int(address[3])+int(address[4]) # 页内偏移
ptable = self.page_tables[pid]
block = ptable.transform(page)
if block == -2:
print("ERROR ADDRESS !!!!")
return
elif block ==-1: #缺页中断
if self.schedule == 'LRU':
block=self.LRU(ptable)
self.Lage(page, ptable)
elif self.schedule == 'FIFO':
block=self.FIFO(ptable)
self.Fage(page, ptable)
self.physical_memory[block].is_allocated = 2
self.physical_memory[block].content = self.file_module.disk.read_block(ptable.table[page][5]) # 接口获取页内信息
ptable.table[page][0] = block
ptable.table[page][1] = 1
ptable.table[page][4] = -1
elif block>=0:
if self.schedule == 'LRU':
self.Lage(block,ptable)
return self.physical_memory[block].getline(page_offset)
def Fage(self,idx,ptable): # 用于FIFO记录顺序
for i in range(len(ptable.table)):
if ptable.table[i][1]==1:
ptable.table[i][2]+=1
ptable.table[idx][2] = 0
def Lage(self,idx,ptable): # 用于LRU记录顺序
for i in range(len(ptable.table)):
if ptable.table[i][1]==1:
ptable.table[i][3]+=1
ptable.table[idx][3] = 0
def LRU(self, ptable):
max=0
page=0
for i in range(len(ptable.table)):
if ptable.table[i][3]>max:
max=ptable.table[i][3]
page=i
if ptable.table[page][4]==1:
self.file_module.disk.write_block(ptable.table[page][0], self.physical_memory[ptable.table[page][0]].content)
ptable.delete(page)
return page
def FIFO(self, ptable):
max=0
page=0
for i in range(len(ptable.table)):
if ptable.table[i][2]>max:
max=ptable.table[i][2]
page=i
if ptable.table[page][4] == 1:
self.file_module.disk.write_block(self.file_module.disk.database + ptable.table[page][0], \
self.physical_memory[ptable.table[page][0]].content)
ptable.delete(page)
return page
if __name__ == '__main__':
M=MemoryManager()