Skip to content

Commit 27047aa

Browse files
committed
update
1 parent 56d4911 commit 27047aa

File tree

4 files changed

+187
-35
lines changed

4 files changed

+187
-35
lines changed

CPU_Scheduler/PreemptivePriorityScheduling.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,34 @@ def schedulingProcess(self, process_list): # Scheduling Algorithm
100100
# start_times.append(current_time)
101101
# end_times.append(current_time + 1)
102102

103-
for i, process in enumerate(completed_list):
104-
start_time = start_times[i]
105-
end_time = end_times[i]
106-
process_name = process[0]
107-
ax.barh(process_name, end_time - start_time, left=start_time, label=process_name)
103+
# for i, process in enumerate(completed_list):
104+
# start_time = start_times[i]
105+
# end_time = end_times[i]
106+
# process_name = process[0]
107+
# ax.barh(process_name, end_time - start_time, left=start_time, label=process_name)
108108

109109

110110
current_time += 1 # Increment Current Time Frame
111111

112112

113113

114-
fig, ax = plt.subplots()
114+
# fig, ax = plt.subplots()
115115

116116

117-
ax.set_xlabel('Time', fontsize=9)
118-
ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
119-
plt.grid(axis='x')
120-
plt.savefig("./GANTT_OUTPUT/GTChart.png", bbox_inches='tight', dpi=100)
121-
# Initialize Totals of TT and WT
122-
total_wt = 0
123-
total_tt = 0
117+
# ax.set_xlabel('Time', fontsize=9)
118+
# ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
119+
# plt.grid(axis='x')
120+
# plt.savefig("./GANTT_OUTPUT/GTChart.png", bbox_inches='tight', dpi=100)
121+
# # Initialize Totals of TT and WT
122+
# total_wt = 0
123+
# total_tt = 0
124124

125-
for process in process_list: # Loop to calculate WT and TT of each rocess
126-
turnaround_time = process[5] - process[1] # Calculate TT of process (Completion Time - Arrival Time)
127-
waiting_time = turnaround_time - process[2] # Calculate WT of process (Turnaround Time - Burst Time)
125+
# for process in process_list: # Loop to calculate WT and TT of each rocess
126+
# turnaround_time = process[5] - process[1] # Calculate TT of process (Completion Time - Arrival Time)
127+
# waiting_time = turnaround_time - process[2] # Calculate WT of process (Turnaround Time - Burst Time)
128128

129-
total_wt += waiting_time # Add TT of the process to the total
130-
total_tt += turnaround_time # Add WT of the process to the total
129+
# total_wt += waiting_time # Add TT of the process to the total
130+
# total_tt += turnaround_time # Add WT of the process to the total
131131

132132
# Compute Averages
133133
avg_wt = total_wt / len(self.process_list)
@@ -148,15 +148,10 @@ def schedulingProcess(self, process_list): # Scheduling Algorithm
148148

149149

150150
# # Main Machinery
151-
# proseso = PreemptivePriorityScheduling() # Create instance
152-
153-
# process_amount = int(input("Enter # of Processes: ")) # Input # of Processes
154-
155-
# user_or_random = input("User or Random Input? [U/R]: ") # Select if User or Random Values
156-
157-
# if user_or_random.lower() == "u":
158-
# proseso.inputUser(process_amount)
159-
# elif user_or_random.lower() == "r":
160-
# proseso.inputRandom(process_amount)
151+
def runner():
152+
proseso = _PreemptivePriorityScheduling() # Create instance
153+
process_list = proseso.inputRandom(10, 8)
154+
proseso.schedulingProcess(process_list) # Run Scheduling Algotihm
161155

