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
def findWaitingTime ( processes , n , wt ) : NEW_LINE INDENT rt = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT rt [ i ] = processes [ i ] [ 1 ] NEW_LINE DEDENT complete = 0 NEW_LINE t = 0 NEW_LINE minm = 999999999 NEW_LINE short = 0 NEW_LINE check = False NEW_LINE while ( complete != n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( ( processes [ j ] [ 2 ] <= t ) and ( rt [ j ] < minm ) and rt [ j ] > 0 ) : NEW_LINE INDENT minm = rt [ j ] NEW_LINE short = j NEW_LINE check = True NEW_LINE DEDENT DEDENT if ( check == False ) : NEW_LINE INDENT t += 1 NEW_LINE continue NEW_LINE DEDENT rt [ short ] -= 1 NEW_LINE minm = rt [ short ] NEW_LINE if ( minm == 0 ) : NEW_LINE INDENT minm = 999999999 NEW_LINE DEDENT if ( rt [ short ] == 0 ) : NEW_LINE INDENT complete += 1 NEW_LINE check = False NEW_LINE fint = t + 1 NEW_LINE wt [ short ] = ( fint - proc [ short ] [ 1 ] - proc [ short ] [ 2 ] ) NEW_LINE if ( wt [ short ] < 0 ) : NEW_LINE INDENT wt [ short ] = 0 NEW_LINE DEDENT DEDENT t += 1 NEW_LINE DEDENT DEDENT def findTurnAroundTime ( processes , n , wt , tat ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT tat [ i ] = processes [ i ] [ 1 ] + wt [ i ] NEW_LINE DEDENT DEDENT def findavgTime ( processes , n ) : NEW_LINE INDENT wt = [ 0 ] * n NEW_LINE tat = [ 0 ] * n NEW_LINE findWaitingTime ( processes , n , wt ) NEW_LINE findTurnAroundTime ( processes , n , wt , tat ) NEW_LINE print ( " Processes ▁ Burst ▁ Time ▁ Waiting " , " Time ▁ Turn - Around ▁ Time " ) NEW_LINE total_wt = 0 NEW_LINE total_tat = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT total_wt = total_wt + wt [ i ] NEW_LINE total_tat = total_tat + tat [ i ] NEW_LINE print ( " ▁ " , processes [ i ] [ 0 ] , " TABSYMBOL TABSYMBOL " , processes [ i ] [ 1 ] , " TABSYMBOL TABSYMBOL " , wt [ i ] , " TABSYMBOL TABSYMBOL " , tat [ i ] ) NEW_LINE DEDENT print ( " Average waiting time = % .5 f   " % ( total_wt / n ) ) NEW_LINE print ( " Average ▁ turn ▁ around ▁ time ▁ = ▁ " , total_tat / n ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT proc = [ [ 1 , 6 , 1 ] , [ 2 , 8 , 1 ] , [ 3 , 7 , 2 ] , [ 4 , 3 , 3 ] ] NEW_LINE n = 4 NEW_LINE findavgTime ( proc , n ) NEW_LINE DEDENT