Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. struct schedulerResult process_SSTF_request(struct schedulerInput request){
  2. struct schedulerResult results;
  3. int size = request.requests.elements;
  4. results.requests.data = (int *)malloc(sizeof(int)*size);
  5. results.requests.elements = size;
  6. int head = request.startTrack;
  7. int seek_count = 0;
  8. int distance, cur_track;
  9. int location;
  10.  
  11. for (int i = 0; i < size; i++) {
  12. location = nextTrack(request.requests.data, head, request.requests.elements);
  13. cur_track = request.requests.data[location];
  14. results.requests.data[i] = cur_track;
  15. // delete element at location
  16. request.requests.elements--;
  17. for (int j = location; j < request.requests.elements; j++){
  18. //printf("%d ", request.requests.data[j]);
  19. request.requests.data[j] = request.requests.data[j+1];
  20. }
  21. int* temp = realloc(request.requests.data, request.requests.elements * sizeof(int));
  22. request.requests.data = temp;
  23.  
  24. //printf("Current Track: %d\n", cur_track);
  25.  
  26. distance = abs(cur_track - head);
  27. seek_count += distance;
  28. head = cur_track;
  29. }
  30.  
  31. printf("SSTF ->");
  32. results.totalHeadMovement = seek_count;
  33.  
  34. return results;
  35. }
  36.  
  37.  
  38. struct schedulerResult process_SCAN_request(struct schedulerInput request){
  39. struct schedulerResult results;
  40.  
  41. int seek_count = 0;
  42. int distance, cur_track;
  43. int size = request.requests.elements;
  44. int capacity = size;
  45. int* left = malloc(capacity * sizeof(int));
  46. int* right = malloc(capacity * sizeof(int));
  47. results.requests.data = (int *)malloc(sizeof(int)*size);
  48. results.requests.elements = size;
  49. int direction = request.direction;
  50. int head = request.startTrack;
  51. int lcount = 0;
  52. int rcount = 0;
  53.  
  54. // appending end values
  55. // which has to be visited
  56. // before reversing the direction
  57. if (direction == -1)
  58. push(left, lcount, 0, &lcount, &capacity);
  59. else if (direction == 1)
  60. push(right, rcount, disk_size, &rcount, &capacity);
  61.  
  62. // push the values to the right of the head to right arry and same for left
  63. for (int i = 0; i < size; i++) {
  64. if (request.requests.data[i] < head)
  65. push(left, lcount, request.requests.data[i], &lcount, &capacity);
  66.  
  67. if (request.requests.data[i] > head)
  68. push(right, rcount, request.requests.data[i], &rcount, &capacity);
  69. }
  70.  
  71. // sorting left and right vectors
  72.  
  73. for (int i = 0; i < lcount; i++) {
  74. for (int j = 0; j < lcount; j++) {
  75. if (left[j] > left[i]) {
  76. int tmp = left[i];
  77. left[i] = left[j];
  78. left[j] = tmp;
  79. }
  80. }
  81. }
  82.  
  83. for (int i = 0; i < rcount; i++) {
  84. for (int j = 0; j < rcount; j++) {
  85. if (right[j] > right[i]) {
  86. int tmp = right[i];
  87. right[i] = right[j];
  88. right[j] = tmp;
  89. }
  90. }
  91. }
  92.  
  93. /*
  94. printf("Left: %d\n", lcount);
  95. for (int i =0; i < lcount; i++)
  96. printf("%d ", left[i]);
  97.  
  98. printf("Right: %d\n", rcount);
  99. for (int i =0; i < rcount; i++)
  100. printf("%d ", right[i]);
  101. */
  102.  
  103. // run the while loop two times.
  104. // one by one scanning right
  105. // and left of the head
  106. int run = 2;
  107. int count = 0;
  108.  
  109. while (run--) {
  110. if (direction == -1) {
  111. for (int i = lcount - 1; i >= 0; i--) {
  112. cur_track = left[i];
  113.  
  114. if (cur_track != 0){
  115. results.requests.data[count] = cur_track;
  116. count++;
  117. }
  118. else{
  119. if (head == 1)
  120. seek_count--;
  121. }
  122.  
  123. // calculate absolute distance // seek time
  124. distance = abs(cur_track - head);
  125. seek_count += distance;
  126. head = cur_track;
  127. }
  128. direction = 1;
  129. }
  130. else if (direction == 1) {
  131. for (int i = 0; i < rcount; i++) {
  132. cur_track = right[i];
  133.  
  134. if (cur_track != disk_size){
  135. results.requests.data[count] = cur_track;
  136. count++;
  137. }
  138.  
  139. distance = abs(cur_track - head);
  140. seek_count += distance;
  141. head = cur_track;
  142.  
  143. }
  144. direction = -1;
  145. }
  146. }
  147. free(left);
  148. free(right);
  149.  
  150. printf("SCAN ->");
  151.  
  152. results.totalHeadMovement = seek_count;
  153.  
  154. return results;
  155. }
  156.  
  157.  
  158. struct schedulerResult process_C_SCAN_request(struct schedulerInput request){
  159. struct schedulerResult results;
  160. int seek_count = 0;
  161. int distance, cur_track;
  162. int size = request.requests.elements;
  163. int capacity = size;
  164. int* left = malloc(capacity * sizeof(int));
  165. int* right = malloc(capacity * sizeof(int));
  166. results.requests.data = (int *)malloc(sizeof(int)*size);
  167. results.requests.elements = size;
  168.  
  169. int head = request.startTrack;
  170. int lcount = 0;
  171. int rcount = 0;
  172.  
  173. // appending end values
  174. // which has to be visited
  175. // before reversing the direction
  176.  
  177. // push the values to the right of the head to right arry and same for left
  178. for (int i = 0; i < size; i++) {
  179. if (request.requests.data[i] < head)
  180. push(left, lcount, request.requests.data[i], &lcount, &capacity);
  181.  
  182. if (request.requests.data[i] > head)
  183. push(right, rcount, request.requests.data[i], &rcount, &capacity);
  184. }
  185.  
  186. push(left, lcount, 0, &lcount, &capacity);
  187. push(right, rcount, disk_size, &rcount, &capacity);
  188.  
  189. // sorting left and right vectors
  190. for (int i = 0; i < lcount; i++) {
  191. for (int j = 0; j < lcount; j++) {
  192. if (left[j] > left[i]) {
  193. int tmp = left[i];
  194. left[i] = left[j];
  195. left[j] = tmp;
  196. }
  197. }
  198. }
  199.  
  200. for (int i = 0; i < rcount; i++) {
  201. for (int j = 0; j < rcount; j++) {
  202. if (right[j] > right[i]) {
  203. int tmp = right[i];
  204. right[i] = right[j];
  205. right[j] = tmp;
  206. }
  207. }
  208. }
  209.  
  210. int count = 0;
  211. for (int i = 0; i < rcount; i++) {
  212. cur_track = right[i];
  213.  
  214. if (cur_track != disk_size){
  215. results.requests.data[count] = cur_track;
  216. count++;
  217. }
  218.  
  219. distance = abs(cur_track - head);
  220. seek_count += distance;
  221. head = cur_track;
  222. }
  223.  
  224. head = 0;
  225. seek_count += disk_size;
  226.  
  227. for (int i = 0; i < lcount; i++) {
  228. cur_track = left[i];
  229.  
  230. if (cur_track != 0){
  231. results.requests.data[count] = cur_track;
  232. count++;
  233. }
  234.  
  235. distance = abs(cur_track - head);
  236. seek_count += distance;
  237. head = cur_track;
  238. }
  239. free(left);
  240. free(right);
  241.  
  242. printf("C-SCAN ->");
  243.  
  244. results.totalHeadMovement = seek_count;
  245.  
  246. return results;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement