《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
6.9 KiB

  1. import os
  2. import random
  3. import numpy as np
  4. import itertools
  5. class Bankers(object):
  6. def __init__(self, totalResource):
  7. #initiating
  8. self.RESOURCE = totalResource
  9. def SignProcesses(self, max_, allocated_):
  10. self.max = max_
  11. self.allocated = allocated_
  12. self.need = self.CalcNeed()
  13. self.avaliable = self.CalcAvaliable()
  14. self.finished = [False]*len(self.allocated)
  15. def Difference(self,a,b):
  16. #return matrix subtracted from a by b
  17. res = []
  18. for i in range(len(a)):
  19. tmp = []
  20. for j in range(len(a[i])):
  21. tmp.append(a[i][j]-b[i][j])
  22. res.append(tmp)
  23. return res
  24. def CalcNeed(self):
  25. #calc request by subtracting signed matrix from max matrix
  26. return self.Difference(self.max,self.allocated)
  27. def CalcAvaliable(self):
  28. """Calc Avaliable Resource"""
  29. a = self.allocated
  30. res = []
  31. for j in range(len(a[0])):
  32. tmp = 0
  33. for i in range(len(a)):
  34. tmp += a[i][j]
  35. res.append(self.RESOURCE[j] - tmp)
  36. return res
  37. def ExecuteProcess(self,index):
  38. #check if less avaliable than Request
  39. # YOUR CODE, YOUR ID
  40. #check END here
  41. #allocating what they need.
  42. # YOUR CODE, YOUR ID
  43. #allocating END here
  44. pass
  45. def TempSafeCheckAfterRelease(self):
  46. #check if at least one request can be done after previous process done. not check whole sequances.
  47. #if every element of Requests can't accepted after previous process done, this mean it is not safe state
  48. # YOUR CODE, YOU ID
  49. #check END here
  50. pass
  51. def print_matrixes(self):
  52. print "_____________________________________________"
  53. print "MAX\t\tAllocated\tNeed"
  54. for idx in range(len(self.max)):
  55. print "%s\t%s\t%s" % (self.max[idx],self.allocated[idx], self.need[idx])
  56. print "_____________________________________________"
  57. print "Resources:"
  58. print "Total: %s\tAvailable: %s\n" % (self.RESOURCE, self.avaliable)
  59. def ReleasingProcess(self,index):
  60. for i in range(0,len(self.RESOURCE)):
  61. self.finished[index] = True
  62. self.allocated[index][i] = 0
  63. self.avaliable = self.CalcAvaliable()
  64. def Execute(self):
  65. i = 0
  66. # get all permutation of processes
  67. perm = itertools.permutations(range(procnum), procnum)
  68. permArray = np.asarray(list(perm))
  69. for arr in permArray:
  70. for i in arr:
  71. if self.finished[i] == False:
  72. print "Executing..."
  73. print "Request: "
  74. print self.need[i]
  75. #check if less avaliable than Request
  76. if self.ExecuteProcess(i):
  77. print "Dispatching Done..."
  78. self.print_matrixes()
  79. print "-----Releasing Process------"
  80. self.ReleasingProcess(i)
  81. self.print_matrixes()
  82. #check if at least one request can be done after previous process done. not check whole sequances.
  83. #if every element of Requests can't accepted after previous process done, this mean it is not safe state
  84. if not (self.TempSafeCheckAfterRelease()):
  85. print "SAFE STATE: NOT SAFE - There are no sequances can avoid Deadlock"
  86. return False
  87. processes.append(i)
  88. else:
  89. print "HOLD: not enough Resource"
  90. if i == len(self.allocated)-1:
  91. i = 0
  92. else:
  93. i += 1
  94. check = True
  95. for k in range(0,len(self.allocated)):
  96. if self.finished[k] == False:
  97. check = False
  98. break
  99. if check == True:
  100. return True
  101. break
  102. #every permutation of processes is false
  103. return False
  104. def getmax():
  105. res = []
  106. for j in range(procnum):
  107. tmp = []
  108. for i in range(len(total_resources)):
  109. randnum=random.random()
  110. remain_max=0
  111. if j >0:
  112. remain_max=total_resources[i]
  113. for k in range(j):
  114. remain_max=remain_max-res[k][i]
  115. if remain_max < 0:
  116. remain_max=0
  117. else:
  118. remain_max=total_resources[i]
  119. tmp.append((int)(randnum*remain_max*0.8))
  120. res.append(tmp)
  121. return res
  122. def getallocated():
  123. res = []
  124. for j in range(procnum):
  125. tmp = []
  126. for i in range(len(total_resources)):
  127. randnum=random.random()
  128. remain=0
  129. if j >0:
  130. remain=max[j][i]
  131. for k in range(j):
  132. remain=remain-res[k][i]
  133. if remain < 0:
  134. remain=0
  135. else:
  136. remain=max[j][i]
  137. tmp.append((int)(randnum*remain))
  138. res.append(tmp)
  139. return res
  140. print "start here"
  141. # random seed
  142. seed = 2
  143. random.seed(seed)
  144. # the number of process list
  145. procnum = 3
  146. # the number of type of resource
  147. resnum = 4
  148. # the max total value of resource
  149. restotalval = 30
  150. # the total resources list
  151. total_resources=[]
  152. # the total processes
  153. processes=[]
  154. # set the real total value of resource in total_resources
  155. for i in range(resnum):
  156. total_resources.append((int)(restotalval*random.random()))
  157. # init the Banker
  158. b = Bankers(total_resources)
  159. # get the max request values of resources from process
  160. max=getmax()
  161. # get the already gotted values of resources from process
  162. allocated=getallocated()
  163. # init need matrix, available vector
  164. b.SignProcesses(max, allocated)
  165. # print all theses matrixes
  166. b.print_matrixes()
  167. # executing Banker algorithm
  168. result=b.Execute()
  169. # show results
  170. if result:
  171. print "SUCCESS proc lists ",processes
  172. else:
  173. print "Failed"
  174. # total_resources = [6, 5, 7, 6]
  175. # processes=[]
  176. # b = Bankers(total_resources)
  177. #
  178. # max = [
  179. # [3, 3, 2, 2],
  180. # [1, 2, 3, 4],
  181. # [1, 3, 5, 0],
  182. # ]
  183. # allocated = [
  184. # [1, 2, 2, 1],
  185. # [1, 0, 3, 3],
  186. # [1, 2, 1, 0],
  187. # ]
  188. #
  189. # b.SignProcesses(max, allocated)
  190. # b.print_matrixes()
  191. # result=b.Execute()
  192. # if result:
  193. # print "SUCCESS proc lists ",processes
  194. # else:
  195. # print "Failed"
  196. #
  197. #
  198. # total_resources = [10, 10, 8, 5]
  199. # processes=[]
  200. # b = Bankers(total_resources)
  201. # max = [
  202. # [10, 8, 2,5],
  203. # [6, 1, 3,1],
  204. # [3, 1, 4,2],
  205. # [5, 4, 2,1]
  206. # ]
  207. # allocated = [
  208. # [3, 0, 0,3],
  209. # [1, 1, 2,0],
  210. # [2, 1, 2,1],
  211. # [0, 0, 2,0]
  212. # ]
  213. # b.SignProcesses(max, allocated)
  214. # b.print_matrixes()
  215. # result=b.Execute()
  216. # if result:
  217. # print "SUCCESS proc lists ",processes
  218. # else:
  219. # print "Failed"