disk_scheduling.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. NAME : RUSHIKESH M FANSE
  3. ROLL NO : 16
  4. DIV : H
  5. */
  6. #include<cstdio>
  7. #include<iostream>
  8. #include<algorithm>
  9. #define MAX 20
  10. using namespace std;
  11. int tracks,curr_head,h_pos_f,h_pos_s,method,track_traversed,avg_seek_len,seek_length,start;
  12. int a[MAX],b[MAX];
  13. void FIFO()
  14. {
  15. int i;
  16. int k=0;
  17. for(i=0;i<tracks;i++)
  18. {
  19. track_traversed+= (a[i]>curr_head)?(a[i]-curr_head):(curr_head-a[i]);
  20. curr_head =a[i];
  21. if(curr_head!=start)
  22. k++;
  23. }
  24. cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/k<<"\n";
  25. }
  26. void SSTF()
  27. {
  28. int i,j,k;
  29. for(j=0;start>=b[j];j++);
  30. i=j-1;
  31. if((start-b[i])<(b[j]-start))
  32. {
  33. for(int k=i;k>=0;k--)
  34. {
  35. track_traversed+=curr_head-b[k];
  36. curr_head=b[k];
  37. }
  38. for(int k=j;k<tracks;k++)
  39. {
  40. track_traversed+=b[k]-curr_head;
  41. curr_head=b[k];
  42. }
  43. }
  44. else if((start-b[i])>(b[j]-start))
  45. {
  46. for(int k=j;k<tracks;k++)
  47. {
  48. track_traversed+=b[k]-curr_head;
  49. curr_head=b[k];
  50. }
  51. for(int k=i;k>=0;k--)
  52. {
  53. track_traversed+=curr_head-b[k];
  54. curr_head=b[k];
  55. }
  56. }
  57. cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/tracks<<"\n";
  58. }
  59. void SCAN()
  60. {
  61. int sc,i,j;
  62. cout<<"enter the direction in which scanning is to be done\n";
  63. cout<<"1.left\n2.right\n";
  64. cin>>sc;
  65. for(j=0;start>=b[j];j++);
  66. i=j-1;
  67. if(sc==1)
  68. {
  69. for(int k=i;k>=0;k--)
  70. {
  71. track_traversed+=curr_head-b[k];
  72. curr_head=b[k];
  73. }
  74. for(int k=j;k<tracks;k++)
  75. {
  76. track_traversed+=b[k]-curr_head;
  77. curr_head=b[k];
  78. }
  79. }
  80. else if(sc==2)
  81. {
  82. for(int k=j;k<tracks;k++)
  83. {
  84. track_traversed+=b[k]-curr_head;
  85. curr_head=b[k];
  86. }
  87. for(int k=i;k>=0;k--)
  88. {
  89. track_traversed+=curr_head-b[k];
  90. curr_head=b[k];
  91. }
  92. }
  93. cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/tracks<<"\n";
  94. }
  95. void CSCAN()
  96. {
  97. int sc,i,j;
  98. cout<<"enter the direction in which scanning is to be done\n";
  99. cout<<"1.left\n2.right\n";
  100. cin>>sc;
  101. for(j=0;start>=b[j];j++);
  102. i=j-1;
  103. if(sc==1)
  104. {
  105. for(int k=0;k<=i;k++)
  106. {
  107. track_traversed+=b[k]-curr_head;
  108. curr_head=b[k];
  109. }
  110. for(int k=j;k<tracks;k++)
  111. {
  112. track_traversed+=b[k]-curr_head;
  113. curr_head=b[k];
  114. }
  115. }
  116. else if(sc==2)
  117. {
  118. for(int k=j;k<tracks;k++)
  119. {
  120. track_traversed+=b[k]-curr_head;
  121. curr_head=b[k];
  122. }
  123. for(int l=0;l<=i;l++)
  124. {
  125. track_traversed+= (b[l]>curr_head)?(b[l]-curr_head):(curr_head-b[l]);
  126. curr_head=b[l];
  127. }
  128. }
  129. cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/tracks<<"\n";
  130. }
  131. int main()
  132. {
  133. cout<<"Enter the no of tracks\n";
  134. cin>>tracks;
  135. cout<<"enter the tracks\n";
  136. for(int i=0;i<tracks;i++)
  137. {
  138. cin>>b[i];
  139. a[i]=b[i];
  140. }
  141. sort(b,b+tracks);
  142. cout<<"which is current head\n";
  143. cin>>curr_head;
  144. start=curr_head;
  145. for(int i=0;i<tracks;i++)
  146. if(b[i]==curr_head)
  147. h_pos_s=i;
  148. while(method<=4)
  149. {
  150. track_traversed=0;
  151. avg_seek_len=0;
  152. curr_head=start;
  153. switch(method)
  154. {
  155. case 1:
  156. //FIFO
  157. cout<<"FIFO\n";
  158. FIFO();
  159. break;
  160. case 2:
  161. //SSTF
  162. cout<<"SSTF\n";
  163. SSTF();
  164. break;
  165. case 3:
  166. //SCAN
  167. cout<<"SCAN\n";
  168. SCAN();
  169. break;
  170. case 4:
  171. //CSCAN
  172. cout<<"CSCAN\n";
  173. CSCAN();
  174. break;
  175. }
  176. method++;
  177. }
  178. }
  179. /*
  180. OUTPUT:
  181. rishi@rishi-PC:~$ ./a.out
  182. Enter the no of tracks
  183. 9
  184. enter the tracks
  185. 55 58 39 18 90 160 150 38 184
  186. which is current head
  187. 100
  188. FIFO
  189. track traversed : 498 average seek length : 55.3333
  190. SSTF
  191. track traversed : 248 average seek length : 27.5556
  192. SCAN
  193. enter the direction in which scanning is to be done
  194. 1.left
  195. 2.right
  196. 2
  197. track traversed : 250 average seek length : 27.7778
  198. CSCAN
  199. enter the direction in which scanning is to be done
  200. 1.left
  201. 2.right
  202. 2
  203. track traversed : 322 average seek length : 35.7778
  204. */