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"\n Enter 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 ("\n Process\t Arrival Time\t Burst Time\t Priority 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"\n Timeframe { 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 ("\n Process\t Waiting Time\t Turnaround 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
0 commit comments