-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
121 lines (102 loc) · 4.03 KB
/
main.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
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
import copy
import FCFS
import GetInput
import RR
import SALT
import SJF
import SRT
import timeCalc
process_details = GetInput.get_input('./inputFile.csv')
process_details_copy = copy.deepcopy(process_details)
wt = []
tat = []
resp = []
avg_wt = 0
avg_tat = 0
avg_resp = 0
utilization = 0
throughput = 0
idle = 0
total = 0
n = len(process_details.keys())
# for keys in process_details:
# print(str(keys) + ' : ' + str(process_details[keys]))
# FCFS.fcfs(process_details)
# SJF.sjf(process_details)
# SRT.srt(process_details)
# awt tat claculation
# current_time = 5
# process = [5, 4, 'p3']
# ready_queue = [[6, 4, 'p1'], [12, 3, 'p2'], [6, 2, 'p4'], [13, 5, 'p5']]
# print(SALT.next_process(ready_queue, process, current_time))
# SALT.srtf(process_details)
# RR.rr(process_details, 3)
# 66char
def print_result(result, algorithm):
avg_wt = timeCalc.avg_time(n, wt)
avg_tat = timeCalc.avg_time(n, tat)
avg_resp = timeCalc.avg_time(n, resp)
utilization = timeCalc.utilization(total, idle)
throughput = timeCalc.throughput(total, n)
print("+============+===========+==================+===============+==============+")
print("|" + ' '*((74-len(algorithm))//2) + algorithm + ' '*((74-len(algorithm))//2) + "|")
print("+============+===========+==================+===============+==============+")
print("| Process ID | Start-End | Turn Around Time | Response Time | Waiting Time |")
print("+------------+-----------+------------------+---------------+--------------+")
for key in list(result.keys())[:-2:]:
index = list(result.keys()).index(key)
print('| ' + key + ' '*9, end='')
print('| ' + str(result[key]['start']) + '-' + str(result[key]['end']) + ' ' * (9 - len(str(result[key]['end']) + str(result[key]['start']))), end='')
print('| ' + str(tat[index]) + ' ' * (17 - len(str(tat[index]))), end='')
print('| ' + str(resp[index]) + ' ' * (14 - len(str(resp[index]))), end='')
print('| ' + str(wt[index]) + ' ' * (13 - len(str(wt[index]))), end='')
print('|', end='\n')
print("+------------+-----------+------------------+---------------+--------------+")
print('| Average' + ' '*16 + '| ' + str(avg_tat) + ' '*(17 - len(str(avg_tat))), end='')
print('| ' + str(avg_resp) + ' ' * (14 - len(str(avg_resp))), end='')
print('| ' + str(avg_wt) + ' ' * (12 - len(str(avg_wt))), end='')
print('|', end='\n')
print("+------------+-----------+------------------+---------------+--------------+")
print('\n')
print('Total Time: ' + str(total))
print('Idle Time: ' + str(idle))
print('CPU Utilization: ', utilization)
print('Throughput: ', throughput)
def create_time_lists(res, process_detail):
for key in list(res.keys())[:-2:]:
cpu_time1 = process_detail[key]['cpu_time1']
cpu_time2 = process_detail[key]['cpu_time2']
end = res[key]['end']
start = res[key]['start']
arrival = process_detail[key]['arrival_time']
io = process_detail[key]['io_time']
wt.append(timeCalc.waiting_time(arrival, end, (cpu_time1 + cpu_time2), io))
tat.append(timeCalc.turn_around_time(arrival, end))
resp.append(timeCalc.response_time(start, arrival))
algorithm = int(input())
result = {}
if algorithm == 1:
result = FCFS.fcfs(process_details)
create_time_lists(result, process_details_copy)
total = result['total']
idle = result['idle']
print_result(result, 'FCFS')
elif algorithm == 2:
result = SJF.sjf(process_details)
create_time_lists(result, process_details_copy)
total = result['total']
idle = result['idle']
print_result(result, 'SJF')
elif algorithm == 3:
tq = int(input())
result = RR.rr(process_details, tq)
create_time_lists(result, process_details_copy)
total = result['total']
idle = result['idle']
print_result(result, 'RoundRobin')
elif algorithm == 4:
result = SALT.srtf(process_details)
create_time_lists(result, process_details_copy)
total = result['total']
idle = result['idle']
print_result(result, 'SRTF')