-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
60 lines (51 loc) ยท 1.48 KB
/
app.py
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
import json
import schedulers
with open('./process.json') as proc_file:
procs = json.load(proc_file)
for proc in procs:
proc['started'] = 0
proc['terminated'] = 0
proc['wait'] = 0
queue = []
current = None
time = 0
while True:
print('t={}'.format(time))
# HRRN์ ์ํด์ ๋๊ธฐ์๊ฐ ๊ตฌํ๊ธฐ
for proc in queue:
proc['wait'] = time - proc['arrival']
# ๋์ฐฉ ํ๋ก์ธ์ค ํ์ธ
arrived = [proc for proc in procs if proc['arrival'] == time]
arrived = sorted(arrived, key=lambda k: k['id'])
queue += arrived
# ์ข
๋ฃ ํ๋ก์ธ์ค ํ์ธ
if current:
if current['execution'] + current['started'] == time:
print('p{} terminated'.format(current['id']))
current['terminated'] = time
current = None
# ํ์ฌ ํ๋ก์ธ์ค ํ์ธ
if not current:
try:
# current = schedulers.first_in_first_out(queue)
# current = schedulers.shortest_job_first(queue)
current = schedulers.highest_response_ratio_next(queue)
except:
break
print('p{} started'.format(current['id']))
current['started'] = time
time += 1
print('\n=== results ===')
awt = 0
att = 0
for proc in procs:
print('#p{}'.format(proc['id']), end='')
wt = proc['started'] - proc['arrival']
print(', wait time: {}'.format(wt), end='') # ๋๊ธฐ ์๊ฐ
tt = proc['terminated'] - proc['arrival']
print(', termination time: {}'.format(tt)) # ๋ฐํ ์๊ฐ
awt += wt
att += tt
awt /= len(procs)
att /= len(procs)
print('\nAWT: {}\nATT: {}'.format(awt, att))