[Coding Assignment2]memory_allocation.py
6.03 KB
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
# -*- coding: utf-8 -*-
# Node 클래스 정의
class Node:
def __init__(self, pnum, psize):
self.pnum = pnum
self.psize = psize
self.next = None
# 자료구조 정의
class MemoryList:
total_memory = 1000.0
available_memory = total_memory
# 초기화 메소드
def __init__(self):
dummy = Node(0,0.0)
self.head = dummy
self.tail = dummy
self.current = None
self.before = None
self.alloc_ptr = None
self.searching_ptr = None
self.hole = 0
self.hole_found = None
self.num_of_data = 0
# self.allocMemory(0,self.total_memory)
# allocMemory 메소드
def allocMemory(self, pnum, psize):
if(psize != 0) :
if(self.available_memory<psize) :
print("Not enough memory!")
# 처음인 경우 그냥 넣고
elif((self.num_of_data == 0)or(self.hole==0)) :
new_node = Node(pnum, psize)
self.available_memory -= psize
self.tail.next = new_node
self.tail = new_node
self.num_of_data += 1
# 첫 할당이 아닐 경우는 alloc_ptr 초기화 해서 사용합니다.
else :
self.first()
self.alloc_ptr = self.current
self.searching_ptr = self.current
# best를 찾기 위한 변수
best_fit = 10000
self.hole_found = False
while(True) :
# pnum = 0(빈자리) 중 psize보다 큰 자리가 있으면
if((self.searching_ptr.pnum==0)and(self.searching_ptr.psize>=psize)) :
self.hole_found = True
# best fit 계산해보고, alloc_ptr 설정
if(best_fit>(self.searching_ptr.psize-psize)) :
best_fit = self.searching_ptr.psize-psize
self.alloc_ptr = self.searching_ptr
# 마지막까지 갔으면 break!
if(self.searching_ptr.next == None) :
self.searching_ptr = None
break
self.searching_ptr = self.searching_ptr.next
if(self.hole_found == True) :
# new_node를 뒤에 붙일 노드로 초기화
new_node = Node(0, (self.alloc_ptr.psize-psize))
new_node.next = self.alloc_ptr.next
self.alloc_ptr.next = new_node
self.alloc_ptr.pnum = pnum
self.alloc_ptr.psize = psize
if(new_node.psize == 0) :
self.alloc_ptr.next = new_node.next
new_node.next = None
self.available_memory -= psize
self.num_of_data += 1
elif(self.hole_found == False) :
new_node = Node(pnum, psize)
self.tail.next = new_node
self.tail = new_node
self.available_memory -= psize
self.num_of_data += 1
elif(psize == 0) :
self.first()
while(True) :
if(self.current.pnum == pnum) :
self.current.pnum = 0
self.available_memory += self.current.psize
self.hole += 1
break
else :
self.next()
# def delete(self):
# # pop_pnum = self.current.pnum
# # pop_psize = self.current.psize
# if self.current is self.tail:
# self.tail = self.before
# self.before.next = self.current.next
# self.current = self.before # 중요 : current가 next가 아닌 before로 변경된다.
# #
# self.num_of_data -= 1
# return pop_pnum, pop_psize
# first 메소드 (search1 - 맨 앞의 노드 검색, before, current 변경)
def first(self):
if self.num_of_data == 0: # 데이터가 없는 경우 첫번째 노드도 없기 때문에 None 리턴
return None
self.before = self.head
self.current = self.head.next
# return self.current.psize, self.current.pnum
# next 메소드 (search2 - current 노드의 다음 노드 검색, 이전에 first 메소드가 한번은 실행되어야 함)
def next(self):
if self.current.next == None:
# print ("End")
return None
else :
self.before = self.current
self.current = self.current.next
return self.current.pnum, self.current.psize
def size(self):
return self.num_of_data
myMemory = MemoryList()
# 파일 open & process 할당.
f = open('processes.txt','r')
i = 1
print("\n[Memory Size : 1000.0]\n")
while(True) :
line = f.readline().strip().split()
if(not line) : break
myMemory.allocMemory(int(line[0]), float(line[1]))
myMemory.first()
print("REQUEST#"+str(i)+" - Process#"+str(line[0])+" "+str(line[1]))
while(True) :
if(myMemory.current.pnum!=0) :
print("Process#" + str(myMemory.current.pnum) + ": " + str(myMemory.current.psize))
else :
print(" Hole: "+str(myMemory.current.psize))
if(myMemory.next()==None) :
break
print("Available Memory: "+str(myMemory.available_memory)+"\n")
i += 1
# print(pdict)
f.close()
# ---------- Testing ----------#
# myMemory.allocMemory(1,22.0)
# myMemory.allocMemory(2,18.0)
# myMemory.allocMemory(3,28.0)
# myMemory.allocMemory(5,25.0)
# myMemory.allocMemory(4,5.0)
# myMemory.allocMemory(2,0)
# myMemory.allocMemory(6,10.0)
# # myMemory.allocMemory(4,0)
# myMemory.allocMemory(5,0)
# myMemory.allocMemory(7,20.0)
# print("MEMORY SIZE: "+str(myMemory.total_memory)+"\n")
# print("REQUEST 1: 22.0")
# print("Best Fit: Allocated at address "+str(myMemory.current.psize))
# print(" "+str(myMemory.available_memory)+" free")