human
Program for Shortest Job First ( SJF ) scheduling | Set 2 ( Preemptive ) | Function to find the waiting time for all processes.
Copy the burst time into rt [ ].
Process until all processes gets completed.
Find process with minimum remaining time among the processes that arrives till the current time `.
Reduce remaining time by one.
Update minimum.
If a process gets completely executed.
Increment complete.
Find finish time of current process.
Calculate waiting time.
Increment time.
Function to calculate turn around time.
Calculating turnaround time.
Function to calculate average waiting and turn - around times ..
Function to find waiting time of all processes.
Function to find turn around time for all processes.
Display processes along with all details.
Driver code.
Process id 's"
How can the above be solved in Python?
assistant
#Here is code to program for Shortest Job First ( SJF ) scheduling and the other steps required.
 def findWaitingTime ( processes , n , wt ) :
	rt = [ 0 ] * n
	for i in range ( n ) :
		rt [ i ] = processes [ i ] [ 1 ]
	complete = 0
	t = 0
	minm = 999999999
	short = 0
	check = False
	while ( complete != n ) :
		for j in range ( n ) :
			if ( ( processes [ j ] [ 2 ] <= t ) and ( rt [ j ] < minm ) and rt [ j ] > 0 ) :
				minm = rt [ j ]
				short = j
				check = True
		if ( check == False ) :
			t += 1
			continue
		rt [ short ] -= 1
		minm = rt [ short ]
		if ( minm == 0 ) :
			minm = 999999999
		if ( rt [ short ] == 0 ) :
			complete += 1
			check = False
			fint = t + 1
			wt [ short ] = ( fint - proc [ short ] [ 1 ] - proc [ short ] [ 2 ] )
			if ( wt [ short ] < 0 ) :
				wt [ short ] = 0
		t += 1
def findTurnAroundTime ( processes , n , wt , tat ) :
	for i in range ( n ) :
		tat [ i ] = processes [ i ] [ 1 ] + wt [ i ]
def findavgTime ( processes , n ) :
	wt = [ 0 ] * n
	tat = [ 0 ] * n
	findWaitingTime ( processes , n , wt )
	findTurnAroundTime ( processes , n , wt , tat )
	print ( " Processes ▁ Burst ▁ Time ▁ Waiting " , " Time ▁ Turn - Around ▁ Time " )
	total_wt = 0
	total_tat = 0
	for i in range ( n ) :
		total_wt = total_wt + wt [ i ]
		total_tat = total_tat + tat [ i ]
		print ( " ▁ " , processes [ i ] [ 0 ] , " 	 	 " , processes [ i ] [ 1 ] , " 	 	 " , wt [ i ] , " 	 	 " , tat [ i ] )
	print ( " Average waiting time = % .5 f   " % ( total_wt / n ) )
	print ( " Average ▁ turn ▁ around ▁ time ▁ = ▁ " , total_tat / n )
if __name__ == " _ _ main _ _ " :
	proc = [ [ 1 , 6 , 1 ] , [ 2 , 8 , 1 ] , [ 3 , 7 , 2 ] , [ 4 , 3 , 3 ] ]
	n = 4
	findavgTime ( proc , n )