In: Computer Science
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.
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