Advertisement
Guest User

priorioty prem

a guest
Dec 13th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct process{
  5. char name[5];
  6. int bt;
  7. int at;
  8. int prt;
  9. int wt,ta;
  10. int flag;
  11. }processes;
  12.  
  13. int main()
  14. {
  15. processes P[10];
  16. int i,n;
  17. printf("\n Enter total no. of processes : ");
  18. scanf("%d",&n);
  19. for(i=0;i<n;i++){
  20. printf("\n PROCESS [%d]",i+1);
  21. printf(" Enter process name : ");
  22. scanf("%s",&P[i].name);
  23. printf(" Enter burst time : ");
  24. scanf("%d",&P[i].bt);
  25. printf(" Enter arrival time : ");
  26. scanf("%d",&P[i].at);
  27. printf(" Enter priority : ");
  28. scanf("%d",&P[i].prt);
  29. }
  30. printf("\n PROC.\tB.T.\tA.T.");
  31. for(i=0;i<n;i++)
  32. printf("\n %s\t%d\t%d\t%d",P[i].name,P[i].bt,P[i].at,P[i].prt);
  33.  
  34. /*processes t;
  35. int j,temp[20];
  36. for(i=1;i<n;i++)
  37. for(j=0;j<n-i;j++){
  38. if(temp[j].at > temp[j+1].at){
  39. t = temp[j];
  40. temp[j] = temp[j+1];
  41. temp[j+1] = t;
  42. }
  43. }
  44. */
  45. //processes t;
  46. int t_total=0,tcurr,b[10],x,min_pr;
  47. int sumw=0,sumt=0;
  48. float avgwt=0.0,avgta=0.0;
  49. processes temp[10],t;
  50.  
  51. for(i=0;i<n;i++){
  52. temp[i]=P[i];
  53. t_total+=P[i].bt;
  54. }
  55.  
  56.  
  57. int j;
  58. for(i=1;i<n;i++)
  59. for(j=0;j<n-i;j++){
  60. if(temp[j].at > temp[j+1].at){
  61. t = temp[j];
  62. temp[j] = temp[j+1];
  63. temp[j+1] = t;
  64. }
  65. }
  66.  
  67. for(i=0;i<n;i++)
  68. b[i] = temp[i].bt;
  69.  
  70. i=j=0;
  71. printf("\n GANTT CHART\n\n %d %s",i,temp[i].name);
  72. for(tcurr=0;tcurr<t_total;tcurr++)
  73. {
  74.  
  75. if(b[i] > 0 && temp[i].at <= tcurr)
  76. b[i]--;
  77.  
  78. if(i!=j)
  79. printf(" %d %s",tcurr,temp[i].name);
  80.  
  81. if(b[i]<=0 && temp[i].flag != 1)
  82. {
  83. temp[i].flag = 1;
  84. temp[i].wt = (tcurr+1) - temp[i].bt - temp[i].at;
  85. temp[i].ta = (tcurr+1) - temp[i].at;
  86. sumw+=temp[i].wt;
  87. sumt+=temp[i].ta;
  88. }
  89. j=i;
  90. min_pr = 999;
  91. for(x=0;x<n;x++){
  92.  
  93. if(temp[x].at <= (tcurr+1) && temp[x].flag != 1){
  94.  
  95. if(min_pr != temp[x].prt && min_pr > temp[x].prt){
  96. min_pr = temp[x].prt;
  97. i=x;
  98. }
  99. }
  100. }
  101.  
  102. }
  103. printf(" %d",tcurr);
  104. avgwt = (float)sumw/n;
  105. avgta = (float)sumt/n;
  106. printf("\n\n Average waiting time = %0.2f\n Average turn-around = %0.2f.",avgwt,avgta);
  107.  
  108. return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement