fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int p[10], at[10], bt[10], ct[10], tat[10], wt[10];
  5. int i, j, temp, n;
  6. float awt = 0, atat = 0;
  7.  
  8. printf("Enter number of processes:\n");
  9. scanf("%d", &n);
  10.  
  11. printf("Enter process IDs:\n");
  12. for (i = 0; i < n; i++) {
  13. scanf("%d", &p[i]);
  14. }
  15.  
  16. printf("Enter arrival time of each process:\n");
  17. for (i = 0; i < n; i++) {
  18. scanf("%d", &at[i]);
  19. }
  20.  
  21. printf("Enter burst time of each process:\n");
  22. for (i = 0; i < n; i++) {
  23. scanf("%d", &bt[i]);
  24. }
  25.  
  26. // Sort processes by arrival time using bubble sort
  27. for (i = 0; i < n - 1; i++) {
  28. for (j = 0; j < n - i - 1; j++) {
  29. if (at[j] > at[j + 1]) {
  30. // Swap arrival times
  31. temp = at[j];
  32. at[j] = at[j + 1];
  33. at[j + 1] = temp;
  34.  
  35. // Swap burst times
  36. temp = bt[j];
  37. bt[j] = bt[j + 1];
  38. bt[j + 1] = temp;
  39.  
  40. // Swap process IDs
  41. temp = p[j];
  42. p[j] = p[j + 1];
  43. p[j + 1] = temp;
  44. }
  45. }
  46. }
  47.  
  48. // Calculate completion time
  49. ct[0] = at[0] + bt[0];
  50. for (i = 1; i < n; i++) {
  51. if (ct[i - 1] < at[i]) {
  52. ct[i] = at[i] + bt[i]; // CPU is idle
  53. } else {
  54. ct[i] = ct[i - 1] + bt[i]; // CPU is busy
  55. }
  56. }
  57.  
  58. // Calculate TAT and WT
  59. for (i = 0; i < n; i++) {
  60. tat[i] = ct[i] - at[i];
  61. wt[i] = tat[i] - bt[i];
  62. atat += tat[i];
  63. awt += wt[i];
  64. }
  65.  
  66. atat /= n;
  67. awt /= n;
  68.  
  69. // Output
  70. printf("\nP\tAT\tBT\tCT\tTAT\tWT\n");
  71. for (i = 0; i < n; i++) {
  72. printf("P%d\t%d\t%d\t%d\t%d\t%d\n", p[i], at[i], bt[i], ct[i], tat[i], wt[i]);
  73. }
  74.  
  75. printf("\nAverage Turnaround Time = %.2f", atat);
  76. printf("\nAverage Waiting Time = %.2f\n", awt);
  77.  
  78. return 0;
  79. }
  80.  
Success #stdin #stdout 0s 5328KB
stdin
3
1 2 3
0 0 0
24 3 3
stdout
Enter number of processes:
Enter process IDs:
Enter arrival time of each process:
Enter burst time of each process:

P	AT	BT	CT	TAT	WT
P1	0	24	24	24	0
P2	0	3	27	27	24
P3	0	3	30	30	27

Average Turnaround Time = 27.00
Average Waiting Time = 17.00