0% found this document useful (0 votes)
7 views1 page

SJF Py

The document presents a Shortest Job First (SJF) scheduling algorithm implemented in Python. It calculates the waiting time and turnaround time for a list of processes based on their burst times, and outputs the results along with average waiting and turnaround times. The example provided uses four processes with specific burst times to demonstrate the algorithm's functionality.
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)
7 views1 page

SJF Py

The document presents a Shortest Job First (SJF) scheduling algorithm implemented in Python. It calculates the waiting time and turnaround time for a list of processes based on their burst times, and outputs the results along with average waiting and turnaround times. The example provided uses four processes with specific burst times to demonstrate the algorithm's functionality.
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
You are on page 1/ 1

# SJF Scheduling Algorithm

def sjf(processes, burst_time):


n = len(processes)
waiting_time = [0] * n
turnaround_time = [0] * n

# Sort processes by burst time


processes = [x for _, x in sorted(zip(burst_time, processes))]
burst_time.sort()

# Calculate waiting time


for i in range(1, n):
waiting_time[i] = waiting_time[i - 1] + burst_time[i - 1]

# Calculate turnaround time


for i in range(n):
turnaround_time[i] = waiting_time[i] + burst_time[i]

print("Process\tBurst Time\tWaiting Time\tTurnaround Time")


for i in range(n):
print(f"P{processes[i]}\t\t{burst_time[i]}\t\t{waiting_time[i]}\t\
t{turnaround_time[i]}")

avg_waiting_time = sum(waiting_time) / n
avg_turnaround_time = sum(turnaround_time) / n
print(f"\nAverage Waiting Time: {avg_waiting_time}")
print(f"Average Turnaround Time: {avg_turnaround_time}")

# Input: List of processes and their burst times


processes = [1, 2, 3, 4]
burst_time = [8, 4, 2, 1]
sjf(processes, burst_time)

You might also like