162-
# proseso.schedulingProcess() # Run Scheduling Algotihm
156+
157+
runner()
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
from random import randint
2+
from copy import deepcopy
3+
4+
5+
6+
class PreemptivePriorityScheduling: # Class for simulating Preemptive Priority CPU Scheduling
7+
process_list = [] # List to Store the Processes [PID, AT, BT, RT, PL,CT]
8+
9+
def inputUser(self, no_of_processes): # USER INPUT
10+
for process_id in range(1, no_of_processes + 1): # Loop to enter user values for each process
11+
temporary = [] # Temporary list to store values
12+
13+
while True:
14+
arrival_time = int(input(f"\nEnter Arrival Time for P{process_id}: ")) # Arrival Time input
15+
16+
if [pr for pr in self.process_list if pr[1] == arrival_time]: # Arrival Time duplicate checker
17+
print("Duplicate")
18+
else:
19+
break
20+
21+
burst_time = int(input(f"Enter Burst Time for P{process_id}: ")) # Burst Time input
22+
23+
remaining_time = deepcopy(burst_time) # Remaining Time is used for the scheduling process to simulate the remaining burst time of the process
24+
25+
priority_level = int(input(f"Enter Priority Level for P{process_id}: ")) # Priority Level input
26+
27+
completion_time = 0 # Completion Time is the time that the process finished running
28+
29+
temporary = [f"P{process_id}", arrival_time, burst_time, remaining_time, priority_level, completion_time] # Storing input values inside a list
30+
31+
self.process_list.append(temporary) # Appending the list of values inside the list of processes
32+
33+
def inputRandom(self, no_of_processes): # RANDOM INPUT
34+
half = no_of_processes // 2
35+
randomizer_limit = no_of_processes + half # Randomizer limit is 150% of the Number of Processes
36+
37+
for process_id in range(1, no_of_processes + 1): # Loop to enter random values for each process
38+
temporary = []
39+
40+
while True:
41+
arrival_time = randint(0, randomizer_limit)
42+
43+
if [pr for pr in self.process_list if pr[1] == arrival_time]:
44+
pass
45+
else:
46+
break
47+
48+
burst_time = randint(1, randomizer_limit)
49+
50+
remaining_time = deepcopy(burst_time)
51+
52+
priority_level = randint(1, no_of_processes)
53+
54+
completion_time = 0
55+
56+
temporary = [f"P{process_id}", arrival_time, burst_time, remaining_time, priority_level, completion_time]
57+
58+
self.process_list.append(temporary)
59+
60+
61+
def schedulingProcess(self): # Scheduling Algorithm
62+
# Print input table
63+
print("\nProcess\tArrival Time\tBurst Time\tPriority Level")
64+
for process in self.process_list:
65+
print(f"{process[0]}\t{process[1]}\t\t{process[2]}\t\t{process[4]}")
66+
67+
current_time = 0 # Current Time Frame
68+
completed_list = [] # List for completed processes
69+
ready_queue = [] # Memory Queue
70+
# this is a 2 dimensional array in which [0] is the process name [1] service time [2] execution stop time
71+
cur_process_data = {}
72+
flag = True
73+
temp = None
74+
counter = 0
75+
while len(completed_list) < len(self.process_list): # Loop for the Algorithm
76+
for process in self.process_list:
77+
if process[1] <= current_time and process not in completed_list and process not in ready_queue: # Inserting newly arrived processes to the Memory Queue
78+
ready_queue.append(process)
79+
80+
print(f"\nTimeframe {current_time}")
81+
print(ready_queue)
82+
if not ready_queue: # Checking for idle time
83+
print(f"Running: None")
84+
current_time += 1
85+
continue # Skip rest of the algorithm if idle time
86+
87+
ready_queue.sort(key=lambda x: x[4]) # Sorting the ready queue based on Priority Level
88+
89+
current_process = ready_queue[0] # Set current process as process with highest level in memory queue
90+
91+
current_process[3] -= 1 # Decerement current process bust time
92+
if flag:
93+
cur_process_data[counter] = []
94+
cur_process_data[counter].append(current_process[0])
95+
cur_process_data[counter].append(current_time)
96+
temp = current_process[0]
97+
flag = False
98+
elif temp != current_process[0]:
99+
counter += 1
100+
cur_process_data[counter-1].append(current_time)
101+
flag = True
102+
103+
# Print Simulation
104+
print(f"Running: {current_process[0]}")
105+
print(f"Current Burst Time: {current_process[3]}")
106+
print(f"Priority Level: {current_process[4]}")
107+
108+
if current_process[3] == 0: # Check if process is Completed
109+
completed_list.append(current_process) # Append current process to completed list
110+
ready_queue.pop(0) # Remove current process from ready queue
111+
current_process[5] = current_time + 1 # Set completion time for current process
112+
113+
current_time += 1 # Increment Current Time Frame
114+
115+
116+
cur_process_data[counter].append(current_time)
117+
# Initialize Totals of TT and WT
118+
total_wt = 0
119+
total_tt = 0
120+
121+
for process in self.process_list: # Loop to calculate WT and TT of each rocess
122+
turnaround_time = process[5] - process[1] # Calculate TT of process (Completion Time - Arrival Time)
123+
waiting_time = turnaround_time - process[2] # Calculate WT of process (Turnaround Time - Burst Time)
124+
125+
total_wt += waiting_time # Add TT of the process to the total
126+
total_tt += turnaround_time # Add WT of the process to the total
127+
128+
# Compute Averages
129+
avg_wt = total_wt / len(self.process_list)
130+
avg_tt = total_tt / len(self.process_list)
131+
132+
# Print table results
133+
print("\nProcess\tWaiting Time\tTurnaround Time")
134+
for process in self.process_list:
135+
print(f"{process[0]}\t{process[5] - process[1] - process[2]}\t\t{process[5] - process[1]}")
136+
137+
print(f"Average Waiting Time: {avg_wt}")
138+
print(f"Average Turnaround Time: {avg_tt}")
139+
140+
141+
142+
print(f"\n\n {cur_process_data}")
143+
144+
145+
146+
# Main Machinery
147+
proseso = PreemptivePriorityScheduling() # Create instance
148+
149+
process_amount = int(input("Enter # of Processes: ")) # Input # of Processes
150+
151+
user_or_random = input("User or Random Input? [U/R]: ") # Select if User or Random Values
152+
153+
if user_or_random.lower() == "u":
154+
proseso.inputUser(process_amount)
155+
elif user_or_random.lower() == "r":
156+
proseso.inputRandom(process_amount)
157+
158+
proseso.schedulingProcess() # Run Scheduling Algotihm
236 Bytes
Binary file not shown.

