SherinKR

Disk.c

Oct 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. int q[10], noq, cp, i, j, temp;
  5. void fcfs();
  6. void scan();
  7. void cscan();
  8. void main()
  9. {
  10. int n, x;
  11. //printf("Enter the maximum number of cylinders : ");
  12. //scanf("%d", &n);
  13. printf("Enter the number of queues : ");
  14. scanf("%d", &noq);
  15. printf("Enter the position of queues :-\n");
  16. for(i=0;i<noq;i++)
  17. {
  18. scanf("%d", &q[i]);
  19. }
  20. printf("Enter the current position of the head : ");
  21. scanf("%d", &cp);
  22. do
  23. {
  24. printf("Choose disc scheduling type :-\n");
  25. printf("1. FCFS\n");
  26. //printf("2. SSTF\n");
  27. printf("2. SCAN\n");
  28. printf("3. CSCAN\n");
  29. printf("4. Exit\n");
  30. scanf("%d", &x);
  31. switch(x)
  32. {
  33. case 1 : fcfs();
  34. break;
  35. //case 2 : sstf();
  36. // break;
  37. case 2 : scan();
  38. break;
  39. case 3 : cscan();
  40. break;
  41. /*case 4 : look();
  42. break;
  43. case 5 : clook();
  44. break;*/
  45. case 4 : exit(0);
  46. default : printf("Enter a valid option : ");
  47. break;
  48. }
  49. }while(1);
  50. }
  51. void fcfs()
  52. {
  53. int i, t=0, cpp;
  54. cpp=cp;
  55. //printf("%d-->",cp);
  56. for(i=0;i<noq;i++)
  57. {
  58. t += abs(cpp-q[i]);
  59. printf("%d-->",cpp);
  60. cpp = q[i];
  61. }
  62. printf("%d\n",cpp);
  63. printf("Seek time : %d\n", t);
  64. }
  65. /*void sstf()
  66. {
  67. int min=999, temp
  68. for(i=0;i<noq;i++)
  69. {
  70. if(visit[i]==0)
  71. {
  72. if(min>abs(st-q[i])
  73. {
  74. min = abs(st-q[i]);
  75. p = i;
  76. }
  77. }
  78. /*for(i=0;i<noq;i++)
  79. {
  80. for(j=i+1;j<noq;j++)
  81. {
  82. if(q[i]>q[j])
  83. {
  84. temp=q[i];
  85. q[i] = q[j];
  86. q[j] = temp;
  87. }
  88. }
  89. }
  90. }*/
  91. void scan()
  92. {
  93. //printf("test\n");
  94. int st, e, t=0, max, dloc;
  95. /*for(i=0;i<noq;i++)
  96. {
  97. if(q[i]>q[i+1])
  98. {
  99. temp=q[i];
  100. q[i] = q[i+1];
  101. q[i+1] = temp;
  102. }
  103. }*/
  104. //printf("test\n");
  105. printf("Enter the disc movement range -\n");
  106. printf("Starting : ");
  107. scanf("%d",&st);
  108. printf("To : ");
  109. scanf("%d",&e);
  110. for(i=0;i<noq;i++);
  111. q[noq]=cp;
  112. noq+=1;
  113. for(i=0;i<noq;i++)
  114. {
  115. for(j=i;j<noq;j++)
  116. {
  117. if(q[i]>q[j])
  118. {
  119. temp=q[i];
  120. q[i] = q[j];
  121. q[j] = temp;
  122. }
  123. }
  124. }
  125. max = q[noq-1];
  126. //printf("test\n");
  127. for(i=0;i<noq;i++) // to find loc of disc in array
  128. {
  129. if(cp==q[i])
  130. {
  131. dloc=i;
  132. break;
  133. }
  134. }
  135. //printf("test\n");
  136. for(i=dloc;i>=0;i--)
  137. {
  138. //printf("test1\n");
  139. printf("%d-->",q[i]);
  140. }
  141. //printf("test\n");
  142. //if(q[i]!=st)
  143. printf("0-->");
  144. //printf("test\n");
  145. for(i=dloc+1;i<noq-1;i++)
  146. {
  147. printf("%d-->",q[i]);
  148. }
  149. //printf("test\n");
  150. printf("%d\n",q[i]);
  151. t=q[dloc]+max;
  152. printf("\nSeek time = %d\n",t);
  153. /*do
  154. {
  155. printf("%d-->",cp);
  156. for(i=cp;i!=e;i++)
  157. {
  158. for(j=0;j<noq;j++)
  159. {
  160. if(i==q[j])
  161. {
  162. if(v[j]==0)
  163. {
  164. printf("%d-->",i);
  165. v[j]==1;
  166. t+=abs(cp-i);
  167. cp=i;
  168. }
  169. }
  170. }
  171. }
  172. if(e!=cp)
  173. printf("%d-->",e);
  174. t+=abs(cp-e);
  175. cp=e;
  176. for(i=0;i<noq;i++)
  177. if(v[i]!=1)
  178. f=0;
  179. if(f==0)
  180. {
  181. for(i=e;i!=st;i--)
  182. {
  183. for(j=0;j<noq;j++)
  184. {
  185. if(i==q[j])
  186. {
  187. if(v[j]==0)
  188. {
  189. printf("%d-->",i);
  190. v[j]==1;
  191. t+=abs(cp-i);
  192. cp=i;
  193. }
  194. }
  195. }
  196. }
  197. t+=abs(cp-i);
  198. if(st!=cp)
  199. printf("%d",st);
  200. t+=abs(cp-st);
  201. printf("\n");
  202. }
  203. printf("Seek time : %d\n", t);
  204. break;
  205. }while(1);*/
  206. //printf("%d\n",t);*/
  207. }
  208. void cscan()
  209. {
  210. int st, e, t=0, f=1, max, dloc;
  211. int c1=0,c2=0,q1[10],q2[10];
  212. printf("Enter the disc movement range -\n");
  213. printf("Starting : ");
  214. scanf("%d",&st);
  215. printf("To : ");
  216. scanf("%d",&e);
  217. for(i=0;i<noq;i++)
  218. {
  219. for(j=i;j<noq;j++)
  220. {
  221. if(q[i]>q[j])
  222. {
  223. temp=q[i];
  224. q[i] = q[j];
  225. q[j] = temp;
  226. }
  227. }
  228. }
  229. for(i=0;i<noq;i++)
  230. {
  231. if(q[i]>=cp)
  232. {
  233. q1[c1++] = q[i];
  234. }
  235. else
  236. {
  237. q2[c2++] = q[i];
  238. }
  239. }
  240. if(cp!=q1[0])
  241. printf("%d-->",cp);
  242. for(i=0;i<c1;i++)
  243. {
  244. printf("%d-->",q1[i]);
  245. }
  246. if(e!=cp)
  247. printf("%d-->",e);
  248. t += (e-cp);
  249. if(st!=q2[0])
  250. printf("%d-->",st);
  251. for(i=0;i<c2-1;i++)
  252. {
  253. printf("%d-->",q2[i]);
  254. }
  255. t += (e-st)+q2[i];
  256. printf("%d\n",q2[i]);
  257. printf("Seek time : %d\n", t);
  258. /*for(i=0;i<noq;i++)
  259. {
  260. for(j=i;j<noq;j++)
  261. {
  262. if(q[i]>q[j])
  263. {
  264. temp=q[i];
  265. q[i] = q[j];
  266. q[j] = temp;
  267. }
  268. }
  269. }
  270. max = q[noq];
  271. for(i=0;i<noq;i++)
  272. {
  273. if(cp==q[i])
  274. {
  275. dloc=i;
  276. break;
  277. }
  278. }
  279. for(i=dloc;i<noq;i++)
  280. {
  281. printf("%d-->",q[i]);
  282. }
  283. if(q[i]!=e)
  284. printf("%d-->",e);
  285. printf("%d-->",st);
  286. for(i=0;i<dloc;i++)
  287. {
  288. printf("%d-->",q[i]);
  289. }
  290. printf("%d\n",q[i]);
  291. t=cp+max;
  292. printf("\nSeek time = %d\n",t);*/
  293. }
Add Comment
Please, Sign In to add comment