Advertisement
Sri27119

roundRobin

Nov 25th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. typedef struct{
  4. int id;
  5. int at;
  6. int bt;
  7. int ct;
  8. }process;
  9.  
  10. int main()
  11. {
  12. printf("Enter number of processes:");
  13. int n;
  14. scanf("%d", &n);
  15. process p[n],rq[64];
  16. int tq = 2;
  17. int front=0,rear=1;
  18.  
  19. for (int i = 0; i < n; i++)
  20. {
  21. p[i].id = i+1;
  22. printf("Enter arrival time and burst time for P%d: ",i+1);
  23. scanf("%d %d",&p[i].at,&p[i].bt);
  24. }
  25.  
  26. //sort according to arrival time
  27. for (int i = 0; i < n; i++)
  28. {
  29. for (int j = 0; j < n-i-1; j++)
  30. {
  31. if(p[j].at > p[j+1].at)
  32. {
  33. process temp = p[j];
  34. p[j] = p[j+1];
  35. p[j+1] = temp;
  36. }
  37. }
  38. }
  39.  
  40. rq[front] = p[0];
  41. int time = p[0].at;
  42. int i=1;
  43. while(i<n || front!=rear)
  44. {
  45.  
  46. if(rq[front].bt > tq)
  47. {
  48. time += tq;
  49. rq[front].bt -= tq;
  50. while(i<n && p[i].at <=time)
  51. {
  52. rq[rear] = p[i];
  53. rear++;
  54. i++;
  55. }
  56. rq[rear++] = rq[front];
  57. }
  58.  
  59. else
  60. {
  61. time += rq[front].bt;
  62. for(int k=0;k<n;k++)
  63. {
  64. if(p[k].id == rq[front].id)
  65. {
  66. p[k].ct = time;
  67. break;
  68. }
  69. }
  70. }
  71.  
  72. front++;
  73. }
  74.  
  75.  
  76. //arrange p according to id
  77. for (int i = 0; i < n; i++)
  78. {
  79. for (int j = 0; j < n-i-1; j++)
  80. {
  81. if(p[j].id > p[j+1].id)
  82. {
  83. process temp = p[j];
  84. p[j] = p[j+1];
  85. p[j+1] = temp;
  86. }
  87. }
  88. }
  89.  
  90. printf("P\tAT\tBT\tCT\n");
  91. for (int i = 0; i < n; i++)
  92. {
  93. printf("P%d\t%d\t%d\t%d\n",p[i].id,p[i].at,p[i].bt,p[i].ct);
  94. }
  95.  
  96.  
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement