|
| 1 | +// Java program for implementation of FCFS |
| 2 | +// scheduling |
| 3 | + |
| 4 | +import java.text.ParseException; |
| 5 | + |
| 6 | +class FCFS { |
| 7 | + |
| 8 | + // Function to find the waiting time for all |
| 9 | + // processes |
| 10 | + static void findWaitingTime(int processes[], int n, |
| 11 | + int bt[], int wt[]) { |
| 12 | + // waiting time for first process is 0 |
| 13 | + wt[0] = 0; |
| 14 | + |
| 15 | + // calculating waiting time |
| 16 | + for (int i = 1; i < n; i++) { |
| 17 | + wt[i] = bt[i - 1] + wt[i - 1]; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + // Function to calculate turn around time |
| 22 | + static void findTurnAroundTime(int processes[], int n, |
| 23 | + int bt[], int wt[], int tat[]) { |
| 24 | + // calculating turnaround time by adding |
| 25 | + // bt[i] + wt[i] |
| 26 | + for (int i = 0; i < n; i++) { |
| 27 | + tat[i] = bt[i] + wt[i]; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + //Function to calculate average time |
| 32 | + static void findavgTime(int processes[], int n, int bt[]) { |
| 33 | + int wt[] = new int[n], tat[] = new int[n]; |
| 34 | + int total_wt = 0, total_tat = 0; |
| 35 | + |
| 36 | + //Function to find waiting time of all processes |
| 37 | + findWaitingTime(processes, n, bt, wt); |
| 38 | + |
| 39 | + //Function to find turn around time for all processes |
| 40 | + findTurnAroundTime(processes, n, bt, wt, tat); |
| 41 | + |
| 42 | + //Display processes along with all details |
| 43 | + System.out.printf("Processes Burst time Waiting" |
| 44 | + +" time Turn around time\n"); |
| 45 | + |
| 46 | + // Calculate total waiting time and total turn |
| 47 | + // around time |
| 48 | + for (int i = 0; i < n; i++) { |
| 49 | + total_wt = total_wt + wt[i]; |
| 50 | + total_tat = total_tat + tat[i]; |
| 51 | + System.out.printf(" %d ", (i + 1)); |
| 52 | + System.out.printf(" %d ", bt[i]); |
| 53 | + System.out.printf(" %d", wt[i]); |
| 54 | + System.out.printf(" %d\n", tat[i]); |
| 55 | + } |
| 56 | + float s = (float)total_wt /(float) n; |
| 57 | + int t = total_tat / n; |
| 58 | + System.out.printf("Average waiting time = %f", s); |
| 59 | + System.out.printf("\n"); |
| 60 | + System.out.printf("Average turn around time = %d ", t); |
| 61 | + } |
| 62 | + |
| 63 | + // Driver code |
| 64 | + public static void main(String[] args) throws ParseException { |
| 65 | + //process id's |
| 66 | + int processes[] = {1, 2, 3}; |
| 67 | + int n = processes.length; |
| 68 | + |
| 69 | + //Burst time of all processes |
| 70 | + int burst_time[] = {10, 5, 8}; |
| 71 | + |
| 72 | + findavgTime(processes, n, burst_time); |
| 73 | + |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | + |
0 commit comments