0% found this document useful (0 votes)
14 views3 pages

CPU Scheduling Algorithms Simulation

The document outlines a program that simulates various CPU scheduling algorithms including FCFS, Round Robin, SJF (Preemptive), and Priority (Non-Preemptive). It provides implementations for FCFS and Round Robin algorithms, detailing functions for calculating waiting time, turnaround time, and average times. Sample outputs for both algorithms are included, demonstrating their respective performance metrics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

CPU Scheduling Algorithms Simulation

The document outlines a program that simulates various CPU scheduling algorithms including FCFS, Round Robin, SJF (Preemptive), and Priority (Non-Preemptive). It provides implementations for FCFS and Round Robin algorithms, detailing functions for calculating waiting time, turnaround time, and average times. Sample outputs for both algorithms are included, demonstrating their respective performance metrics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Write a program to simulate CPU Scheduling Algorithms: FCFS, SJF (Preemptive),

Priority
(Non-Preemptive) and Round Robin (Preemptive).

-----------------------------------------------------------------------------------
--------------------------------------
1> FCFS

def findWaitingTime(processes, n, bt, wt):


wt[0] = 0
for i in range(1, n):
wt[i] = bt[i - 1] + wt[i - 1]

def findTurnAroundTime(processes, n, bt, wt, tat):


for i in range(n):
tat[i] = bt[i] + wt[i]

def findavgTime(processes, n, bt):


wt = [0] * n
tat = [0] * n
total_wt = 0
total_tat = 0

findWaitingTime(processes, n, bt, wt)


findTurnAroundTime(processes, n, bt, wt, tat)

print("Processes Burst time Waiting time Turn around time")


for i in range(n):
total_wt = total_wt + wt[i]
total_tat = total_tat + tat[i]
print(" " + str(i + 1) + "\t\t" + str(bt[i]) + "\t " +
str(wt[i]) + "\t\t " + str(tat[i]))

print("Average waiting time = " + str(total_wt / n))


print("Average turn around time = " + str(total_tat / n))

if __name__ == "__main__":
processes = [1, 2, 3]
n = len(processes)
burst_time = [10, 5, 8]
findavgTime(processes, n, burst_time)

OUTPUT:

Processes Burst time Waiting time Turn around time


1 10 0 10
2 5 10 15
3 8 15 23
Average waiting time = 8.333333333333334
Average turn around time = 16.0

-----------------------------------------------------------------------------------
--------------------------------------

2> Round Robin


def findWaitingTime(processes, n, bt, wt, quantum):
rem_bt = [0] * n
for i in range(n):
rem_bt[i] = bt[i]
t = 0

while True:
done = True
for i in range(n):
if rem_bt[i] > 0:
done = False
if rem_bt[i] > quantum:
t += quantum
rem_bt[i] -= quantum
else:
t = t + rem_bt[i]
wt[i] = t - bt[i]
rem_bt[i] = 0
if done:
break

def findTurnAroundTime(processes, n, bt, wt, tat):


for i in range(n):
tat[i] = bt[i] + wt[i]

def findavgTime(processes, n, bt, quantum):


wt = [0] * n
tat = [0] * n

findWaitingTime(processes, n, bt, wt, quantum)


findTurnAroundTime(processes, n, bt, wt, tat)

print("{:<12} {:<12} {:<15} {:<15}".format("Process", "Burst Time", "Waiting


Time", "Turnaround Time"))

total_wt = 0
total_tat = 0

for i in range(n):
total_wt += wt[i]
total_tat += tat[i]
print("{:<12} {:<12} {:<15} {:<15}".format(processes[i], bt[i], wt[i],
tat[i]))

print("\nAverage waiting time = {:.2f}".format(total_wt / n))


print("Average turnaround time = {:.2f}".format(total_tat / n))

if __name__ == "__main__":
proc = [1, 2, 3]
n = 3
burst_time = [10, 5, 8]
quantum = 2
findavgTime(proc, n, burst_time, quantum)
OUTPUT:

Process Burst Time Waiting Time Turnaround Time


1 10 13 23
2 5 10 15
3 8 13 21

Average waiting time = 12.00


Average turnaround time = 19.67

-----------------------------------------------------------------------------------
--------------------------------------

You might also like