ref_safety_algorithm2.py
3.96 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
#_*_ coding: utf-8 _*_
from string import ascii_uppercase
letters = list(ascii_uppercase)
def main() :
available = []
original_available = []
process = []
finished_process = []
finished = False
total_allocation = []
# resource는 3종류이며, 각각 20, 30, 40을 가지고 있다.
number_of_resources = 3
total_resources = [20,30,40]
# process는 4개가 있다.
number_of_processes = 4
# [ process:[[max],[current]], ...]
process = [[[10,6,4],[0,1,1]],[[5,6,7],[2,2,2]],[[15,3,4],[1,2,2]],[[7,9,20],[2,8,11]]]
# total_allocation, available, original_available 계산
total_allocation = get_total_allocation(process, number_of_processes, number_of_resources)
available = get_total_available(total_resources, total_allocation, number_of_resources)
original_available = available
# finish 설정
for i in range(0,number_of_processes) :
finished_process.append(False)
print(" total_resources : " + str(total_resources))
print("total_allocation : " + str(total_allocation))
print(" available : " + str(available))
print(" finish : " + str(finished_process))
get_process_need(process, number_of_processes, number_of_resources)
print("\n process : Max Allocation Need")
for i in range(0, number_of_processes) :
print(" " + str(process[i]))
temp = list(process)
while(len(process) != 0) :
queue, process = compare_avail_need(available, process, number_of_processes, number_of_resources)
available = get_new_avail(available, queue, len(queue), number_of_resources)
is_safe, available = check_safety(original_available, available, total_allocation, number_of_resources)
process = list(temp)
print("New available instances : " + str(available))
print(" Processes : " + str(process))
print(" Safe" if is_safe==True else " Not Safe")
def get_total_allocation(process, number_of_processes, number_of_resources) :
sum = 0
temp = []
# 각 process들의 현재 할당된 resource를 더해서 현재 전체 사용중인 resource 도출
for i in range(0, number_of_resources) :
for j in range(0, number_of_processes) :
sum += process[j][1][i]
temp.append(sum)
sum = 0
return temp
def get_total_available(total_resources, total_allocation, number_of_resources) :
temp = []
# 현재 사용 가능한 resource를 계산
for i in range(0,number_of_resources) :
temp.append(total_resources[i] - total_allocation[i])
return temp
def get_process_need(process, number_of_processes, number_of_resources) :
need = []
for i in range(0, number_of_processes) :
for j in range(0, number_of_resources) :
need.append(process[i][0][j] - process[i][1][j])
process[i].append(need)
need = []
def compare_avail_need(available, process, number_of_processes, number_of_resources) :
remove_list = []
queue = []
for i in range(0, number_of_processes) :
if(process[i][2] < available) :
queue.append([process[i][2], process[i][0]])
remove_list.append(process[i])
queue.sort()
for i in range(0, len(remove_list)) :
process.remove(remove_list[i])
return queue, process
def get_new_avail(available, queue, number_of_processes, number_of_resources) :
for i in range(0, number_of_processes) :
for j in range(0, number_of_resources) :
available[j] = abs(available[j] - queue[i][0][j]) + queue[i][1][j]
return available
def check_safety(original_available, available, total_allocation, number_of_resources) :
available = list([abs(rn_available - rn_total_allocation) for rn_available, rn_total_allocation in zip(available, total_allocation)])
return (True if (original_available == available) else False), available
if __name__ == '__main__' :
main()