fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. char process[5][3] = {"P1", "P2", "P3", "P4", "P5"};
  5. int arrival[5] = {3, 5, 0, 5, 4};
  6. int burst[5] = {4, 3, 2, 1, 3};
  7. int waiting[5], turnaround[5];
  8. int start[5], finish[5];
  9. int n = 5, i, j, temp;
  10. float totalWT = 0, totalTAT = 0;
  11.  
  12. // Sort according to arrival time
  13. for (i = 0; i < n - 1; i++) {
  14. for (j = i + 1; j < n; j++) {
  15. if (arrival[i] > arrival[j]) {
  16. temp = arrival[i]; arrival[i] = arrival[j]; arrival[j] = temp;
  17. temp = burst[i]; burst[i] = burst[j]; burst[j] = temp;
  18. char t[3];
  19. sprintf(t, "%s", process[i]);
  20. sprintf(process[i], "%s", process[j]);
  21. sprintf(process[j], "%s", t);
  22. }
  23. }
  24. }
  25.  
  26. int currentTime = 0;
  27.  
  28. for (i = 0; i < n; i++) {
  29. if (currentTime < arrival[i])
  30. currentTime = arrival[i];
  31.  
  32. start[i] = currentTime;
  33. finish[i] = start[i] + burst[i];
  34. turnaround[i] = finish[i] - arrival[i];
  35. waiting[i] = turnaround[i] - burst[i];
  36.  
  37. totalWT += waiting[i];
  38. totalTAT += turnaround[i];
  39.  
  40. currentTime = finish[i];
  41. }
  42.  
  43. printf("\n--- FCFS Scheduling ---\n");
  44. printf("Process\tAT\tBT\tWT\tTAT\n");
  45. for (i = 0; i < n; i++) {
  46. printf("%s\t%d\t%d\t%d\t%d\n",
  47. process[i], arrival[i], burst[i], waiting[i], turnaround[i]);
  48. }
  49.  
  50. printf("\nAverage Waiting Time = %.2f", totalWT / n);
  51. printf("\nAverage Turnaround Time = %.2f\n", totalTAT / n);
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
--- FCFS Scheduling ---
Process	AT	BT	WT	TAT
P3	0	2	0	2
P1	3	4	0	4
P5	4	3	3	6
P4	5	1	5	6
P2	5	3	6	9

Average Waiting Time = 2.80
Average Turnaround Time = 5.40