NB52053

R. R

Dec 15th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include<stdio.h>
  2. struct process
  3. {
  4. char name;
  5. int at,bt,wt,tt,rt;
  6. int completed;
  7. float ntt;
  8. }p[10];
  9.  
  10. int n;
  11. int q[10];  //queue
  12. int front=-1,rear=-1;
  13.  
  14. void enqueue(int i)
  15. {
  16. if(rear==10)
  17. printf("overflow");
  18. rear++;
  19. q[rear]=i;
  20. if(front==-1)
  21. front=0;
  22.  
  23. }
  24.  
  25. int dequeue()
  26. {
  27. if(front==-1)
  28. printf("underflow");
  29. int temp=q[front];
  30. if(front==rear)
  31. front=rear=-1;
  32. else
  33. front++;
  34. return temp;
  35. }
  36.  
  37. int isInQueue(int i)
  38. {int k;
  39. for(k=front;k<=rear;k++)
  40. {
  41. if(q[k]==i)
  42. return 1;
  43. }
  44. return 0;
  45.  
  46. }
  47.  
  48. void sortByArrival()
  49. {
  50. struct process temp;
  51. int i,j;
  52. for(i=0;i<n-1;i++)
  53. for(j=i+1;j<n;j++)
  54. {
  55. if(p[i].at>p[j].at)
  56. {
  57. temp=p[i];
  58. p[i]=p[j];
  59. p[j]=temp;
  60. }
  61. }
  62. }
  63.  
  64. void main()
  65. {
  66. int i,j,time=0,sum_bt=0,tq;
  67. char c;
  68.         float avgtt=0;
  69.  printf("Enter no of processes:");
  70.  scanf("%d",&n);
  71.  for(i=0,c='A';i<n;i++,c++)
  72.  {
  73.  p[i].name=c;
  74.  printf("\nEnter the arrival time and burst time of process %c: ",p[i].name);
  75.  scanf("%d%d",&p[i].at,&p[i].bt);
  76.  p[i].rt=p[i].bt;
  77.  p[i].completed=0;
  78.  sum_bt+=p[i].bt;
  79.  
  80. }
  81.  
  82. printf("\nEnter the time quantum:");
  83. scanf("%d",&tq);
  84.  
  85. sortByArrival();
  86. enqueue(0);          // enqueue the first process
  87. printf("Process execution order: ");
  88. for(time=p[0].at;time<sum_bt;)       // run until the total burst time reached
  89. {   i=dequeue();
  90.  
  91. if(p[i].rt<=tq)
  92. {                          /* for processes having remaining time with less than or equal to time quantum  */
  93.  
  94. time+=p[i].rt;
  95. p[i].rt=0;
  96. p[i].completed=1;
  97.     printf(" %c ",p[i].name);
  98.             p[i].wt=time-p[i].at-p[i].bt;
  99.             p[i].tt=time-p[i].at;
  100.             p[i].ntt=((float)p[i].tt/p[i].bt);
  101.             for(j=0;j<n;j++)                /*enqueue the processes which have come while scheduling */
  102.             {
  103.             if(p[j].at<=time && p[j].completed!=1&& isInQueue(j)!=1)
  104.             {
  105.             enqueue(j);
  106.  
  107.             }
  108.            }
  109.         }
  110.     else               // more than time quantum
  111.     {
  112.     time+=tq;
  113.     p[i].rt-=tq;
  114.     printf(" %c ",p[i].name);
  115.     for(j=0;j<n;j++)    /*first enqueue the processes which have come while                                             scheduling */
  116.             {
  117.             if(p[j].at<=time && p[j].completed!=1&&i!=j&& isInQueue(j)!=1)
  118.              {
  119.             enqueue(j);
  120.  
  121.             }
  122.            }
  123.            enqueue(i);   // then enqueue the uncompleted process
  124.  
  125.     }
  126.  
  127.  
  128.  
  129. }
  130.  
  131. printf("\nName\tArrival Time\tBurst Time\tWaiting Time\tTurnAround Time\t Normalized TT");
  132. for(i=0;i<n;i++)
  133. {avgtt+=p[i].tt;
  134. printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d\t\t%f",p[i].name,p[i].at,p[i].bt,p[i].wt,p[i].tt,p[i].ntt);
  135. }
  136.  
  137. printf("\nAverage turnaround time:%.2f\n",avgtt/n);
  138. }
Add Comment
Please, Sign In to add comment