Program for Shortest Job First ( SJF ) scheduling | Set 2 ( Preemptive ) | C # program to implement Shortest Remaining Time First Shortest Remaining Time First ( SRTF ) ; public int pid ; Process ID public int bt ; Burst Time public int art ; Arrival Time ; Method 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 ; Method to calculate turn around time ; calculating turnaround time by adding bt [ i ] + wt [ i ] ; Method to calculate average time ; Function to find waiting time of all processes ; Function to find turn around time for all processes ; Display processes along with all details ; Calculate total waiting time and total turnaround time ; Driver Method
using System ; public class Process { public Process ( int pid , int bt , int art ) { this . pid = pid ; this . bt = bt ; this . art = art ; } } public class GFG { static void findWaitingTime ( Process [ ] proc , int n , int [ ] wt ) { int [ ] rt = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) rt [ i ] = proc [ i ] . bt ; int complete = 0 , t = 0 , minm = int . MaxValue ; int shortest = 0 , finish_time ; bool check = false ; while ( complete != n ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( proc [ j ] . art <= t ) && ( rt [ j ] < minm ) && rt [ j ] > 0 ) { minm = rt [ j ] ; shortest = j ; check = true ; } } if ( check == false ) { t ++ ; continue ; } rt [ shortest ] -- ; minm = rt [ shortest ] ; if ( minm == 0 ) minm = int . MaxValue ; if ( rt [ shortest ] == 0 ) { complete ++ ; check = false ; finish_time = t + 1 ; wt [ shortest ] = finish_time - proc [ shortest ] . bt - proc [ shortest ] . art ; if ( wt [ shortest ] < 0 ) wt [ shortest ] = 0 ; } t ++ ; } } static void findTurnAroundTime ( Process [ ] proc , int n , int [ ] wt , int [ ] tat ) { for ( int i = 0 ; i < n ; i ++ ) tat [ i ] = proc [ i ] . bt + wt [ i ] ; } static void findavgTime ( Process [ ] proc , int n ) { int [ ] wt = new int [ n ] ; int [ ] tat = new int [ n ] ; int total_wt = 0 , total_tat = 0 ; findWaitingTime ( proc , n , wt ) ; findTurnAroundTime ( proc , n , wt , tat ) ; Console . WriteLine ( " Processes ▁ " + " ▁ Burst ▁ time ▁ " + " ▁ Waiting ▁ time ▁ " + " ▁ Turn ▁ around ▁ time " ) ; for ( int i = 0 ; i < n ; i ++ ) { total_wt = total_wt + wt [ i ] ; total_tat = total_tat + tat [ i ] ; Console . WriteLine ( " ▁ " + proc [ i ] . pid + " TABSYMBOL TABSYMBOL " + proc [ i ] . bt + " TABSYMBOL TABSYMBOL ▁ " + wt [ i ] + " TABSYMBOL TABSYMBOL " + tat [ i ] ) ; } Console . WriteLine ( " Average ▁ waiting ▁ time ▁ = ▁ " + ( float ) total_wt / ( float ) n ) ; Console . WriteLine ( " Average ▁ turn ▁ around ▁ time ▁ = ▁ " + ( float ) total_tat / ( float ) n ) ; } public static void Main ( String [ ] args ) { Process [ ] proc = { new Process ( 1 , 6 , 1 ) , new Process ( 2 , 8 , 1 ) , new Process ( 3 , 7 , 2 ) , new Process ( 4 , 3 , 3 ) } ; findavgTime ( proc , proc . Length ) ; } }