Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define up_bound 200 // Upper Bound
- #define low_bound 0 // Lower Bound
- int n, head; //n -> number of tracks, head -> starting position
- void sort(int arr[], int size) { // Bubble Sort -> Ascending order
- for (int i = 0; i < size - 1; ++i) {
- for (int j = 0; j < size - i - 1; ++j) {
- if (arr[j] > arr[j + 1]) {
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- }
- int findHead(int tracks[]) { // Find index of head from sorted array tracks[n+3]
- int position = 1;
- for (int i = 0; i < n + 3; ++i) {
- if (tracks[i] == head) {
- position = i;
- break;
- }
- }
- return position;
- }
- void FCFS(int tracks[]) { //Requests are processed in the order they arrive
- int sum = 0, current = head, new; /* sum -> Total seek time
- * current -> to track the previously processed request
- * new -> absolute value of difference b/w previous and current request
- */
- printf("\nTraversed Order : { %d =>", head);
- for (int i = 0; i < n; ++i) {
- if (i < n - 1)
- printf(" %d =>", tracks[i]);
- else
- printf(" %d }", tracks[i]);
- new = current > tracks[i] ? current - tracks[i] : tracks[i] - current; /* if(current>head)
- current - tracks[i]
- else
- tracks[i] - current */
- current = tracks[i];
- sum += new; // sum = sum + new
- }
- printf("\nTotal Head Movement = %d\n", sum);
- }
- void SCAN(int tracks[]) { /*moves the disk arm in one direction (either left or right) until it reaches the
- end of the disk, When it reaches the end, it reverses direction and continues.*/
- int sum = 0, new, current; /* sum -> Total seek time
- * current -> to track the previously processed request
- * new -> absolute value of difference b/w previous and current request
- */
- tracks[n] = head; // insert head, upper_bound and lower_bound to the sequence
- tracks[n + 1] = low_bound;
- tracks[n + 2] = up_bound;
- sort(tracks, n + 3); // sort array including the newly inserted values
- int head_pos = findHead(tracks); // find the index of head from the sorted array
- printf("\nTraversed Order : { %d =>", tracks[head_pos]);
- current = tracks[head_pos];
- for (int i = head_pos + 1; i < n + 3; ++i) { // Loop to calculate disk movement from head to upper_bound
- printf(" %d =>", tracks[i]);
- new = tracks[i] - current;
- current = tracks[i];
- sum += new;
- }
- for (int i = head_pos - 1; i > 0; i--) { //Loop to calculate disk movement from upper_bound to the least request
- if (i > 1)
- printf(" %d =>", tracks[i]);
- else
- printf(" %d }", tracks[i]);
- new = current - tracks[i];
- current = tracks[i];
- sum += new;
- }
- printf("\nTotal Head Movement = %d\n", sum);
- }
- void C_SCAN(int tracks[]) { /*Moves the disk arm in one direction (either left or right) until it reaches the
- end of the disk, As soon as it reaches the other end, it immediately returns to the
- beginning without servicing any requests, It then starts servicing requests again once
- it reaches the beginning.*/
- int sum = 0, new, current;
- tracks[n] = head;
- tracks[n + 1] = low_bound;
- tracks[n + 2] = up_bound;
- sort(tracks, n + 3);
- int head_pos = findHead(tracks);
- printf("\nTraversed Order: { %d =>", tracks[head_pos]);
- current = tracks[head_pos];
- for (int i = head_pos + 1; i < n + 3; ++i) {
- printf(" %d =>", tracks[i]);
- new = tracks[i] - current;
- current = tracks[i];
- sum += new;
- }
- sum += current;
- current = tracks[0];
- for (int i = 0; i < head_pos; ++i) {
- if (i < head_pos - 1)
- printf(" %d =>", tracks[i]);
- else
- printf(" %d }", tracks[i]);
- new = tracks[i] - current;
- current = tracks[i];
- sum += new;
- }
- printf("\nTotal Head Movement = %d\n", sum);
- }
- int main() {
- while (1) {
- /////////////////////////////////////////////////
- printf("\nEnter number of tracks:");
- scanf("%d", &n);
- printf("Enter head position:");
- scanf("%d", &head);
- int tracks[n + 3]; // 3 index extra for inserting head, lower_bound and upper-bound
- printf("Enter %d sequence of tracks: ", n);
- for (int i = 0; i < n; ++i) {
- scanf("%d", &tracks[i]);
- }
- /////////////////////////////////////////////////
- int ch;
- printf("\nEnter your choice:\n1. FCFS\n2. SCAN\n3. C-Scan\n0. Exit\n");
- scanf("%d", &ch);
- switch (ch) {
- case 1:
- FCFS(tracks);
- break;
- case 2:
- SCAN(tracks);
- break;
- case 3:
- C_SCAN(tracks);
- break;
- case 0:
- exit(0);
- default:
- printf("Invalid Argument!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement