forked from sagocious/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemAlloc_draft.py
More file actions
97 lines (82 loc) · 2.41 KB
/
memAlloc_draft.py
File metadata and controls
97 lines (82 loc) · 2.41 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
class Block:
def __init__(self, pid, start, size):
self.startAddr = start
self.size = size
self.isFree = False
self.pid = pid
self.next = None
def endAddr(self):
return self.startAddr + self.size
def makeFree(self):
self.isFree = True
self.pid = -1
def setPID(self, pid):
self.pid = pid
self.isFree = False
class Memory:
def __init__(self, size):
self.head = Block(-1, 0, size)
self.head.makeFree()
def isExists(self, pid):
cur = self.head
while cur!=None:
if cur.pid == pid:
return True
cur = cur.next
return False
def allocate(self, pid, size):
if self.isExists(pid):
print("processId" + pid + " is already exists")
return
cur = self.head
prev = None
found = False
while cur != None and not found:
if cur.isFree and (size <= cur.size):
found = True
else:
prev = cur
cur = cur.next
if found:
if cur.size == size:
cur.setPID(pid)
else:
b = Block(pid, cur.startAddr, size)
if prev != None:
prev.next = b
else:
self.head = b
b.next = cur
cur.size -= size
cur.startAddr += size
print("Allocated!")
else:
print("No space")
def terminate(self, pid):
cur = self.head
while cur!=None:
if cur.pid == pid:
cur.makeFree()
print("process {0} terminated!".format(pid))
return
cur = cur.next
def show(self):
print('')
cur = self.head
while cur != None:
print("pid:{0}\trange:{1}-{2}\tsize:{3}".format(cur.pid, cur.startAddr, cur.endAddr(), cur.size))
cur = cur.next
print(" (-1 is free space)")
print(" Allocation:\t[A <pid> <size>]\n Termination:\t[T <pid>]\n Show:\t\t[show]")
m = Memory(2560)
m.allocate(400, 'OS')
while True:
s = raw_input("Command: ").split()
if s[0]=="A":
m.allocate(s[1], int(s[2]))
elif s[0]=="T":
m.terminate(s[1])
elif s[0]=="show":
m.show()
elif s[0] == "exit":
exit()