Repository navigation
Expand file tree
/
Copy pathOperating_System_Scheduling.py
More file actions
179 lines (138 loc) · 5.55 KB
/
Copy pathOperating_System_Scheduling.py
File metadata and controls
179 lines (138 loc) · 5.55 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
#ID: 011 223 0876
#Name: Gazi Rafat Al Kafi
num_of_process = int(input("Enter the number of process:"))
arrival_time = []
burst_time = []
time_quantum = 0
current_time = 0
total_tat = 0
total_wt = 0
completed = 0
done = [False] * num_of_process
print("---------------------------------------------------------------------------")
for i in range(num_of_process):
at = int(input("Enter arival time of process " + str(i+1) + ": "))
bt = int(input("Enter the brust time of process" + str(i+1) + ": "))
arrival_time.append(at)
burst_time.append(bt)
processes = list(zip(range(1, num_of_process + 1), arrival_time, burst_time))
print("---------------------------------------------------------------------------")
print("-----------------------------Menu------------------------------------------")
print("---------------------------------------------------------------------------")
print("Select which scheduling you wanna use: ")
print("1. FCFS")
print("2. SJF (Non-Preemptive)")
print("3. SRTF (Preemptive SJF)")
print("4. Round robin")
print("---------------------------------------------------------------------------")
select = int(input())
if (select == 1):
print("FCFS selected")
if (select == 2):
print("SJF selected")
if (select == 3):
print("SRTF selected")
if (select == 4):
print("Robin Round selected")
time_quantum = int(input("Enter Time Quantum for Robin Round: "))
print("---------------------------------------------------------------------------")
if(select == 1):
processes.sort(key=lambda x: x[1])
print("Pid\tAt\tBt\tCt\ttat\twt")
for pid, at, bt in processes:
if current_time < at:
current_time = at
ct = current_time + bt
tat = ct - at
wt = tat - bt
current_time = ct
total_tat += tat
total_wt += wt
print(f"{pid}\t{at}\t{bt}\t{ct}\t{tat}\t{wt}")
print(f"\nAverage Turnaround Time: {total_tat / num_of_process:.2f}")
print(f"Average Waiting Time: {total_wt / num_of_process:.2f}")
print("---------------------------------------------------------------------------")
if (select==2):
print("PID\tAT\tBT\tCT\tTAT\tWT")
while completed < num_of_process:
shortest = -1
for i in range(num_of_process):
if arrival_time[i] <= current_time and not done[i]:
if shortest == -1 or burst_time[i] < burst_time[shortest]:
shortest = i
if shortest == -1:
current_time += 1
continue
current_time += burst_time[shortest]
ct = current_time
tat = ct - arrival_time[shortest]
wt = tat - burst_time[shortest]
done[shortest] = True
completed += 1
total_tat += tat
total_wt += wt
print(f"{shortest+1}\t{arrival_time[shortest]}\t{burst_time[shortest]}\t{ct}\t{tat}\t{wt}")
print(f"\nAverage Turnaround Time: {total_tat / num_of_process:.2f}")
print(f"Average Waiting Time: {total_wt / num_of_process:.2f}")
print("---------------------------------------------------------------------------")
elif (select == 3):
print("PID\tAT\tBT\tCT\tTAT\tWT")
remaining_bt = burst_time.copy()
while completed < num_of_process:
shortest = -1
for i in range(num_of_process):
if arrival_time[i] <= current_time and not done[i]:
if shortest == -1 or remaining_bt[i] < remaining_bt[shortest]:
shortest = i
if shortest == -1:
current_time += 1
continue
remaining_bt[shortest] -= 1
current_time += 1
if remaining_bt[shortest] == 0:
done[shortest] = True
completed += 1
ct = current_time
tat = ct - arrival_time[shortest]
wt = tat - burst_time[shortest]
total_tat += tat
total_wt += wt
print(f"{shortest+1}\t{arrival_time[shortest]}\t{burst_time[shortest]}\t{ct}\t{tat}\t{wt}")
print(f"\nAverage Turnaround Time: {total_tat / num_of_process:.2f}")
print(f"Average Waiting Time: {total_wt / num_of_process:.2f}")
print("---------------------------------------------------------------------------")
elif select == 4:
print("PID\tAT\tBT\tCT\tTAT\tWT")
remaining_bt = burst_time.copy()
ct = [0] * num_of_process
ready_queue = []
visited = [False] * num_of_process
while completed < num_of_process:
for i in range(num_of_process):
if arrival_time[i] <= current_time and not visited[i]:
ready_queue.append(i)
visited[i] = True
if not ready_queue:
current_time += 1
continue
i = ready_queue.pop(0)
exec_time = min(time_quantum, remaining_bt[i])
remaining_bt[i] -= exec_time
current_time += exec_time
for j in range(num_of_process):
if arrival_time[j] <= current_time and not visited[j]:
ready_queue.append(j)
visited[j] = True
if remaining_bt[i] == 0:
ct[i] = current_time
tat = ct[i] - arrival_time[i]
wt = tat - burst_time[i]
total_tat += tat
total_wt += wt
completed += 1
print(f"{i+1}\t{arrival_time[i]}\t{burst_time[i]}\t{ct[i]}\t{tat}\t{wt}")
else:
ready_queue.append(i)
print(f"\nAverage Turnaround Time: {total_tat/num_of_process:.2f}")
print(f"Average Waiting Time: {total_wt/num_of_process:.2f}")
print("---------------------------------------------------------------------------")