ash_wath

Without Arrival Time

Sep 4th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. #include<stdio.h>
  2. void fcfs()
  3. {
  4. int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
  5. printf("Enter total number of processes:");
  6. scanf("%d",&n);
  7.  
  8. printf("\nEnter Process Burst Time\n");
  9. for(i=0;i<n;i++)
  10. {
  11. printf("%d:",i+1);
  12. scanf("%d",&bt[i]);
  13. }
  14.  
  15. wt[0]=0;
  16. for(i=1;i<n;i++)
  17. {
  18. wt[i]=0;
  19. for(j=0;j<i;j++)
  20. wt[i]+=bt[j];
  21. }
  22.  
  23. printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
  24. for(i=0;i<n;i++)
  25. {
  26. tat[i]=bt[i]+wt[i];
  27. avwt+=wt[i];
  28. avtat+=tat[i];
  29. printf("\n%d\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
  30. }
  31.  
  32. avwt/=i;
  33. avtat/=i;
  34. printf("\n\nAverage Waiting Time:%d",avwt);
  35. printf("\nAverage Turnaround Time:%d",avtat);
  36. }
  37. void sjf()
  38. {
  39. int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
  40. float avg_wt,avg_tat;
  41. printf("Enter number of process:");
  42. scanf("%d",&n);
  43.  
  44. printf("\nEnter Burst Time:\n");
  45. for(i=0;i<n;i++)
  46. {
  47. printf("p%d:",i+1);
  48. scanf("%d",&bt[i]);
  49. p[i]=i+1;
  50. }
  51. for(i=0;i<n;i++)
  52. {
  53. pos=i;
  54. for(j=i+1;j<n;j++)
  55. {
  56. if(bt[j]<bt[pos])
  57. pos=j;
  58. }
  59.  
  60. temp=bt[i];
  61. bt[i]=bt[pos];
  62. bt[pos]=temp;
  63.  
  64. temp=p[i];
  65. p[i]=p[pos];
  66. p[pos]=temp;
  67. }
  68.  
  69. wt[0]=0;
  70. for(i=1;i<n;i++)
  71. {
  72. wt[i]=0;
  73. for(j=0;j<i;j++)
  74. wt[i]+=bt[j];
  75.  
  76. total+=wt[i];
  77. }
  78.  
  79. avg_wt=(float)total/n;
  80. total=0;
  81.  
  82. printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
  83. for(i=0;i<n;i++)
  84. {
  85. tat[i]=bt[i]+wt[i];
  86. total+=tat[i];
  87. printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
  88. }
  89.  
  90. avg_tat=(float)total/n; //average turnaround time
  91. printf("\n\nAverage Waiting Time=%f",avg_wt);
  92. printf("\nAverage Turnaround Time=%f\n",avg_tat);
  93. }
  94. void priority()
  95. {
  96. int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
  97. printf("Enter Total Number of Process:");
  98. scanf("%d",&n);
  99.  
  100. printf("\nEnter Burst Time and Priority\n");
  101. for(i=0;i<n;i++)
  102. {
  103. printf("\n%d\n",i+1);
  104. printf("Burst Time:");
  105. scanf("%d",&bt[i]);
  106. printf("Priority:");
  107. scanf("%d",&pr[i]);
  108. p[i]=i+1;
  109. }
  110.  
  111. for(i=0;i<n;i++)
  112. {
  113. pos=i;
  114. for(j=i+1;j<n;j++)
  115. {
  116. if(pr[j]<pr[pos])
  117. pos=j;
  118. }
  119.  
  120. temp=pr[i];
  121. pr[i]=pr[pos];
  122. pr[pos]=temp;
  123.  
  124. temp=bt[i];
  125. bt[i]=bt[pos];
  126. bt[pos]=temp;
  127.  
  128. temp=p[i];
  129. p[i]=p[pos];
  130. p[pos]=temp;
  131. }
  132.  
  133. wt[0]=0;
  134. for(i=1;i<n;i++)
  135. {
  136. wt[i]=0;
  137. for(j=0;j<i;j++)
  138. wt[i]+=bt[j];
  139.  
  140. total+=wt[i];
  141. }
  142.  
  143. avg_wt=total/n;
  144. total=0;
  145.  
  146. printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
  147. for(i=0;i<n;i++)
  148. {
  149. tat[i]=bt[i]+wt[i];
  150. total+=tat[i];
  151. printf("\n%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
  152. }
  153.  
  154. avg_tat=total/n;
  155. printf("\n\nAverage Waiting Time=%d",avg_wt);
  156. printf("\nAverage Turnaround Time=%d\n",avg_tat);
  157. }
  158. void rr()
  159. {
  160. int count,j,n,time,remain,flag=0,time_quantum;
  161. int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
  162. printf("Enter Total Process:\t ");
  163. scanf("%d",&n);
  164. remain=n;
  165. for(count=0;count<n;count++)
  166. {
  167. printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
  168. scanf("%d",&at[count]);
  169. scanf("%d",&bt[count]);
  170. rt[count]=bt[count];
  171. }
  172. printf("Enter Time Quantum:\t");
  173. scanf("%d",&time_quantum);
  174. printf("\n\nProcess\tTurnaround Time Waiting Time\n\n");
  175. for(time=0,count=0;remain!=0;)
  176. {
  177. if(rt[count]<=time_quantum && rt[count]>0)
  178. {
  179. time+=rt[count];
  180. rt[count]=0;
  181. flag=1;
  182. }
  183. else if(rt[count]>0)
  184. {
  185. rt[count]-=time_quantum;
  186. time+=time_quantum;
  187. }
  188. if(rt[count]==0 && flag==1)
  189. {
  190. remain--;
  191. printf("%d\t\t%d\t\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
  192. wait_time+=time-at[count]-bt[count];
  193. turnaround_time+=time-at[count];
  194. flag=0;
  195. }
  196. if(count==n-1)
  197. count=0;
  198. else if(at[count+1]<=time)
  199. count++;
  200. else
  201. count=0;
  202. }
  203. printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
  204. printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
  205. }
  206. main()
  207. {
  208. int ch;
  209. printf("1.FCSF\n2.SJF\n3.Priority\n4.Round Robin\n");
  210. scanf("%d",&ch);
  211. switch(ch)
  212. {
  213. case 1:
  214. fcfs();
  215. break;
  216. case 2:
  217. sjf();
  218. break;
  219. case 3:
  220. priority();
  221. break;
  222. case 4:
  223. rr();
  224. break;
  225. default:
  226. printf("Invalid option");
  227. }
  228. }
Add Comment
Please, Sign In to add comment