Repository navigation
Expand file tree
/
Copy pathThread Scheduling.py
More file actions
563 lines (448 loc) · 25.8 KB
/
Copy pathThread Scheduling.py
File metadata and controls
563 lines (448 loc) · 25.8 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import sys
def findavgTime_FCFS(processes, n, arrival_time, burst_time):
ct = [0] * n # 완료 시간
wt = [0] * n # 대기 시간
tat = [0] * n # 반환 시간
total_wt = 0 # 총 대기 시간
total_tat = 0 # 총 반환 시간
current_time = 0 # 간트 차트에서 현재 시간을 추적
schedule = [] # 간트 차트
# 완료 시간 계산
for i in range(n):
if current_time < arrival_time[i]:
current_time = arrival_time[i] # 프로세스 도착 시간 이후로 대기
ct[i] = current_time + burst_time[i] # 완료 시간 계산
current_time = ct[i] # 현재 시간 업데이트
# 반환 시간과 대기 시간 계산
for i in range(n):
tat[i] = ct[i] - arrival_time[i] # 반환 시간 = 완료 시간 - 도착 시간
wt[i] = tat[i] - burst_time[i] # 대기 시간 = 반환 시간 - 실행 시간
# 간트 차트 생성
current_time = 0
for i in range(n):
if current_time < arrival_time[i]:
current_time = arrival_time[i] # 프로세스 도착 시간 이후로 간트 차트 갱신
schedule.append(f"{current_time} : {processes[i]} ({burst_time[i]})")
current_time += burst_time[i] # 간트 차트 시간 업데이트
schedule.append(f"{current_time} : E") # 종료 지점 추가
# 총 대기 시간과 반환 시간 계산
total_wt = sum(wt) # 총 대기 시간
total_tat = sum(tat) # 총 반환 시간
# 평균 반환 시간과 대기 시간 계산
avg_tat = total_tat / n # 평균 반환 시간
avg_wt = total_wt / n # 평균 대기 시간
# 마지막으로 완료된 프로세스의 종료 시간 반환
last_completed_time = current_time
return schedule, last_completed_time, avg_tat, avg_wt
def findavgTime_SJF_non_preemptive(processes, n, arrival_time, burst_time):
completed = [False] * n # 각 프로세스의 완료 여부
time = 0 # 현재 시간
tat = [0] * n # 반환 시간 (Turnaround Time)
wt = [0] * n # 대기 시간 (Waiting Time)
completion_time = [0] * n # 완료 시간 기록
schedule = [] # 간트 차트
# 모든 프로세스가 완료될 때까지 반복
while any(not completed[i] for i in range(n)):
# 현재 시간까지 도착한 프로세스 중에서 실행 시간이 가장 짧은 프로세스 선택
available_processes = [
(i, burst_time[i]) for i in range(n) if arrival_time[i] <= time and not completed[i]
]
if not available_processes: # 실행 가능한 프로세스가 없을 경우 Idle 상태
schedule.append(f"{time} : Idle")
time += 1 # Idle 시간을 1 증가
continue
# 실행 시간이 가장 짧은 프로세스 선택
shortest_process = min(available_processes, key=lambda x: x[1])[0]
# 간트 차트에 프로세스 기록
schedule.append(f"{time} : {processes[shortest_process]} ({burst_time[shortest_process]})")
# 프로세스 실행
time += burst_time[shortest_process]
completion_time[shortest_process] = time # 완료 시간 기록
completed[shortest_process] = True # 프로세스 완료 처리
# 반환 시간(TAT) 및 대기 시간(WT) 계산
tat[shortest_process] = time - arrival_time[shortest_process] # TAT = 완료 시간 - 도착 시간
wt[shortest_process] = tat[shortest_process] - burst_time[shortest_process] # WT = TAT - 실행 시간
# 종료 시점 추가
schedule.append(f"{time} : E")
# 평균 반환 시간(TAT) 및 대기 시간(WT) 계산
avg_tat = sum(tat) / n # 평균 반환 시간
avg_wt = sum(wt) / n # 평균 대기 시간
# 마지막으로 완료된 프로세스의 종료 시간 반환
last_completed_time = max(completion_time)
return schedule, last_completed_time, avg_tat, avg_wt
def findavgTime_SJF_preemptive(processes, n, arrival_time, burst_time):
# 초기화
remaining_time = burst_time[:] # 각 프로세스의 남은 실행 시간
completion_time = [0] * n # 각 프로세스의 완료 시간
waiting_time = [0] * n # 대기 시간
turnaround_time = [0] * n # 반환 시간
is_completed = [False] * n # 프로세스 완료 여부
time = 0 # 현재 시간
completed = 0 # 완료된 프로세스 수
schedule = [] # 간트 차트
last_process = None # 마지막으로 실행된 프로세스
start_time = 0 # 현재 프로세스의 시작 시간
# 모든 프로세스가 완료될 때까지 반복
while completed != n:
# 실행 가능한 프로세스 중 남은 실행 시간이 가장 짧은 프로세스 선택
idx = -1
min_bt = float('inf')
for i in range(n):
if arrival_time[i] <= time and not is_completed[i]:
if remaining_time[i] < min_bt: # 더 짧은 실행 시간을 가진 프로세스 찾기
min_bt = remaining_time[i]
idx = i
elif remaining_time[i] == min_bt: # 실행 시간이 같은 경우 도착 시간이 빠른 프로세스 선택
if arrival_time[i] < arrival_time[idx]:
idx = i
if idx != -1: # 실행할 프로세스가 있는 경우
# 새로운 프로세스 실행 시작 기록
if last_process is None or last_process != processes[idx]:
if last_process is not None: # 이전 프로세스 종료 기록
executed_time = time - start_time
schedule.append(f"{start_time} : {last_process} ({executed_time})")
start_time = time # 현재 프로세스의 시작 시간 업데이트
last_process = processes[idx]
remaining_time[idx] -= 1 # 남은 실행 시간 감소
time += 1 # 현재 시간 증가
# 프로세스 완료 처리
if remaining_time[idx] == 0:
is_completed[idx] = True
completed += 1
completion_time[idx] = time # 완료 시간 기록
turnaround_time[idx] = completion_time[idx] - arrival_time[idx] # 반환 시간 계산
waiting_time[idx] = turnaround_time[idx] - burst_time[idx] # 대기 시간 계산
else: # 실행 가능한 프로세스가 없는 경우 (Idle 상태)
if last_process is not None: # 이전 프로세스 종료 기록
executed_time = time - start_time
schedule.append(f"{start_time} : {last_process} ({executed_time})")
last_process = None # Idle 상태 시작
time += 1 # 시간 증가
# 마지막으로 실행된 프로세스 종료 기록
if last_process is not None:
executed_time = time - start_time
schedule.append(f"{start_time} : {last_process} ({executed_time})")
# 종료 시점 추가
schedule.append(f"{time} : E")
# 평균 반환 시간(TAT) 및 대기 시간(WT) 계산
avg_tat = sum(turnaround_time) / n # 평균 반환 시간
avg_wt = sum(waiting_time) / n # 평균 대기 시간
# 마지막 프로세스 완료 시간 반환
last_completed_time = time
return schedule, last_completed_time, avg_tat, avg_wt
def findavgTime_LJF(processes, n, arrival_time, burst_time):
# 각 프로세스의 완료 시간, 대기 시간, 반환 시간 초기화
completion_time = [0] * n # 완료 시간
waiting_time = [0] * n # 대기 시간
turnaround_time = [0] * n # 반환 시간
is_completed = [False] * n # 완료 여부
time = 0 # 현재 시간
completed = 0 # 완료된 프로세스 수
schedule = [] # 간트 차트 (스케줄링 결과)
# 모든 프로세스가 완료될 때까지 반복
while completed != n:
# 도착한 프로세스 중 실행 시간이 가장 긴 프로세스 선택
idx = -1
max_bt = float('-inf')
for i in range(n):
if arrival_time[i] <= time and not is_completed[i]:
if burst_time[i] > max_bt: # 더 긴 실행 시간을 가진 프로세스 찾기
max_bt = burst_time[i]
idx = i
elif burst_time[i] == max_bt: # 실행 시간이 동일한 경우 도착 시간이 빠른 프로세스 선택
if arrival_time[i] < arrival_time[idx]:
idx = i
if idx != -1: # 실행할 프로세스가 있는 경우
# 간트 차트에 프로세스 기록
if len(schedule) == 0 or schedule[-1].split(" : ")[1].split()[0] != processes[idx]:
schedule.append(f"{time} : {processes[idx]} ({burst_time[idx]})")
# 선택된 프로세스 실행
time += burst_time[idx] # 현재 시간 증가
completion_time[idx] = time # 완료 시간 기록
turnaround_time[idx] = completion_time[idx] - arrival_time[idx] # 반환 시간 계산
waiting_time[idx] = turnaround_time[idx] - burst_time[idx] # 대기 시간 계산
is_completed[idx] = True # 프로세스 완료 처리
completed += 1 # 완료된 프로세스 수 증가
else:
# 실행할 수 있는 프로세스가 없는 경우 (Idle 상태)
schedule.append(f"{time} : Idle")
time += 1 # 시간 증가
# 종료 시점 기록
schedule.append(f"{time} : E")
# 평균 반환 시간(TAT) 및 대기 시간(WT) 계산
avg_tat = sum(turnaround_time) / n # 평균 반환 시간
avg_wt = sum(waiting_time) / n # 평균 대기 시간
# 마지막으로 완료된 프로세스의 종료 시간 반환
last_completed_time = max(completion_time)
return schedule, last_completed_time, avg_tat, avg_wt
def findavgTime_LRTF(processes, n, arrival_time, burst_time):
current_time = 0 # 현재 시간
completed = 0 # 완료된 프로세스 수
schedule = [] # 간트 차트 (스케줄링 결과)
remaining_time = burst_time[:] # 각 프로세스의 남은 실행 시간
completion_time = [0] * n # 각 프로세스의 완료 시간
tat = [0] * n # 반환 시간 (Turnaround Time)
wt = [0] * n # 대기 시간 (Waiting Time)
last_executed_process = None # 마지막으로 실행된 프로세스
# 실행 가능한 프로세스 중 남은 실행 시간이 가장 긴 프로세스를 찾는 함수
def findLargest():
max_index = -1
max_bt = -1
for i in range(n):
if arrival_time[i] <= current_time and remaining_time[i] > 0: # 현재 실행 가능한 프로세스
if remaining_time[i] > max_bt: # 실행 시간이 가장 긴 프로세스 선택
max_bt = remaining_time[i]
max_index = i
return max_index
# 모든 프로세스가 완료될 때까지 반복
while completed < n:
index = findLargest() # 남은 실행 시간이 가장 긴 프로세스 선택
if index != -1: # 실행 가능한 프로세스가 있는 경우
# 새 프로세스가 실행될 경우 간트 차트에 이전 프로세스 기록
if last_executed_process != processes[index]:
if last_executed_process is not None and last_executed_process != "Idle":
schedule.append(f"{start_time} : {last_executed_process} ({current_time - start_time})")
start_time = current_time # 새 프로세스의 시작 시간 기록
last_executed_process = processes[index]
current_time += 1 # 현재 시간 증가
remaining_time[index] -= 1 # 남은 실행 시간 감소
if remaining_time[index] == 0: # 프로세스 완료 시 처리
completed += 1 # 완료된 프로세스 수 증가
completion_time[index] = current_time # 완료 시간 기록
else: # 실행 가능한 프로세스가 없는 경우 (Idle 상태)
if last_executed_process != "Idle":
if last_executed_process is not None:
schedule.append(f"{start_time} : {last_executed_process} ({current_time - start_time})")
schedule.append(f"{current_time} : Idle") # Idle 상태 기록
last_executed_process = "Idle"
start_time = current_time
current_time += 1 # 시간 증가
# 마지막 프로세스 종료 기록
if last_executed_process is not None and last_executed_process != "Idle":
schedule.append(f"{start_time} : {last_executed_process} ({current_time - start_time})")
schedule.append(f"{current_time} : E") # 종료 시점 기록
# 반환 시간 및 대기 시간 계산
for i in range(n):
tat[i] = completion_time[i] - arrival_time[i] # TAT = 완료 시간 - 도착 시간
wt[i] = tat[i] - burst_time[i] # WT = TAT - 실행 시간
# 평균 반환 시간(TAT) 및 대기 시간(WT) 계산
avg_tat = sum(tat) / n
avg_wt = sum(wt) / n
# 마지막으로 완료된 프로세스의 종료 시간 반환
last_completed_time = max(completion_time)
return schedule, last_completed_time, avg_tat, avg_wt
def findavgTime_Priority_non_preemptive(processes, n, arrival_time, burst_time, priority):
# 초기화
remaining_processes = list(zip(processes, arrival_time, burst_time, priority)) # 남은 프로세스 리스트
time = 0 # 현재 시간
waiting_time_total = 0 # 총 대기 시간
turnaround_time_total = 0 # 총 반환 시간
last_completed_time = 0 # 마지막 완료 시간
schedule = [] # 간트 차트 기록
# 모든 프로세스가 완료될 때까지 반복
while remaining_processes:
# 현재 시간까지 도착한 프로세스 필터링
available_processes = [p for p in remaining_processes if p[1] <= time]
if available_processes: # 실행 가능한 프로세스가 있는 경우
# 우선순위가 높은 프로세스 선택 (우선순위 값이 클수록 높은 우선순위)
available_processes.sort(key=lambda x: (-x[3], x[1])) # 우선순위 > 도착 시간 기준 정렬
current_process = available_processes[0] # 가장 높은 우선순위 프로세스 선택
remaining_processes.remove(current_process) # 선택된 프로세스를 남은 프로세스 리스트에서 제거
process, at, bt, prio = current_process # 프로세스 정보 해제
# 간트 차트에 프로세스 실행 기록
schedule.append(f"{time} : {process} ({bt})")
# 프로세스 실행 후 시간 업데이트
time += bt
# 대기 시간 계산 (대기 시간 = 시작 시간 - 도착 시간)
waiting_time = time - at - bt
waiting_time_total += waiting_time
# 반환 시간 계산 (반환 시간 = 종료 시간 - 도착 시간)
turnaround_time = time - at
turnaround_time_total += turnaround_time
# 완료 시간 갱신
last_completed_time = time
else:
# 실행 가능한 프로세스가 없는 경우 시간을 다음 도착 시간으로 이동
if remaining_processes:
time = min([p[1] for p in remaining_processes]) # 가장 빠른 도착 시간으로 이동
# 종료 시간 기록
schedule.append(f"{time} : E")
# 평균 반환 시간 및 대기 시간 계산
avg_tat = turnaround_time_total / n # 평균 반환 시간
avg_wt = waiting_time_total / n # 평균 대기 시간
# 결과 반환
return schedule, last_completed_time, avg_tat, avg_wt
def findavgTime_Priority_preemptive(processes, n, arrival_time, burst_time, priority):
# 프로세스 정보를 초기화 (도착 시간 기준 오름차순, 우선순위 기준 내림차순 정렬)
process_info = sorted(zip(processes, arrival_time, burst_time, priority), key=lambda x: (x[1], -x[3]))
# 초기 변수 설정
time = 0 # 현재 시간
waiting_time = [0] * n # 각 프로세스의 대기 시간
turnaround_time = [0] * n # 각 프로세스의 반환 시간
remaining_burst_time = [bt for _, _, bt, _ in process_info] # 각 프로세스의 남은 실행 시간
is_completed = [False] * n # 프로세스 완료 여부
schedule = [] # 간트 차트 결과 저장
completed = 0 # 완료된 프로세스 수
current_process = None # 현재 실행 중인 프로세스
process_start_time = 0 # 현재 프로세스의 시작 시간
last_completed_time = 0 # 최종 완료 시간
# 모든 프로세스가 완료될 때까지 반복
while completed < n:
# 현재 시간까지 도착한 프로세스 필터링
available_processes = [i for i, p in enumerate(process_info) if p[1] <= time and not is_completed[i]]
if not available_processes: # 실행 가능한 프로세스가 없으면 시간 증가
time += 1
continue
# 우선순위가 가장 높은 프로세스 선택 (우선순위 같으면 도착 시간이 빠른 순)
next_process_index = max(available_processes, key=lambda i: (process_info[i][3], -process_info[i][1]))
process, at, bt, prio = process_info[next_process_index]
# 실행 중인 프로세스가 변경된 경우 간트 차트에 이전 프로세스 기록
if current_process != process:
if current_process is not None:
schedule.append(f"{process_start_time} : {current_process} ({time - process_start_time})")
current_process = process
process_start_time = time # 새 프로세스의 시작 시간 갱신
# 현재 프로세스를 1 단위 시간 실행
remaining_burst_time[next_process_index] -= 1
time += 1
# 프로세스 완료 시 처리
if remaining_burst_time[next_process_index] == 0:
is_completed[next_process_index] = True # 완료 처리
completed += 1 # 완료된 프로세스 수 증가
turnaround_time[next_process_index] = time - at # 반환 시간 계산
waiting_time[next_process_index] = turnaround_time[next_process_index] - bt # 대기 시간 계산
# 마지막 프로세스의 실행 정보 간트 차트에 추가
if current_process is not None:
schedule.append(f"{process_start_time} : {current_process} ({time - process_start_time})")
# 종료 시점 추가
schedule.append(f"{time} : E")
# 평균 반환 시간 및 대기 시간 계산
avg_tat = sum(turnaround_time) / n # 평균 반환 시간
avg_wt = sum(waiting_time) / n # 평균 대기 시간
last_completed_time = time # 최종 완료 시간 갱신
# 결과 반환
return schedule, last_completed_time, avg_tat, avg_wt
def findavgTime_RR(processes, n, arrival_time, burst_time, quantum):
rem_bt = burst_time[:] # 남은 실행 시간 초기화
wt = [0] * n # 각 프로세스의 대기 시간
tat = [0] * n # 각 프로세스의 반환 시간
completed_time = [0] * n # 각 프로세스의 완료 시간
raw_schedule = [] # 원시 간트 차트 저장
current_time = 0 # 현재 시간
queue = [] # 대기열 초기화
# 초기 대기열 설정 (현재 시간에 도착한 프로세스 추가)
for i in range(n):
if arrival_time[i] <= current_time:
queue.append(i)
# 모든 프로세스를 처리할 때까지 반복
while queue:
idx = queue.pop(0) # 대기열에서 첫 번째 프로세스 가져오기
if rem_bt[idx] > 0: # 실행 가능한 경우
if rem_bt[idx] > quantum: # 실행 시간이 time quantum보다 긴 경우
# 간트 차트에 실행 정보 기록
raw_schedule.append((current_time, processes[idx], quantum))
current_time += quantum # 현재 시간 업데이트
rem_bt[idx] -= quantum # 남은 실행 시간 감소
queue.append(idx) # 프로세스를 다시 대기열에 추가
else: # 실행 시간이 time quantum 이하인 경우
# 프로세스 종료 처리
raw_schedule.append((current_time, processes[idx], rem_bt[idx]))
current_time += rem_bt[idx] # 남은 실행 시간만큼 실행
rem_bt[idx] = 0 # 실행 완료
completed_time[idx] = current_time # 완료 시간 기록
wt[idx] = current_time - arrival_time[idx] - burst_time[idx] # 대기 시간 계산
# 새로운 프로세스가 도착한 경우 대기열에 추가
for i in range(n):
if (
arrival_time[i] <= current_time # 도착 시간이 현재 시간 이하
and i not in queue # 대기열에 없는 프로세스
and rem_bt[i] > 0 # 실행 시간이 남아 있는 프로세스
and i != idx # 현재 처리 중인 프로세스 제외
):
queue.append(i)
# 반환 시간 계산
for i in range(n):
tat[i] = completed_time[i] - arrival_time[i] # 반환 시간 = 완료 시간 - 도착 시간
# 종료 시점 추가
raw_schedule.append((current_time, "E", 0))
# 연속 실행된 프로세스를 병합하여 최종 스케줄 생성
schedule = []
for i in range(len(raw_schedule)):
if i > 0 and raw_schedule[i][1] == raw_schedule[i - 1][1]:
# 같은 프로세스라면 시간을 병합
start_time, process, duration = schedule.pop()
schedule.append((start_time, process, duration + raw_schedule[i][2]))
else:
schedule.append(raw_schedule[i])
# 스케줄을 출력 형식으로 변환
formatted_schedule = [f"{start} : {process} ({duration})" for start, process, duration in schedule if process != "E"]
# 평균 반환 시간 및 대기 시간 계산
avg_tat = sum(tat) / n # 평균 반환 시간
avg_wt = sum(wt) / n # 평균 대기 시간
# 마지막 완료 시간 반환
last_completed_time = current_time
return formatted_schedule + [f"{current_time} : E"], last_completed_time, avg_tat, avg_wt
# 메인 함수 정의
def main():
# 입력 데이터를 저장할 리스트 초기화
processes = [] # 프로세스 이름
arrival_time = [] # 도착 시간
burst_time = [] # 실행 시간
priority = [] # 우선순위
quantum = 1 # Round Robin 스케줄링에서 사용할 시간 퀀텀
try:
# 표준 입력에서 데이터 읽기
for line in sys.stdin: # 사용자 입력을 한 줄씩 읽음
line = line.strip()
if line.upper() == 'E': # 'E' 입력 시 데이터 입력 종료
break
details = line.split()
if len(details) != 4: # 입력 형식 확인
raise ValueError(f"Invalid input format: {line}")
# 입력 데이터를 각 리스트에 추가
processes.append(details[0]) # 프로세스 이름
arrival_time.append(int(details[1])) # 도착 시간
burst_time.append(int(details[2])) # 실행 시간
priority.append(int(details[3])) # 우선순위
n = len(processes) # 총 프로세스 수
results = {} # 결과를 저장할 딕셔너리
# FCFS 스케줄링
results["FCFS"] = findavgTime_FCFS(processes, n, arrival_time, burst_time)
# SJF Non Preemptive 스케줄링
results["SJF (non preemptive)"] = findavgTime_SJF_non_preemptive(processes, n, arrival_time, burst_time)
# SJF Preemptive 스케줄링
results["SJF (preemptive)"] = findavgTime_SJF_preemptive(processes, n, arrival_time, burst_time)
# LJF Non Preemptive 스케줄링
results["LJF (non preemptive)"] = findavgTime_LJF(processes, n, arrival_time, burst_time)
# LRTF 스케줄링
results["LJF (preemptive)"] = findavgTime_LRTF(processes, n, arrival_time, burst_time)
# Priority Non Preemptive 스케줄링
results["Priority (non preemptive)"] = findavgTime_Priority_non_preemptive(
processes, n, arrival_time, burst_time, priority
)
# Priority Preemptive 스케줄링
results["Priority (preemptive)"] = findavgTime_Priority_preemptive(
processes, n, arrival_time, burst_time, priority
)
# Round Robin 스케줄링
results["RR"] = findavgTime_RR(processes, n, arrival_time, burst_time, quantum)
# 스케줄링 결과 출력
for algorithm, result in results.items():
schedule, last_completed_time, avg_tat, avg_wt = result # 결과에서 4개의 값 추출
print(f"{algorithm}") # 알고리즘 이름 출력
for entry in schedule: # 간트 차트 출력
print(entry)
print()
# 요약 테이블 출력
print("Results Completed Time Turnaround Time Waiting Time")
print("----------------------------------------------------------------------")
for algorithm, result in results.items():
schedule, last_completed_time, avg_tat, avg_wt = result
# 각 스케줄링 알고리즘의 결과 출력
print(f"{algorithm:<27}{last_completed_time:<18.2f}{avg_tat:<18.2f}{avg_wt:<18.2f}")
except Exception as e:
# 예외 발생 시 에러 메시지 출력
print(f"An error occurred: {e}")
# 메인 함수 실행
if __name__ == "__main__":
main()