Question

In: Computer Science

IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...

IN jAVA Language PLEASE

Write a JAVA program that implements the following disk-scheduling algorithms:

a. FCFS

b. SSTF

c. SCAN

Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose.

The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head movement required for each algorithm.

Solutions

Expert Solution

Answer:

Java program that implements the disk scheduling algorithms.

a. FCFS
b. SSTF
c. SCAN

Program:

// This node class is used by the SSTF algorithm method.

node.java

public class node {
   int distance = 0;
// true if the track has been accessed
boolean accessed = false;
}

DiskscheAlgo.java


import java.util.Random;

public class DiskscheAlgo {
  
// initialize the CYLINDERS and REQUESTS
   static int CYLINDERS = 5000;
   static int REQUESTS = 50;

   // Initialize start = 0
   static int start = 0;

   static int[] ran_array = new int[REQUESTS];
   int[] test_array = new int[REQUESTS];

   // This method calculate movements for FCFS algorithm
   static int fcfs(int[] ran_array) {

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

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

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

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

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

       return head_movement;
   }

   // This method calculate distance which is used in SSTF algorithm
   public static void calculateDifference(int queue[], int head, node diff[])

   {
       for (int i = 0; i < diff.length; i++)
           diff[i].distance = Math.abs(queue[i] - head);
   }

   // This method finds the minimum distance
   public static int findMin(node diff[]) {
       int index = -1, minimum = Integer.MAX_VALUE;

       for (int i = 0; i < diff.length; i++) {
           if (!diff[i].accessed && minimum > diff[i].distance) {

               minimum = diff[i].distance;
               index = i;
           }
       }
       return index;
   }

   // This method calculate movements for SSTF algorithm
   public static int sstf(int[] ran_array) {
       int head = 0;
       if (ran_array.length == 0)
           return 0;

       node diff[] = new node[ran_array.length];

       for (int i = 0; i < diff.length; i++)

           diff[i] = new node();

       int seek_count = 0;

       int[] seek_sequence = new int[ran_array.length + 1];

       for (int i = 0; i < ran_array.length; i++) {

           seek_sequence[i] = head;
           calculateDifference(ran_array, head, diff);

           int index = findMin(diff);

           diff[index].accessed = true;

           seek_count += diff[index].distance;

           head = ran_array[index];
       }

       seek_sequence[seek_sequence.length - 1] = head;

       return seek_count;

   }

   // This method calculate movements for SCAN algorithm
   public static int scan(int[] ranArray) {

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

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

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

       }

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

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

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

       }

       return head_movement;

   }

   // Main method
   public static void main(String[] args) {
       Random rand = new Random();
      
// Generate random array
       for (int i = 0; i < REQUESTS; i++) {

           ran_array[i] = rand.nextInt(CYLINDERS);
       }

       System.out.println("Start index: " + start + ", " + "start value: " + ran_array[start]);

       // Calculate and print all head movements for algorithms
       System.out.println("FCFS head movements: " + fcfs(ran_array));
       System.out.println("SSTF head movements: " + sstf(ran_array));
       System.out.println("SCAN head movements: " + scan(ran_array));
   }

}

Output:

Start index: 0, start value: 3975
FCFS head movements: 81431
SSTF head movements: 4909
SCAN head movements: 84029


Related Solutions

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. FCFS SSTF SCAN C-SCAN LOOK 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...
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.
List of six process scheduling algorithms (for operating systems): First-Come, First-Served (FCFS) Scheduling Shortest-Job-Next (SJN) Scheduling...
List of six process scheduling algorithms (for operating systems): First-Come, First-Served (FCFS) Scheduling Shortest-Job-Next (SJN) Scheduling Priority Scheduling Shortest Remaining Time Round Robin(RR) Scheduling Multiple-Level Queues Scheduling Compare and contrast the algorithms against one another here. You have at least six algorithms, so this should be an extensive part of the project, and all research based.
in C++ We are going to implement the following scheduling algorithms 1. First-Come First-Served (FCFS) 2....
in C++ We are going to implement the following scheduling algorithms 1. First-Come First-Served (FCFS) 2. Shortest Remaining Time First (SRTF) 3. Highest Response Ratio Next (HRRN) 4. Round Robin, with di_erent quantum values (RR) We are interested to compute the following metrics, for each experiment: _ The average turnaround time _ The total throughput (number of processes done per unit time) _ The CPU utilization _ The average number of processes in the ready queue The simulator needs to...
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...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that...
Write an MSP430 assembly language program that implements the following algorithm: 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. I have another file where I save my vectors called, "vars.c" here I save: int a[] = { 14, 65, 9} int b[] =...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that sums up all the numeric characters present in a phrase ("string" that follows the "C" language convention). By For example, if the phrase is "Today is the 28th of month 9", the subroutine must perform the following sum: 2 + 8 + 9 = 19. The subroutine must receive the address of the first character of the corresponding phrase in the "stack", and return...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called ‘numadd’ that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called ‘numadd’ that sums up all the numeric characters present in a sentence (“string” that follows the “C” language convention). For example, if the phrase is "Today is the 21st of month 5", the subroutine must perform the following sum: 2 + 1 + 5 = 8. The subroutine must receive the address of the first character of the corresponding phrase in the " stack ”, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT