Advertisement
Sri27119

sjf

Nov 25th, 2024
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 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. int n=5;
  13. process p[n],q[n];
  14.  
  15.  
  16. for(int i = 0; i < n; i++)
  17. {
  18. p[i].id = i+1;
  19. printf("Enter arrival time and burst time for process P%d",i+1);
  20. scanf("%d %d",&p[i].at,&p[i].bt);
  21. }
  22.  
  23. //sort according to arrival time
  24. for (int i = 0; i < n; i++)
  25. {
  26. for (int j = 0; j < n-i-1; j++)
  27. {
  28. if(p[j].at > p[j+1].at)
  29. {
  30. process temp = p[j];
  31. p[j] = p[j+1];
  32. p[j+1] = temp;
  33. }
  34. }
  35. }
  36.  
  37. int front=0, rear=1;
  38. int time = p[0].at;
  39. q[front]=p[0];
  40. int i=1;
  41.  
  42. while(i<n || front!=rear)
  43. {
  44. time += q[front].bt;
  45. for(int k=0;k<n;k++)
  46. {
  47. if(p[k].id == q[front].id)
  48. {
  49. p[k].ct = time;
  50. break;
  51. }
  52. }
  53.  
  54. while(i<n && p[i].at <=time)
  55. {
  56. q[rear] = p[i];
  57. rear++;
  58. i++;
  59. }
  60.  
  61.  
  62.  
  63. front++;
  64.  
  65. for(int j=front+1; j<rear; j++)
  66. {
  67. if(q[front].bt>q[j].bt)
  68. {
  69. process temp = q[front];
  70. q[front] = q[j];
  71. q[j] = temp;
  72. }
  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. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement