-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduling.py
91 lines (85 loc) · 3.63 KB
/
Scheduling.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
class Process:
def __init__(self, pid, arrival_time, burst_time, priority=0):
self.pid = pid
self.arrival_time = arrival_time
self.burst_time = burst_time
self.priority = priority
self.waiting_time = 0
self.turnaround_time = 0
self.remaining_time = burst_time
def fcfs(processes):
processes.sort(key=lambda x: x.arrival_time)
current_time = 0
for process in processes:
if current_time < process.arrival_time:
current_time = process.arrival_time
process.waiting_time = current_time - process.arrival_time
process.turnaround_time = process.waiting_time + process.burst_time
current_time += process.burst_time
def sjf(processes):
processes.sort(key=lambda x: (x.arrival_time, x.burst_time))
current_time = 0
for process in processes:
if current_time < process.arrival_time:
current_time = process.arrival_time
process.waiting_time = current_time - process.arrival_time
process.turnaround_time = process.waiting_time + process.burst_time
current_time += process.burst_time
def srtf(processes):
processes.sort(key=lambda x: x.arrival_time)
current_time = 0
while processes:
available_processes = [p for p in processes if p.arrival_time <= current_time]
if not available_processes:
current_time = processes[0].arrival_time
continue
process = min(available_processes, key=lambda x: x.remaining_time)
processes.remove(process)
if process.remaining_time > 1:
current_time += 1
process.remaining_time -= 1
processes.append(process)
else:
current_time += process.remaining_time
process.waiting_time = current_time - process.arrival_time - process.burst_time
process.turnaround_time = process.waiting_time + process.burst_time
def priority_non_preemptive(processes):
processes.sort(key=lambda x: (x.arrival_time, x.priority))
current_time = 0
for process in processes:
if current_time < process.arrival_time:
current_time = process.arrival_time
process.waiting_time = current_time - process.arrival_time
process.turnaround_time = process.waiting_time + process.burst_time
current_time += process.burst_time
def priority_preemptive(processes):
processes.sort(key=lambda x: x.arrival_time)
current_time = 0
while processes:
available_processes = [p for p in processes if p.arrival_time <= current_time]
if not available_processes:
current_time = processes[0].arrival_time
continue
process = min(available_processes, key=lambda x: x.priority)
processes.remove(process)
if process.remaining_time > 1:
current_time += 1
process.remaining_time -= 1
processes.append(process)
else:
current_time += process.remaining_time
process.waiting_time = current_time - process.arrival_time - process.burst_time
process.turnaround_time = process.waiting_time + process.burst_time
def round_robin(processes, quantum):
queue = processes[:]
current_time = 0
while queue:
process = queue.pop(0)
if process.burst_time > quantum:
current_time += quantum
process.burst_time -= quantum
queue.append(process)
else:
current_time += process.burst_time
process.waiting_time = current_time - process.arrival_time - process.burst_time
process.turnaround_time = process.waiting_time + process.burst_time