Showing
17 changed files
with
879 additions
and
1 deletions
1 | +# Operating Systems - Coding Assignment1: safety algorithm | ||
2 | +- [Coding Assignment1]safety_algorithm.py | ||
3 | +- [Coding Assignment1]r_SafeCaseResult.png | ||
4 | +- [Coding Assignment1]r_UnsafeCaseResult.png | ||
5 | +- (ref)ref_safety_algorithm.py | ||
6 | +- (ref)ref_safety_algorithm2.py | ||
7 | +- (ref)ref_safety_algorithm3.py | ||
8 | + | ||
9 | +# Operating Systems - Coding Assignment2: memory allocation | ||
10 | +- [Coding Assignment2]memory_allocation.py | ||
11 | +- processes.txt | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | +#_*_ coding: utf-8 _*_ | ||
2 | + | ||
3 | +process_num = 4 #process 갯수 | ||
4 | +resource_num = 3 #resourse 종류 | ||
5 | +resource_max = [] | ||
6 | + | ||
7 | +finished_process = [] # process 수행종료 여부 | ||
8 | +alloc = [] # 각 process의 초기할당 | ||
9 | +process_max = [] # 각 process의 max | ||
10 | +avail = [] # 각 process의 available | ||
11 | +need = [] # 각 process의 need | ||
12 | +safety_order = [] # safe한 process의 순서를 나타냄 | ||
13 | + | ||
14 | +def main() : | ||
15 | + | ||
16 | + # 초기 설정 작업 # | ||
17 | + | ||
18 | + # finished_process 계산 | ||
19 | + for i in range(0, process_num) : | ||
20 | + finished_process.append(False) | ||
21 | + safety_order.append(0) | ||
22 | + | ||
23 | + | ||
24 | + while(1) : | ||
25 | + | ||
26 | + print("\nPlease Enter 1 to Safe Case") | ||
27 | + print("or 2 to Unsafe Case") | ||
28 | + choice = int(input("Enter : ")) | ||
29 | + | ||
30 | + if(choice == 1) : | ||
31 | + | ||
32 | + # Safe Case의 각 resource 상한과 현재 할당량, process별 상한 지정 | ||
33 | + resource_max = [20,30,40] | ||
34 | + alloc = [[0,5,1],[2,2,2],[1,2,1],[2,8,18]] | ||
35 | + process_max = [[10,15,4],[2,25,35],[17,20,30],[10,19,20]] | ||
36 | + | ||
37 | + for j in range(0, resource_num) : # avail 계산 | ||
38 | + temp = resource_max[j] | ||
39 | + for i in range(0,process_num) : | ||
40 | + temp -= alloc[i][j] | ||
41 | + avail.append(temp) | ||
42 | + | ||
43 | + for i in range(0, process_num) : # need 계산 | ||
44 | + temp = [] | ||
45 | + for j in range(0, resource_num) : | ||
46 | + temp.append(abs(process_max[i][j]-alloc[i][j])) | ||
47 | + need.append(temp) | ||
48 | + | ||
49 | + break | ||
50 | + | ||
51 | + elif(choice == 2) : | ||
52 | + | ||
53 | + # Unsafe Case의 각 resource 상한과 현재 할당량, process별 상한 지정 | ||
54 | + resource_max = [20,30,40] | ||
55 | + alloc = [[0,5,1],[2,2,2],[1,2,1],[2,8,18]] | ||
56 | + process_max = [[20,15,4],[2,25,35],[19,20,30],[15,19,20]] | ||
57 | + | ||
58 | + for j in range(0, resource_num) : # avail 계산 | ||
59 | + temp = resource_max[j] | ||
60 | + for i in range(0,process_num) : | ||
61 | + temp -= alloc[i][j] | ||
62 | + avail.append(temp) | ||
63 | + | ||
64 | + for i in range(0, process_num) : # need 계산 | ||
65 | + temp = [] | ||
66 | + for j in range(0, resource_num) : | ||
67 | + temp.append(abs(process_max[i][j]-alloc[i][j])) | ||
68 | + need.append(temp) | ||
69 | + | ||
70 | + break | ||
71 | + | ||
72 | + else : | ||
73 | + print("Please Enter Right Value!") | ||
74 | + | ||
75 | + | ||
76 | + | ||
77 | + # 현재상황 출력 | ||
78 | + print("\n--------------Safety Algorithm--------------\n") | ||
79 | + | ||
80 | + print("Current Allocation : [A, B, C]") | ||
81 | + for i in range(0, process_num) : | ||
82 | + print(" P#"+str(i+1)+" : "+str(alloc[i])) | ||
83 | + | ||
84 | + print("\n Current Max : [A, B, C]") | ||
85 | + for i in range(0, process_num) : | ||
86 | + print(" P#"+str(i+1)+" : "+str(process_max[i])) | ||
87 | + | ||
88 | + | ||
89 | + | ||
90 | + # 비교 및 순서도출 | ||
91 | + time = 0 | ||
92 | + | ||
93 | + while(time < process_num) : | ||
94 | + for i in range(0, process_num) : | ||
95 | + k = 0 | ||
96 | + for j in range(0,resource_num) : | ||
97 | + if(need[i][j] <= avail[j]) : | ||
98 | + k += 1 # 해당 process가 available보다 작은 need를 가졌다면 count | ||
99 | + | ||
100 | + if(k == resource_num) : # 해당 process의 모든 resource가 available보다 작은 need를 가진 경우 | ||
101 | + finished_process[i] = True # 해당 process는 finish 했다고 표시됨 | ||
102 | + for l in range(0, resource_num) : | ||
103 | + avail[l] = avail[l] + alloc[i][l] # 자신이 할당했던 resource를 환원하고 | ||
104 | + need[i][l] = 10000 # 끝난 process는 need값을 많이 높여 비교대상에서 제외 | ||
105 | + | ||
106 | + safety_order[time] = i # safety_order에 현재 time에 적합한 process의 index를 설정 | ||
107 | + break | ||
108 | + | ||
109 | + time += 1 | ||
110 | + | ||
111 | + | ||
112 | + time = 0 | ||
113 | + | ||
114 | + for i in range(0, process_num) : | ||
115 | + if(finished_process[i] == True) : | ||
116 | + time += 1 # i값에 따라 True인 process를 찾고 time을 증가시킨다 | ||
117 | + | ||
118 | + if(time == process_num) : # 모든 process가 True여야만 Safe Case이다. | ||
119 | + result = "" | ||
120 | + for i in range(0, process_num) : | ||
121 | + result = result + "Process#" + str(safety_order[i]+1) + "/" | ||
122 | + print("\n Safe Processes! : " + result) | ||
123 | + | ||
124 | + else : | ||
125 | + print("\nNot Safe Processes!") | ||
126 | + | ||
127 | + | ||
128 | + print("\n--------------------------------------------\n") | ||
129 | + | ||
130 | +if __name__ == '__main__' : | ||
131 | + main() | ||
132 | + |
1 | +# -*- coding: utf-8 -*- | ||
2 | + | ||
3 | +# Node 클래스 정의 | ||
4 | +class Node: | ||
5 | + def __init__(self, pnum, psize): | ||
6 | + self.pnum = pnum | ||
7 | + self.psize = psize | ||
8 | + self.next = None | ||
9 | + | ||
10 | + | ||
11 | +# 자료구조 정의 | ||
12 | +class MemoryList: | ||
13 | + total_memory = 1000.0 | ||
14 | + available_memory = total_memory | ||
15 | + | ||
16 | + # 초기화 메소드 | ||
17 | + def __init__(self): | ||
18 | + dummy = Node(0,0.0) | ||
19 | + self.head = dummy | ||
20 | + self.tail = dummy | ||
21 | + | ||
22 | + self.current = None | ||
23 | + self.before = None | ||
24 | + self.alloc_ptr = None | ||
25 | + self.searching_ptr = None | ||
26 | + self.hole = 0 | ||
27 | + self.hole_found = None | ||
28 | + | ||
29 | + self.num_of_data = 0 | ||
30 | + | ||
31 | + # self.allocMemory(0,self.total_memory) | ||
32 | + | ||
33 | + # allocMemory 메소드 | ||
34 | + def allocMemory(self, pnum, psize): | ||
35 | + | ||
36 | + if(psize != 0) : | ||
37 | + | ||
38 | + if(self.available_memory<psize) : | ||
39 | + print("Not enough memory!") | ||
40 | + | ||
41 | + # 처음인 경우 그냥 넣고 | ||
42 | + elif((self.num_of_data == 0)or(self.hole==0)) : | ||
43 | + new_node = Node(pnum, psize) | ||
44 | + self.available_memory -= psize | ||
45 | + self.tail.next = new_node | ||
46 | + self.tail = new_node | ||
47 | + | ||
48 | + self.num_of_data += 1 | ||
49 | + | ||
50 | + # 첫 할당이 아닐 경우는 alloc_ptr 초기화 해서 사용합니다. | ||
51 | + else : | ||
52 | + self.first() | ||
53 | + self.alloc_ptr = self.current | ||
54 | + self.searching_ptr = self.current | ||
55 | + | ||
56 | + # best를 찾기 위한 변수 | ||
57 | + best_fit = 10000 | ||
58 | + self.hole_found = False | ||
59 | + | ||
60 | + while(True) : | ||
61 | + # pnum = 0(빈자리) 중 psize보다 큰 자리가 있으면 | ||
62 | + if((self.searching_ptr.pnum==0)and(self.searching_ptr.psize>=psize)) : | ||
63 | + self.hole_found = True | ||
64 | + # best fit 계산해보고, alloc_ptr 설정 | ||
65 | + if(best_fit>(self.searching_ptr.psize-psize)) : | ||
66 | + best_fit = self.searching_ptr.psize-psize | ||
67 | + self.alloc_ptr = self.searching_ptr | ||
68 | + | ||
69 | + | ||
70 | + # 마지막까지 갔으면 break! | ||
71 | + if(self.searching_ptr.next == None) : | ||
72 | + self.searching_ptr = None | ||
73 | + break | ||
74 | + self.searching_ptr = self.searching_ptr.next | ||
75 | + | ||
76 | + if(self.hole_found == True) : | ||
77 | + # new_node를 뒤에 붙일 노드로 초기화 | ||
78 | + new_node = Node(0, (self.alloc_ptr.psize-psize)) | ||
79 | + | ||
80 | + new_node.next = self.alloc_ptr.next | ||
81 | + self.alloc_ptr.next = new_node | ||
82 | + | ||
83 | + self.alloc_ptr.pnum = pnum | ||
84 | + self.alloc_ptr.psize = psize | ||
85 | + | ||
86 | + if(new_node.psize == 0) : | ||
87 | + self.alloc_ptr.next = new_node.next | ||
88 | + new_node.next = None | ||
89 | + | ||
90 | + self.available_memory -= psize | ||
91 | + self.num_of_data += 1 | ||
92 | + | ||
93 | + elif(self.hole_found == False) : | ||
94 | + | ||
95 | + new_node = Node(pnum, psize) | ||
96 | + self.tail.next = new_node | ||
97 | + self.tail = new_node | ||
98 | + self.available_memory -= psize | ||
99 | + self.num_of_data += 1 | ||
100 | + | ||
101 | + | ||
102 | + | ||
103 | + | ||
104 | + elif(psize == 0) : | ||
105 | + self.first() | ||
106 | + while(True) : | ||
107 | + if(self.current.pnum == pnum) : | ||
108 | + self.current.pnum = 0 | ||
109 | + self.available_memory += self.current.psize | ||
110 | + self.hole += 1 | ||
111 | + break | ||
112 | + else : | ||
113 | + self.next() | ||
114 | + | ||
115 | + | ||
116 | + # def delete(self): | ||
117 | + # # pop_pnum = self.current.pnum | ||
118 | + # # pop_psize = self.current.psize | ||
119 | + | ||
120 | + # if self.current is self.tail: | ||
121 | + # self.tail = self.before | ||
122 | + | ||
123 | + # self.before.next = self.current.next | ||
124 | + # self.current = self.before # 중요 : current가 next가 아닌 before로 변경된다. | ||
125 | + # # | ||
126 | + | ||
127 | + # self.num_of_data -= 1 | ||
128 | + | ||
129 | + # return pop_pnum, pop_psize | ||
130 | + | ||
131 | + # first 메소드 (search1 - 맨 앞의 노드 검색, before, current 변경) | ||
132 | + def first(self): | ||
133 | + if self.num_of_data == 0: # 데이터가 없는 경우 첫번째 노드도 없기 때문에 None 리턴 | ||
134 | + return None | ||
135 | + | ||
136 | + self.before = self.head | ||
137 | + self.current = self.head.next | ||
138 | + | ||
139 | + # return self.current.psize, self.current.pnum | ||
140 | + | ||
141 | + # next 메소드 (search2 - current 노드의 다음 노드 검색, 이전에 first 메소드가 한번은 실행되어야 함) | ||
142 | + def next(self): | ||
143 | + if self.current.next == None: | ||
144 | + # print ("End") | ||
145 | + return None | ||
146 | + | ||
147 | + else : | ||
148 | + self.before = self.current | ||
149 | + self.current = self.current.next | ||
150 | + | ||
151 | + return self.current.pnum, self.current.psize | ||
152 | + | ||
153 | + def size(self): | ||
154 | + return self.num_of_data | ||
155 | + | ||
156 | + | ||
157 | + | ||
158 | + | ||
159 | + | ||
160 | + | ||
161 | + | ||
162 | + | ||
163 | +myMemory = MemoryList() | ||
164 | + | ||
165 | + | ||
166 | +# 파일 open & process 할당. | ||
167 | +f = open('processes.txt','r') | ||
168 | +i = 1 | ||
169 | +print("\n[Memory Size : 1000.0]\n") | ||
170 | + | ||
171 | +while(True) : | ||
172 | + line = f.readline().strip().split() | ||
173 | + if(not line) : break | ||
174 | + | ||
175 | + myMemory.allocMemory(int(line[0]), float(line[1])) | ||
176 | + | ||
177 | + myMemory.first() | ||
178 | + | ||
179 | + print("REQUEST#"+str(i)+" - Process#"+str(line[0])+" "+str(line[1])) | ||
180 | + while(True) : | ||
181 | + if(myMemory.current.pnum!=0) : | ||
182 | + print("Process#" + str(myMemory.current.pnum) + ": " + str(myMemory.current.psize)) | ||
183 | + else : | ||
184 | + print(" Hole: "+str(myMemory.current.psize)) | ||
185 | + | ||
186 | + if(myMemory.next()==None) : | ||
187 | + break | ||
188 | + | ||
189 | + print("Available Memory: "+str(myMemory.available_memory)+"\n") | ||
190 | + i += 1 | ||
191 | + | ||
192 | + | ||
193 | +# print(pdict) | ||
194 | +f.close() | ||
195 | + | ||
196 | + | ||
197 | + | ||
198 | +# ---------- Testing ----------# | ||
199 | + | ||
200 | +# myMemory.allocMemory(1,22.0) | ||
201 | +# myMemory.allocMemory(2,18.0) | ||
202 | +# myMemory.allocMemory(3,28.0) | ||
203 | +# myMemory.allocMemory(5,25.0) | ||
204 | +# myMemory.allocMemory(4,5.0) | ||
205 | +# myMemory.allocMemory(2,0) | ||
206 | +# myMemory.allocMemory(6,10.0) | ||
207 | +# # myMemory.allocMemory(4,0) | ||
208 | +# myMemory.allocMemory(5,0) | ||
209 | +# myMemory.allocMemory(7,20.0) | ||
210 | + | ||
211 | + | ||
212 | + | ||
213 | + | ||
214 | +# print("MEMORY SIZE: "+str(myMemory.total_memory)+"\n") | ||
215 | + | ||
216 | +# print("REQUEST 1: 22.0") | ||
217 | +# print("Best Fit: Allocated at address "+str(myMemory.current.psize)) | ||
218 | +# print(" "+str(myMemory.available_memory)+" free") |
1 | +from string import ascii_uppercase | ||
2 | + | ||
3 | +letters = list(ascii_uppercase) | ||
4 | + | ||
5 | +def main(): | ||
6 | + available = [] | ||
7 | + original_available = [] | ||
8 | + process = [] | ||
9 | + | ||
10 | + total_allocation = [] | ||
11 | + total_resources = [] | ||
12 | + | ||
13 | + # f(x1) | ||
14 | + number_of_resources = 3 | ||
15 | + number_of_processes = 4 | ||
16 | + | ||
17 | + # f(x2) | ||
18 | + total_resources = set_total_resources(number_of_resources=number_of_resources) | ||
19 | + | ||
20 | + # f(x3) | ||
21 | + process = set_processes(number_of_resources=number_of_resources, number_of_processes=number_of_processes) | ||
22 | + total_allocation = get_total_allocation(process=process, number_of_processes=len(process), number_of_resources=number_of_resources) | ||
23 | + available = get_available(total_resources=total_resources, total_allocation=total_allocation, number_of_resources=number_of_resources) | ||
24 | + original_available = list(available) | ||
25 | + | ||
26 | + | ||
27 | + # f(x4) | ||
28 | + process = get_process_need(process=process, number_of_processes=len(process), number_of_resources=number_of_resources) | ||
29 | + | ||
30 | + temp = list(process) | ||
31 | + | ||
32 | + | ||
33 | + # f(x5) | ||
34 | + while (len(process) != 0): | ||
35 | + queue, process = compare_ai_need(available=available, process=process, number_of_processes=len(process), number_of_resources=number_of_resources) | ||
36 | + # f(x6) | ||
37 | + available = get_new_ai(available=available, queue=queue, number_of_processes=len(queue), number_of_resources=number_of_resources) | ||
38 | + | ||
39 | + | ||
40 | + # f(x7) | ||
41 | + is_safe, available = check_safety(original_available=original_available, available=available, total_allocation=total_allocation, number_of_resources=number_of_resources) | ||
42 | + | ||
43 | + process = list(temp) | ||
44 | + | ||
45 | + print("Total Resources : {}".format(total_resources)) | ||
46 | + print("Total Allocation : {}".format(total_allocation)) | ||
47 | + print("Original Available Instance : {}".format(original_available)) | ||
48 | + print("New Available Instance : {}".format(available)) | ||
49 | + print("Processes : {}".format(process)) | ||
50 | + print("Safe" if is_safe == True else "Not Safe") | ||
51 | + | ||
52 | + | ||
53 | + | ||
54 | + | ||
55 | +def set_total_resources(number_of_resources=0): | ||
56 | + total_resources = [] | ||
57 | + for resource_number in range(0, number_of_resources): | ||
58 | + total_resources.append(int(input("Total Resources, value for {}: ".format(letters[resource_number])))) | ||
59 | + return total_resources | ||
60 | + | ||
61 | +def set_processes(number_of_resources=0, number_of_processes=0): | ||
62 | + cur_allocation = [] | ||
63 | + max_allocation = [] | ||
64 | + process = [] | ||
65 | + for index in range(0, number_of_processes): | ||
66 | + for resource_number in range(0, number_of_resources): | ||
67 | + max_allocation.append(int(input("Max. Allocation #{}, value for {}: ".format(index + 1, letters[resource_number])))) | ||
68 | + for resource_number in range(0, number_of_resources): | ||
69 | + cur_allocation.append(int(input("Cur. Allocation #{}, value for {}: ".format(index + 1, letters[resource_number])))) | ||
70 | + process.append([max_allocation, cur_allocation]) | ||
71 | + max_allocation = [] | ||
72 | + cur_allocation = [] | ||
73 | + return process | ||
74 | + | ||
75 | +def get_total_allocation(process=[], number_of_processes=0, number_of_resources=0): | ||
76 | + summation = 0 | ||
77 | + total_allocation = [] | ||
78 | + for resource_number in range(0, number_of_resources): | ||
79 | + for index in range(0, number_of_processes): | ||
80 | + summation += process[index][1][resource_number] | ||
81 | + total_allocation.append(summation) | ||
82 | + summation = 0 | ||
83 | + return total_allocation | ||
84 | + | ||
85 | +def get_available(total_resources=[], total_allocation=[], number_of_resources=0): | ||
86 | + available = list([abs(rn_total_resources - rn_total_allocation) for rn_total_resources, rn_total_allocation in zip(total_resources, total_allocation)]) | ||
87 | + return available | ||
88 | + | ||
89 | +def get_process_need(process=[], number_of_processes=0, number_of_resources=0): | ||
90 | + for index in range(0, number_of_processes): | ||
91 | + need = list([abs(rn_max_alloc - rn_current_alloc) for index, rn_max_alloc, rn_current_alloc in zip(process, process[index][0], process[index][1])]) | ||
92 | + process[index].append(need) | ||
93 | + need = [] | ||
94 | + return process | ||
95 | + | ||
96 | +def compare_ai_need(available=[], process=[], number_of_processes=0, number_of_resources=0): | ||
97 | + for_removal = [] | ||
98 | + queue = [] | ||
99 | + for index in range(0, number_of_processes): | ||
100 | + if (process[index][2] < available): | ||
101 | + queue.append([process[index][2], process[index][0]]) | ||
102 | + for_removal.append(process[index]) | ||
103 | + queue.sort() | ||
104 | + for index in range(0, len(for_removal)): | ||
105 | + process.remove(for_removal[index]) | ||
106 | + return queue, process | ||
107 | + | ||
108 | +def get_new_ai(available=[], queue=[], number_of_processes=0, number_of_resources=0): | ||
109 | + for index in range(0, number_of_processes): | ||
110 | + for resource_number in range(0, number_of_resources): | ||
111 | + available[resource_number] = abs(available[resource_number] - queue[index][0][resource_number]) + queue[index][1][resource_number] | ||
112 | + return available | ||
113 | + | ||
114 | +def check_safety(original_available=[], available=[], total_allocation=[], number_of_resources=0): | ||
115 | + available = list([abs(rn_available - rn_total_allocation) for rn_available, rn_total_allocation in zip(available, total_allocation)]) | ||
116 | + return (True if (original_available == available) else False), available | ||
117 | + | ||
118 | +if __name__ == '__main__': | ||
119 | + main() | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | +#_*_ coding: utf-8 _*_ | ||
2 | + | ||
3 | +from string import ascii_uppercase | ||
4 | + | ||
5 | + | ||
6 | +letters = list(ascii_uppercase) | ||
7 | + | ||
8 | + | ||
9 | +def main() : | ||
10 | + | ||
11 | + available = [] | ||
12 | + original_available = [] | ||
13 | + process = [] | ||
14 | + finished_process = [] | ||
15 | + finished = False | ||
16 | + | ||
17 | + total_allocation = [] | ||
18 | + | ||
19 | + # resource는 3종류이며, 각각 20, 30, 40을 가지고 있다. | ||
20 | + number_of_resources = 3 | ||
21 | + total_resources = [20,30,40] | ||
22 | + | ||
23 | + # process는 4개가 있다. | ||
24 | + number_of_processes = 4 | ||
25 | + | ||
26 | + # [ process:[[max],[current]], ...] | ||
27 | + 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]]] | ||
28 | + | ||
29 | + # total_allocation, available, original_available 계산 | ||
30 | + total_allocation = get_total_allocation(process, number_of_processes, number_of_resources) | ||
31 | + available = get_total_available(total_resources, total_allocation, number_of_resources) | ||
32 | + original_available = available | ||
33 | + | ||
34 | + # finish 설정 | ||
35 | + for i in range(0,number_of_processes) : | ||
36 | + finished_process.append(False) | ||
37 | + | ||
38 | + print(" total_resources : " + str(total_resources)) | ||
39 | + print("total_allocation : " + str(total_allocation)) | ||
40 | + print(" available : " + str(available)) | ||
41 | + print(" finish : " + str(finished_process)) | ||
42 | + | ||
43 | + | ||
44 | + get_process_need(process, number_of_processes, number_of_resources) | ||
45 | + print("\n process : Max Allocation Need") | ||
46 | + | ||
47 | + for i in range(0, number_of_processes) : | ||
48 | + print(" " + str(process[i])) | ||
49 | + | ||
50 | + | ||
51 | + temp = list(process) | ||
52 | + | ||
53 | + while(len(process) != 0) : | ||
54 | + queue, process = compare_avail_need(available, process, number_of_processes, number_of_resources) | ||
55 | + available = get_new_avail(available, queue, len(queue), number_of_resources) | ||
56 | + | ||
57 | + | ||
58 | + is_safe, available = check_safety(original_available, available, total_allocation, number_of_resources) | ||
59 | + | ||
60 | + process = list(temp) | ||
61 | + | ||
62 | + print("New available instances : " + str(available)) | ||
63 | + print(" Processes : " + str(process)) | ||
64 | + print(" Safe" if is_safe==True else " Not Safe") | ||
65 | + | ||
66 | + | ||
67 | + | ||
68 | + | ||
69 | + | ||
70 | +def get_total_allocation(process, number_of_processes, number_of_resources) : | ||
71 | + sum = 0 | ||
72 | + temp = [] | ||
73 | + # 각 process들의 현재 할당된 resource를 더해서 현재 전체 사용중인 resource 도출 | ||
74 | + for i in range(0, number_of_resources) : | ||
75 | + for j in range(0, number_of_processes) : | ||
76 | + sum += process[j][1][i] | ||
77 | + temp.append(sum) | ||
78 | + sum = 0 | ||
79 | + | ||
80 | + return temp | ||
81 | + | ||
82 | +def get_total_available(total_resources, total_allocation, number_of_resources) : | ||
83 | + temp = [] | ||
84 | + | ||
85 | + # 현재 사용 가능한 resource를 계산 | ||
86 | + for i in range(0,number_of_resources) : | ||
87 | + temp.append(total_resources[i] - total_allocation[i]) | ||
88 | + | ||
89 | + return temp | ||
90 | + | ||
91 | +def get_process_need(process, number_of_processes, number_of_resources) : | ||
92 | + need = [] | ||
93 | + for i in range(0, number_of_processes) : | ||
94 | + for j in range(0, number_of_resources) : | ||
95 | + need.append(process[i][0][j] - process[i][1][j]) | ||
96 | + process[i].append(need) | ||
97 | + need = [] | ||
98 | + | ||
99 | +def compare_avail_need(available, process, number_of_processes, number_of_resources) : | ||
100 | + remove_list = [] | ||
101 | + queue = [] | ||
102 | + | ||
103 | + for i in range(0, number_of_processes) : | ||
104 | + if(process[i][2] < available) : | ||
105 | + queue.append([process[i][2], process[i][0]]) | ||
106 | + remove_list.append(process[i]) | ||
107 | + queue.sort() | ||
108 | + for i in range(0, len(remove_list)) : | ||
109 | + process.remove(remove_list[i]) | ||
110 | + return queue, process | ||
111 | + | ||
112 | +def get_new_avail(available, queue, number_of_processes, number_of_resources) : | ||
113 | + for i in range(0, number_of_processes) : | ||
114 | + for j in range(0, number_of_resources) : | ||
115 | + available[j] = abs(available[j] - queue[i][0][j]) + queue[i][1][j] | ||
116 | + return available | ||
117 | + | ||
118 | +def check_safety(original_available, available, total_allocation, number_of_resources) : | ||
119 | + available = list([abs(rn_available - rn_total_allocation) for rn_available, rn_total_allocation in zip(available, total_allocation)]) | ||
120 | + return (True if (original_available == available) else False), available | ||
121 | + | ||
122 | + | ||
123 | + | ||
124 | +if __name__ == '__main__' : | ||
125 | + main() | ||
126 | + | ||
127 | + |
1 | +#_*_ coding: utf-8 _*_ | ||
2 | + | ||
3 | +process_num = 4 #process 갯수 | ||
4 | +resource_num = 3 #resourse 종류 | ||
5 | +resource_max = [] | ||
6 | + | ||
7 | +finished_process = [] # process 수행종료 여부 | ||
8 | +alloc = [] # 각 process의 초기할당 | ||
9 | +process_max = [] # 각 process의 max | ||
10 | +avail = [] # 각 process의 available | ||
11 | +need = [] # 각 process의 need | ||
12 | +safety_order = [] # safe한 process의 순서를 나타냄 | ||
13 | + | ||
14 | +def main() : | ||
15 | + | ||
16 | + # 초기 설정 작업 # | ||
17 | + | ||
18 | + # finished_process 계산 | ||
19 | + for i in range(0, process_num) : | ||
20 | + finished_process.append(False) | ||
21 | + safety_order.append(0) | ||
22 | + | ||
23 | + | ||
24 | + while(1) : | ||
25 | + | ||
26 | + print("\nPlease Enter 1 to Safe Case") | ||
27 | + print("or 2 to Unsafe Case") | ||
28 | + choice = int(input("Enter : ")) | ||
29 | + | ||
30 | + if(choice == 1) : | ||
31 | + | ||
32 | + # Safe Case의 각 resource 상한과 현재 할당량, process별 상한 지정 | ||
33 | + resource_max = [20,30,40] | ||
34 | + alloc = [[0,5,1],[2,2,2],[1,2,1],[2,8,18]] | ||
35 | + process_max = [[10,15,4],[2,25,35],[17,20,30],[10,19,20]] | ||
36 | + | ||
37 | + for j in range(0, resource_num) : # avail 계산 | ||
38 | + temp = resource_max[j] | ||
39 | + for i in range(0,process_num) : | ||
40 | + temp -= alloc[i][j] | ||
41 | + avail.append(temp) | ||
42 | + | ||
43 | + for i in range(0, process_num) : # need 계산 | ||
44 | + temp = [] | ||
45 | + for j in range(0, resource_num) : | ||
46 | + temp.append(abs(process_max[i][j]-alloc[i][j])) | ||
47 | + need.append(temp) | ||
48 | + | ||
49 | + break | ||
50 | + | ||
51 | + elif(choice == 2) : | ||
52 | + | ||
53 | + # Unsafe Case의 각 resource 상한과 현재 할당량, process별 상한 지정 | ||
54 | + resource_max = [20,30,40] | ||
55 | + alloc = [[0,5,1],[2,2,2],[1,2,1],[2,8,18]] | ||
56 | + process_max = [[20,15,4],[2,25,35],[19,20,30],[15,19,20]] | ||
57 | + | ||
58 | + for j in range(0, resource_num) : # avail 계산 | ||
59 | + temp = resource_max[j] | ||
60 | + for i in range(0,process_num) : | ||
61 | + temp -= alloc[i][j] | ||
62 | + avail.append(temp) | ||
63 | + | ||
64 | + for i in range(0, process_num) : # need 계산 | ||
65 | + temp = [] | ||
66 | + for j in range(0, resource_num) : | ||
67 | + temp.append(abs(process_max[i][j]-alloc[i][j])) | ||
68 | + need.append(temp) | ||
69 | + | ||
70 | + break | ||
71 | + | ||
72 | + else : | ||
73 | + print("Please Enter Right Value!") | ||
74 | + | ||
75 | + | ||
76 | + | ||
77 | + # 현재상황 출력 | ||
78 | + print("\n--------------Safety Algorithm--------------\n") | ||
79 | + | ||
80 | + print("Current Allocation : [A, B, C]") | ||
81 | + for i in range(0, process_num) : | ||
82 | + print(" P#"+str(i+1)+" : "+str(alloc[i])) | ||
83 | + | ||
84 | + print("\n Current Max : [A, B, C]") | ||
85 | + for i in range(0, process_num) : | ||
86 | + print(" P#"+str(i+1)+" : "+str(process_max[i])) | ||
87 | + | ||
88 | + | ||
89 | + | ||
90 | + # 비교 및 순서도출 | ||
91 | + time = 0 | ||
92 | + | ||
93 | + while(time < process_num) : | ||
94 | + for i in range(0, process_num) : | ||
95 | + k = 0 | ||
96 | + for j in range(0,resource_num) : | ||
97 | + if(need[i][j] <= avail[j]) : | ||
98 | + k += 1 # 해당 process가 available보다 작은 need를 가졌다면 count | ||
99 | + | ||
100 | + if(k == resource_num) : # 해당 process의 모든 resource가 available보다 작은 need를 가진 경우 | ||
101 | + finished_process[i] = True # 해당 process는 finish 했다고 표시됨 | ||
102 | + for l in range(0, resource_num) : | ||
103 | + avail[l] = avail[l] + alloc[i][l] # 자신이 할당했던 resource를 환원하고 | ||
104 | + need[i][l] = 10000 # 끝난 process는 need값을 많이 높여 비교대상에서 제외 | ||
105 | + | ||
106 | + safety_order[time] = i # safety_order에 현재 time에 적합한 process의 index를 설정 | ||
107 | + break | ||
108 | + | ||
109 | + time += 1 | ||
110 | + | ||
111 | + | ||
112 | + time = 0 | ||
113 | + | ||
114 | + for i in range(0, process_num) : | ||
115 | + if(finished_process[i] == True) : | ||
116 | + time += 1 # i값에 따라 True인 process를 찾고 time을 증가시킨다 | ||
117 | + | ||
118 | + if(time == process_num) : # 모든 process가 True여야만 Safe Case이다. | ||
119 | + result = "" | ||
120 | + for i in range(0, process_num) : | ||
121 | + result = result + "Process#" + str(safety_order[i]+1) + "/" | ||
122 | + print("\n Safe Processes! : " + result) | ||
123 | + | ||
124 | + else : | ||
125 | + print("\nNot Safe Processes!") | ||
126 | + | ||
127 | + | ||
128 | + print("\n--------------------------------------------\n") | ||
129 | + | ||
130 | +if __name__ == '__main__' : | ||
131 | + main() | ||
132 | + |
02_operating_systems/Java Lab/01Slide.pdf
0 → 100644
No preview for this file type
02_operating_systems/Java Lab/README.md
0 → 100644
02_operating_systems/Java Lab/lecture3.java
0 → 100644
1 | +import java.util.Scanner; | ||
2 | + | ||
3 | +public class lecture3{ | ||
4 | + public static void main(String args[]){ | ||
5 | + /* | ||
6 | + int num; | ||
7 | + | ||
8 | + System.out.print("Enter Number : "); | ||
9 | + Scanner input = new Scanner(System.in); | ||
10 | + num = input.nextInt(); | ||
11 | + | ||
12 | + if(num%5 == 0) | ||
13 | + System.out.println("HiFive"); | ||
14 | + else if(num%2 == 0) | ||
15 | + System.out.println("HiEven"); | ||
16 | + else | ||
17 | + System.out.println("HiOdd"); | ||
18 | + */ | ||
19 | + | ||
20 | + /* | ||
21 | + float bmi; | ||
22 | + | ||
23 | + System.out.print("Enter Weight : "); | ||
24 | + Scanner input = new Scanner(System.in); | ||
25 | + bmi = input.nextFloat(); | ||
26 | + | ||
27 | + if(bmi<18.5){ | ||
28 | + System.out.println("Underweight"); | ||
29 | + } | ||
30 | + else if(bmi<25){ | ||
31 | + System.out.println("Normal"); | ||
32 | + } | ||
33 | + else if(bmi<30){ | ||
34 | + System.out.println("Overweight"); | ||
35 | + } | ||
36 | + else{ | ||
37 | + System.out.println("Obese"); | ||
38 | + } | ||
39 | + */ | ||
40 | + | ||
41 | + int x = 1; | ||
42 | + | ||
43 | + System.out.println((x > 1) & (x++ < 10)); | ||
44 | + System.out.println((1 > x) && ( 1 > x++)); | ||
45 | + System.out.println((1 == x) | (10 > x++)); | ||
46 | + System.out.println((1 == x) || (10 > x++)); | ||
47 | + | ||
48 | + | ||
49 | + } | ||
50 | + | ||
51 | + | ||
52 | +} | ||
53 | + |
02_operating_systems/Java Lab/lecture4.java
0 → 100644
02_operating_systems/Java Lab/test.java
0 → 100644
1 | +import java.util.Scanner; | ||
2 | +import java.lang.Math; | ||
3 | + | ||
4 | +public class test{ | ||
5 | + public static void main(String args[]){ | ||
6 | + | ||
7 | + double radius; | ||
8 | + double area; | ||
9 | + | ||
10 | + Scanner input = new Scanner(System.in); | ||
11 | + System.out.print("Enter a double value: "); | ||
12 | + radius = input.nextDouble(); | ||
13 | + | ||
14 | + //radius = 20; | ||
15 | + | ||
16 | + area = Math.pow(radius, 2) * 3.14159; | ||
17 | + | ||
18 | + System.out.println(area); | ||
19 | + | ||
20 | + | ||
21 | + | ||
22 | + | ||
23 | + } | ||
24 | + | ||
25 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
02_operating_systems/Java Lab/test2.java
0 → 100644
1 | +import java.util.Scanner; | ||
2 | +import java.lang.Math; | ||
3 | + | ||
4 | +public class test2{ | ||
5 | + public static void main(String args[]){ | ||
6 | + | ||
7 | + double fahrenheit; | ||
8 | + double celcious; | ||
9 | + | ||
10 | + Scanner input = new Scanner(System.in); | ||
11 | + System.out.print("Enter a fahrenheit degree: "); | ||
12 | + fahrenheit = input.nextDouble(); | ||
13 | + | ||
14 | + | ||
15 | + celcious = (5.0 / 9)*(fahrenheit – 32); | ||
16 | + | ||
17 | + System.out.println(fahrenheit+"fahrenheit degree is "+celcious+"celcious degree"); | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | + } | ||
22 | + | ||
23 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
02_operating_systems/README.md
0 → 100644
-
Please register or login to post a comment