ash_wath

Sjf

Sep 4th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int i,n,p[10]={1,2,3,4,5,6,7,8,9,10},min,k=1,btime=0;
  5. int bt[10],temp,j,at[10],wt[10],tt[10],ta=0,sum=0;
  6. float wavg=0,tavg=0,tsum=0,wsum=0;
  7. printf(" -------Shortest Job First Scheduling ( NP )-------\n");
  8. printf("\nEnter the No. of processes :");
  9. scanf("%d",&n);
  10.  
  11. for(i=0;i<n;i++)
  12. {
  13. printf("\tEnter the burst time of %d process :",i+1);
  14. scanf(" %d",&bt[i]);
  15. printf("\tEnter the arrival time of %d process :",i+1);
  16. scanf(" %d",&at[i]);
  17. }
  18.  
  19. /*Sorting According to Arrival Time*/
  20.  
  21. for(i=0;i<n;i++)
  22. {
  23. for(j=0;j<n;j++)
  24. {
  25. if(at[i]<at[j])
  26. {
  27. temp=p[j];
  28. p[j]=p[i];
  29. p[i]=temp;
  30. temp=at[j];
  31. at[j]=at[i];
  32. at[i]=temp;
  33. temp=bt[j];
  34. bt[j]=bt[i];
  35. bt[i]=temp;
  36. }
  37. }
  38. }
  39.  
  40. /*Arranging the table according to Burst time,
  41. Execution time and Arrival Time
  42. Arrival time <= Execution time
  43. */
  44.  
  45. for(j=0;j<n;j++)
  46. {
  47. btime=btime+bt[j];
  48. min=bt[k];
  49. for(i=k;i<n;i++)
  50. {
  51. if (btime>=at[i] && bt[i]<min)
  52. {
  53. temp=p[k];
  54. p[k]=p[i];
  55. p[i]=temp;
  56. temp=at[k];
  57. at[k]=at[i];
  58. at[i]=temp;
  59. temp=bt[k];
  60. bt[k]=bt[i];
  61. bt[i]=temp;
  62. }
  63. }
  64. k++;
  65. }
  66. wt[0]=0;
  67. for(i=1;i<n;i++)
  68. {
  69. sum=sum+bt[i-1];
  70. wt[i]=sum-at[i];
  71. wsum=wsum+wt[i];
  72. }
  73.  
  74. wavg=(wsum/n);
  75. for(i=0;i<n;i++)
  76. {
  77. ta=ta+bt[i];
  78. tt[i]=ta-at[i];
  79. tsum=tsum+tt[i];
  80. }
  81.  
  82. tavg=(tsum/n);
  83.  
  84. printf("************************");
  85. printf("\n RESULT:-");
  86. printf("\nProcess\t Burst\t Arrival\t Waiting\t Turn-around" );
  87. for(i=0;i<n;i++)
  88. {
  89. printf("\n p%d\t %d\t %d\t\t %d\t\t\t%d",p[i],bt[i],at[i],wt[i],tt[i]);
  90. }
  91.  
  92. printf("\n\nAVERAGE WAITING TIME : %f",wavg);
  93. printf("\nAVERAGE TURN AROUND TIME : %f",tavg);
  94. return 0;
  95. }
Add Comment
Please, Sign In to add comment