CPU_Scheduler/main.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import customtkinter, math, os
22
from NonPreemptivePriorityScheduling import _NonPreemptivePriorityScheduling
33
from PreemptivePriorityScheduling import _PreemptivePriorityScheduling
4+
import PreemptivePriorityScheduling
45
from CTkTable import *
56
from matplotlib import pyplot as plt
67
from matplotlib.table import Table
@@ -10,7 +11,6 @@
1011
from PIL import Image, ImageTk
1112

1213
## TODO
13-
# Add some exit button on the top level window to destroy them
1414
# Complete the GANTT Chart for PPS
1515

1616
NP = 0
@@ -167,12 +167,11 @@ def startExecution(self):
167167
if self.AlgoMenu.get() == "Preemptive Priority Scheduling":
168168

169169
processList = self.PPS_Instance.inputRandom(int(self.Process_Input.get()), math.trunc(self.Burst_Time.get()))
170-
# completed_list = self.PPS_Instance.schedulingProcess(process_list=processList)
170+
completed_list = PreemptivePriorityScheduling.runner(processList)
171171
# WT = self.PPS_Instance.waitingTime
172172
# TT = self.PPS_Instance.turnaroundTime
173-
print(processList)
173+
print(completed_list)
174174

175-
176175
elif self.AlgoMenu.get() == "Non-Preemtive Priotity Scheduling":
177176
processList = self.NonPPS_Instance.Random_Input(int(self.Process_Input.get()), math.trunc(self.Burst_Time.get()))
178177
processTiming = self.NonPPS_Instance.Execute(processList)
@@ -182,11 +181,11 @@ def startExecution(self):
182181

183182
## The two lines below are used sa printing of process table
184183
global data
185-
186184
data = processList
185+
186+
## This Generates the Top Level window that shows the charts and Table
187187
self.toplev = ToplevelWindow(self.AlgoMenu.get())
188188

189-
## This Generates the Top Level window that shows the charts and Table
190189

191190

192191
class App(customtkinter.CTk):

0 commit comments

Comments
 (0)