Scheduling Codes
Scheduling Codes
FIFO
Input:
# Input the number of processes
n = int(input("Enter number of processes: "))
P1 0 2 2 2 0
P2 4 3 7 3 0
P3 6 1 8 2 1
Average turnaround time: 2.3333333333333335
Average waiting time: 0.3333333333333333
PS C:\Users\choud\OneDrive\Desktop\coos>
SJF
Input:
def sjf_scheduling(processes):
# n is the number of processes
n = len(processes)
# Loop through each process to get its arrival and burst times
from the user
for i in range(num_processes):
arrival_time = int(input(f"Enter arrival time for Process {i +
1}: ")) # Get arrival time
burst_time = int(input(f"Enter burst time for Process {i + 1}:
")) # Get burst time
processes.append((i + 1, arrival_time, burst_time)) # Store
process as a tuple: (process ID, arrival time, burst time)
int main() {
int n, tq, sum_tat = 0, sum_wt = 0, remain;
string processNames[n];
int AT[n], BT[n], RT[n], CT[n], TAT[n], WT[n];
int i = 0;
while (remain > 0) {
// Ensure the process is ready (has arrived)
if (RT[i] > 0 && AT[i] <= time) {
// If the process can finish within the time quantum
if (RT[i] <= tq && RT[i] > 0) {
time += RT[i]; // Increment time by remaining time
RT[i] = 0; // Process is done
temp = 1; // Mark the process as complete
}
// If the process needs more time than the quantum
else if (RT[i] > 0) {
RT[i] -= tq; // Deduct the quantum from remaining
time
time += tq; // Increment the time by the quantum
}
cout <<
"|-------------------------------------------------------------------------------
----------|\n";
cout << "Average Turnaround Time: " << avg_tat << "
seconds\n";
cout << "Average Waiting Time: " << avg_wt << " seconds\
n";
return 0;
}
Output:
PS C:\Users\choud\OneDrive\Desktop\coos> g++ -o result
rr.cpp
PS C:\Users\choud\OneDrive\Desktop\coos> ./result.exe
Enter the number of processes: 5
Enter the value of time quantum: 3
Enter the arrival time of P1: 0
Enter the burst time of P1: 5
Enter the arrival time of P2: 6
Enter the burst time of P2: 3
Enter the arrival time of P3: 9
Enter the burst time of P3: 8
Enter the arrival time of P4: 11
Enter the burst time of P4: 4
Enter the arrival time of P5: 12
Enter the burst time of P5: 4