-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
186 lines (123 loc) · 4.91 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# ----------------------------- RR -----------------------------
def RR(data):
global process_time
value = 0
count = 0
while sum(jobs[data]) != 0:
for i in range(len(jobs[data])):
if jobs[data][i] == 0:
continue
if count == time:
value = 1
break
jobs[data][i] -= 1
count += 1
process_time += 1
if jobs[data][i] == 0:
turnaround_time[data][i] = process_time
if value:
break
print("---------RR---------", " ".join([str(i) for i in jobs[data]]))
# -------------------------- SJF 01 --------------------------
def SJF1(data):
global process_time
value = 0
count = time
while sum(jobs[data]) != 0:
if count == 0:
break
non_zero_list = [x for x in jobs[data] if x != 0]
index = jobs[data].index(min(non_zero_list))
if min(non_zero_list) >= count:
process_time += count
jobs[data][index] -= count
count = 0
if jobs[data][index] == 0:
turnaround_time[data][index] = process_time
break
process_time += jobs[data][index]
jobs[data][index] = 0
count -= min(non_zero_list)
if jobs[data][index] == 0:
turnaround_time[data][index] = process_time
print("--------SJF1--------", " ".join([str(i) for i in jobs[data]]))
# -------------------------- SJF 02 --------------------------
def SJF2(data):
global process_time
value = 0
count = time
while sum(jobs[data]) != 0:
if count == 0:
break
non_zero_list = [x for x in jobs[data] if x != 0]
index = jobs[data].index(min(non_zero_list))
if min(non_zero_list) >= count:
process_time += count
jobs[data][index] -= count
count = 0
if jobs[data][index] == 0:
turnaround_time[data][index] = process_time
break
process_time += jobs[data][index]
jobs[data][index] = 0
count -= min(non_zero_list)
if jobs[data][index] == 0:
turnaround_time[data][index] = process_time
print("--------SJF2--------", " ".join([str(i) for i in jobs[data]]))
# ---------------------------- FCFS ----------------------------
def FCFS(data):
global process_time
value = 0
count = time
for i in range(len(jobs[data])):
if count == 0 or sum(jobs[data]) == 0:
break
if jobs[data][i] == 0:
continue
if jobs[data][i] >= count:
jobs[data][i] -= count
process_time += count
count = 0
if jobs[data][i] == 0:
turnaround_time[data][i] = process_time
break
process_time += jobs[data][i]
count -= jobs[data][i]
jobs[data][i] = 0
if jobs[data][i] == 0:
turnaround_time[data][i] = process_time
print("--------FCFS--------", " ".join([str(i) for i in jobs[data]]))
def Scheduler():
for i in priority:
if sum(jobs[i]) != 0:
globals()[i](i)
if __name__ == "__main__":
time = 20
process_time = 0
priority = {'RR': 0, 'SJF1': 1, 'SJF2': 2, 'FCFS': 3}
jobs = {'RR': [], 'SJF1': [], 'SJF2': [], 'FCFS': []}
for i in priority:
jobs[i] = [int(i) for i in input("Enter values for " + i + "(priority level " + str(priority[i]) + "): ").split()]
turnaround_time = {'RR': [0] * len(jobs['RR']), 'SJF1': [0] * len(jobs['SJF1']), 'SJF2': [0] * len(jobs['SJF2']), 'FCFS': [0] * len(jobs['FCFS'])}
waiting_time = {'RR': [0] * len(jobs['RR']), 'SJF1': [0] * len(jobs['SJF1']), 'SJF2': [0] * len(jobs['SJF2']), 'FCFS': [0] * len(jobs['FCFS'])}
burst_time = {'RR': jobs['RR'].copy(), 'SJF1': jobs['SJF1'].copy(), 'SJF2': jobs['SJF2'].copy(), 'FCFS': jobs['FCFS'].copy()}
print()
while True:
if sum(jobs["RR"]) == 0 and sum(jobs["SJF1"]) == 0 and sum(jobs["SJF2"]) == 0 and sum(jobs["FCFS"]) == 0:
break
Scheduler()
for i in jobs:
for j in range(len(turnaround_time[i])):
waiting_time[i][j] = turnaround_time[i][j] - burst_time[i][j]
print("\n")
print("Burst Times:")
for i in priority:
print("Priority level", priority[i], ":", ", ".join(str(i) for i in burst_time[i]))
print()
print("Consume Times:")
for i in priority:
print("Priority level", priority[i], ":", ", ".join(str(i) for i in turnaround_time[i]))
print()
print("Waiting Times:")
for i in priority:
print("Priority level", priority[i], ":", ", ".join(str(i) for i in waiting_time[i]))