Question

In: Computer Science

Write a program that implements the follow disk scheduling algorithms. You can use C or Java...

Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment.

  1. FCFS
  2. SSTF
  3. SCAN
  4. C-SCAN
  5. LOOK
  6. C-LOOK

Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required by each algorithm.

Run the program 10 times and report your findings (e.g. in a spreadsheet or text document).

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>

#define CYLINDERS 5000
#define REQUESTS 1000

int start = 0;

int ran_array[REQUESTS];
int test_array[REQUESTS];

int* sort_array() {

        int temp = 0, i = 0, j = 0;

        for (i = 0; i < REQUESTS; ++i) {

        for (j = i + 1; j < REQUESTS; ++j) {

            if (ran_array[i] > ran_array[j]) {

                temp =  ran_array[i];
                ran_array[i] =  ran_array[j];
                ran_array[j] = temp;
            }
        }
    }

    return ran_array;
}


/* First-Come-First-Serve (fcfs) starts from the index after the starting
index and continually adds the headmovement from the starting index in 
order recieved. If at end of array, start from index zero and continually 
add until starting index */
int fcfs(int *ran_array) {

        int i = 0, head_movement = 0, this_start = ran_array[start];

    for(i = start; i < REQUESTS; i++) {

        head_movement += abs(ran_array[i] - this_start);
    }

    for(i = 0; i < start; i++) {

        head_movement += abs(this_start - ran_array[i]);
    }
        
    return head_movement;
}

/* Shortest-Seek-Time-First (SSTF) takes the current head position, and
adds the position closest to the current head. This new position now becomes
the head, then this system repeats. 
First we sort the array. Then We have counters for above and below start 
index that we decrement if used. Once these equal to REQUEST-2 (excluding 
start index) we exit. */
int sstf(int * ran_array) {

        ran_array = sort_array();

        int small_i = start-1, large_i = start+1;
        int small_diff = 0, large_diff = 0;
        int head_movement = 0, total = REQUESTS-2, new_head = start, head_value = ran_array[start];
        
        while(total >= 0) {

                small_diff = abs(ran_array[new_head] - ran_array[small_i]);
                large_diff = abs(ran_array[large_i] - ran_array[new_head]);

                if(small_diff < large_diff) {

                        head_movement += small_diff;
                        new_head = small_i;
                        small_i--;
                        
                } else {

                        head_movement += large_diff;
                        new_head = large_i;
                        large_i++;
                }

                total--;

        }

        return head_movement;

}

/* SCAN - array is already sorted from sstf. SCAN starts from one left of start, 
and continually goes down to zero (if included in randome array or not). Then 
starts at one higher than start and continually goes up to highest value (not 5000) */
int scan(int * ranArray) {

        int i = 0, curr_val = 0, sav_val = ran_array[start], difference = 0;
        int head_movement = 0, curr_i = 0;

        for(i = start-1; i >= 0; --i) {

                curr_val = ran_array[i];
                difference = abs(sav_val - curr_val);
                head_movement += difference;
                sav_val = curr_val;

        }

        /* used to subtract value from zero, or just add same value */
        head_movement += sav_val;
        sav_val = 0;

        for(i = start+1; i < REQUESTS; i++) {

                curr_val = ran_array[i];
                difference = abs(curr_val - sav_val);
                head_movement += difference;
                sav_val = curr_val;

        }

        return head_movement;

}

/* Circular Scan (C-SCAN) - start at start index, increase to upper boundary 
(even if no value at boundary), save boundary value, go to start boundary 
(zero value) increase till last value before start value */
int cscan(int * ranArray) {

        int i = 0, curr_val = 0, sav_val = ran_array[start], difference = 0;
        int head_movement = 0, curr_i = 0, upper_bound = 4999;

        for(i = start+1; i < REQUESTS; i++) {

                curr_val = ran_array[i];
                difference = abs(sav_val - curr_val);
                head_movement += difference;
                sav_val = curr_val;

        }

        /* add last val - upper bound, go to and add zero bounday (4999)*/
        head_movement += upper_bound - sav_val;
        sav_val = 0;
        head_movement += 4999;

        for(i = 0; i < start; i++) {

                curr_val = ran_array[i];
                difference = abs(curr_val - sav_val);
                head_movement += difference;
                sav_val = curr_val;

        }

        return head_movement;
}

