Repository navigation
Expand file tree
/
Copy pathPractical10.py
More file actions
207 lines (136 loc) · 4.77 KB
/
Copy pathPractical10.py
File metadata and controls
207 lines (136 loc) · 4.77 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
# Implement Round Robin Scheduling with configurable Time Quantum
import random
from collections import deque
# Class used to store information about each process
class Process:
# Constructor of the Process class
def __init__(self, pid, burst_time, arrival_time=0):
self.pid = pid
self.burst_time = burst_time
self.arrival_time = arrival_time
# Initially, remaining time is equal to burst time
self.remaining_time = burst_time
# These values will be calculated during execution
self.completion_time = None
self.waiting_time = 0
self.turnaround_time = 0
# Initially, process has not been executed
self.last_execution_time = arrival_time
# Function to implement Round Robin Scheduling
def round_robin(processes, time_quantum):
# Current CPU time
time = 0
# Queue containing processes that are ready to execute
ready_queue = deque()
# List used to store Gantt Chart information
gantt_chart = []
# Sort processes according to their arrival time
processes.sort(key=lambda p: p.arrival_time)
# Make a copy of the process list
processes_left = processes.copy()
# Continue until all processes are completed
# and the ready queue becomes empty
while processes_left or ready_queue:
# Check which processes have arrived
for p in processes_left[:]:
if p.arrival_time <= time:
# Add arrived process to the ready queue
ready_queue.append(p)
# Remove it from the waiting list
processes_left.remove(p)
# If no process is ready, move the CPU time forward
if not ready_queue:
time += 0.1
continue
# Take the first process from the ready queue
current = ready_queue.popleft()
# Store the starting time of this process
start_time = time
# Execute the process for one time quantum
# or until the process finishes
exec_time = min(current.remaining_time, time_quantum)
# Increase the current time
time += exec_time
# Decrease the remaining execution time
current.remaining_time -= exec_time
# Add process execution information to Gantt Chart
gantt_chart.append((current.pid, start_time, time))
# Calculate waiting time
current.waiting_time += start_time - current.last_execution_time
# Update the last execution time
current.last_execution_time = time
# If process still has remaining time,
# put it back into the ready queue
if current.remaining_time > 0:
ready_queue.append(current)
else:
# Process has completed
current.completion_time = time
# Calculate Turnaround Time
current.turnaround_time = (
current.completion_time - current.arrival_time
)
# Return the Gantt Chart and completed processes
return gantt_chart, processes
# Main program
if __name__ == "__main__":
# Number of processes
num_processes = 5
# Time Quantum for Round Robin
time_quantum = 1.0
# Create 5 processes with random burst time
# and random arrival time
processes = [
Process(
i,
burst_time=random.uniform(1, 5),
arrival_time=random.uniform(0, 3)
)
for i in range(num_processes)
]
# Display process information
print("Processes:")
for p in processes:
print(
f"P{p.pid}: "
f"arrival={p.arrival_time:.2f}, "
f"burst={p.burst_time:.2f}"
)
# Run Round Robin Scheduling
gantt_chart, finished_processes = round_robin(
processes,
time_quantum
)
# Display Gantt Chart
print("\nGantt Chart:")
for pid, start, end in gantt_chart:
print(
f"| P{pid} ({start:.1f}-{end:.1f}) ",
end=""
)
print("|")
# Variables used to calculate average times
total_wt = 0
total_tat = 0
# Display details of each process
print("\nProcess Details:")
for p in finished_processes:
# Add waiting time to total waiting time
total_wt += p.waiting_time
# Add turnaround time to total turnaround time
total_tat += p.turnaround_time
print(
f"P{p.pid}: "
f"WT={p.waiting_time:.2f}, "
f"TAT={p.turnaround_time:.2f}"
)
# Calculate and display average waiting time
print(
f"\nAverage Waiting Time: "
f"{total_wt / num_processes:.2f}"
)
# Calculate and display average turnaround time
print(
f"Average Turnaround Time: "
f"{total_tat / num_processes:.2f}"
)