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

P	AT	BT	start time 	 end time	 WT	 TAT

p[4]	 0	 3	 0	 3	 0	 3
p[1]	 2	 6	 3	 9	 1	 7
p[2]	 5	 2	 9	 11	 4	 6
p[5]	 4	 4	 11	 15	 7	 11
p[3]	 1	 8	 15	 23	 14	 22
Average Turnaround Time = 9.80
Average Waiting Time = 5.20