/* Look - start from value above start, increase to highest value. 
Then goes to value below start value and decreases until smallest value */
int look(int* ranArray) {

        int i = 0, curr_val = 0, sav_val = ran_array[start], difference = 0;
        int head_movement = 0, curr_i = 0;

        for(i = start+1; i < REQUESTS; i++) {

                curr_val = ran_array[i];
                difference = abs(sav_val - curr_val);
                head_movement += difference;
                sav_val = curr_val;

        }

        for(i = start-1; i >= 0; --i) {

                curr_val = ran_array[i];
                difference = abs(curr_val - sav_val);
                head_movement += difference;
                sav_val = curr_val;

        }

        return head_movement;
}

/* C-Look - Starts from value after start value, goes to highest value, 
then goes to smallest value and increases until value before start value */
int clook(int* ranArray) {

        int i = 0, curr_val = 0, sav_val = ran_array[start], difference = 0;
        int head_movement = 0, curr_i = 0;

        for(i = start+1; i < REQUESTS; i++) {

                curr_val = ran_array[i];
                difference = abs(sav_val - curr_val);
                head_movement += difference;
                sav_val = curr_val;

        }

        for(i = 0; i < start; i++) {

                curr_val = ran_array[i];
                difference = abs(curr_val - sav_val);
                head_movement += difference;
                sav_val = curr_val;

        }       

        return head_movement;
}


int main (int argc, char *argv[]) {


        int i = 0;
        start = atoi(argv[1]);

        if(argc != 2) {

                printf("Please compile program with starting index from 0-4999. Ex. ./diskAlgorithms 423\n");
                exit(-1);
        }

        for(i = 0; i < REQUESTS; i++) {

                ran_array[i] = rand() % 5000;
        }

        printf("\nStart index: %d, start value: %d\n\n", start, ran_array[start]);

        printf("FCFS head movements: %d\n", fcfs(ran_array));
        printf("SSTF head movements: %d\n", sstf(ran_array));
        printf("SCAN head movements: %d\n", scan(ran_array));
        printf("CSCAN head movements: %d\n", cscan(ran_array));
        printf("LOOK head movements: %d\n", look(ran_array));
        printf("C-LOOK head movements: %d\n\n", clook(ran_array));

        return 0;
}

Related Solutions

Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative...
Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative performance of the algorithms for a variety of datasets. Background The skeleton program sorting.cpp contains a main function for testing the operation of several sort algorithms over various data sizes and dataset organisations. The program understands the following arguments: -i insertion sort -s selection sort (default) -q quicksort -a (already) sorted dataset -v reverse-sorted dataset -r random dataset (default) -n no sorting x generate...
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
XML and JAVA Write a Java program that meets these requirements. It is important you follow...
XML and JAVA Write a Java program that meets these requirements. It is important you follow these requirements closely. • Create a NetBeans project named LastnameAssign1. Change Lastname to your last name. For example, my project would be named NicholsonAssign1. • In the Java file, print a welcome message that includes your full name. • The program should prompt for an XML filename to write to o The filename entered must end with .xml and have at least one letter...
Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter...
Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter 8 of your text. First generate a random page-reference string (this should be 20 entries long) where page numbers range from 0 to 9. Apply the random page-reference string to each algorithm, and record the number of page faults incurred by each algorithm. Implement the replacement algorithms so that the number of page frames goes from 1 to 7 and you must compute the...
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms...
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms are shell sort and quick sort. Assume that input data is stored in a text file. Experimenting with a prototype data (integers from 1 to 10) to ensure that your implementation works correctly and the results match expectations. The program will sort what is in the text file and print the amount of comparisons and exchanges for both algorithms.
Write an MSP430 assembly language program that implements the following 2 algorithms: 2) a macro called...
Write an MSP430 assembly language program that implements the following 2 algorithms: 2) a macro called "vdot" that calculates the "dot product" of two vectors "a" and "b", implemented as “arrays” (following the “C” language convention), of 3 elements. the macro should receive 2 pointers to the first element of each vector and return the result in R13.
Please use Java You have to write a programing for the following sorting algorithms in increasing...
Please use Java You have to write a programing for the following sorting algorithms in increasing order and print required steps in order to explain how your sorting algorithms work. 3. Implement the merge sort algorithm. With answers in this link (https://www.chegg.com/homework-help/questions-and-answers/write-programing-following-sorting-algorithms-increasing-order-print-required-steps-order--q53916147?trackid=PJ_TOK85), answer the above questions.
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT