fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int n, tq;
  5. int at[10], bt[10], rt[10], ct[10];
  6. int tat[10], wt[10];
  7. int time = 0, completed = 0;
  8. float avg_tat = 0, avg_wt = 0;
  9.  
  10. printf("Enter number of processes: ");
  11. scanf("%d", &n);
  12.  
  13. printf("Enter Arrival Time and Burst Time:\n");
  14. for (int i = 0; i < n; i++) {
  15. printf("P%d: ", i + 1);
  16. scanf("%d %d", &at[i], &bt[i]);
  17. rt[i] = bt[i];
  18. }
  19.  
  20. printf("Enter Time Quantum: ");
  21. scanf("%d", &tq);
  22.  
  23. printf("\nGantt Chart:\n");
  24.  
  25. while (completed < n) {
  26. int executed = 0;
  27.  
  28. for (int i = 0; i < n; i++) {
  29. if (at[i] <= time && rt[i] > 0) {
  30. executed = 1;
  31.  
  32. printf("| P%d (%d-%d) ", i + 1, time,
  33. time + (rt[i] > tq ? tq : rt[i]));
  34.  
  35. if (rt[i] > tq) {
  36. time += tq;
  37. rt[i] -= tq;
  38. } else {
  39. time += rt[i];
  40. rt[i] = 0;
  41. ct[i] = time;
  42. completed++;
  43. }
  44. }
  45. }
  46.  
  47. if (!executed)
  48. time++;
  49. }
  50.  
  51. printf("|\n");
  52.  
  53. printf("Process AT BT CT TAT WT\n");
  54. for (int i = 0; i < n; i++) {
  55. tat[i] = ct[i] - at[i];
  56. wt[i] = tat[i] - bt[i];
  57.  
  58. avg_tat += tat[i];
  59. avg_wt += wt[i];
  60.  
  61. printf("P%d %d %d %d %d %d\n",
  62. i + 1, at[i], bt[i], ct[i], tat[i], wt[i]);
  63. }
  64.  
  65. printf("Average Turnaround Time = %.2f\n", avg_tat / n);
  66. printf("Average Waiting Time = %.2f\n", avg_wt / n);
  67.  
  68. return 0;
  69. }
  70.  
  71.  
  72.  
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
Enter number of processes: Enter Arrival Time and Burst Time:
Enter Time Quantum: 
Gantt Chart:
|
Process AT BT CT TAT WT
Average Turnaround Time = -nan
Average Waiting Time = -nan