In: Computer Science
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS
SCAN This algorithm is performed by moving the R/W head back-and-forth to the innermost and outermost track. As it scans the tracks from end to end, it process all the requests found in the direction it is headed. This will ensure that all track requests, whether in the outermost, middle or innermost location, will be traversed by the access arm thereby finding all the requests. This is also known as the Elevator algorithm.
LOOK This algorithm is similar to SCAN algorithm except for the end-to-end reach of each sweep. The R/W head is only tasked to go the farthest location in need of servicing. This is also a directional algorithm, as soon as it is done with the last request in one direction it then sweeps in the other direction.
FCFS It is the simplest form of disk scheduling algorithms. The I/O requests are served or processes according to their arrival. The request arrives first will be accessed and served first. Since it follows the order of arrival, it causes the wild swings from the innermost to the outermost tracks of the disk and vice versa . The farther the location of the request being serviced by the read/write head from its current location, the higher the seek time will be.
CLOOK Circular LOOK is like a C-SCAN which uses a return sweep before processing a set of disk requests. It does not reach the end of the tracks unless there is a request, either read or write on such disk location similar with the LOOK algorithm.
CSCAN This algorithm is a modified version of the SCAN algorithm. C-SCAN sweeps the disk from end-to-end,but as soon it reaches one of the end tracks it then moves to the other end track without servicing any requesting location. As soon as it reaches the other end track it then starts servicing and grants requests headed to its direction. This algorithm improves the unfair situation of the end tracks against the middle tracks
CODE:
// SCAN algorithm
import java.util.*;
class GFG
{
static int size = 8;
static int disk_size = 200;
static void SCAN(int arr[], int head, String direction)
{
int seek_count = 0;
int distance, cur_track;
Vector<Integer> left = new Vector<Integer>(),
right = new Vector<Integer>();
Vector<Integer> seek_sequence = new Vector<Integer>();
// appending end values
// which has to be visited
// before reversing the direction
if (direction == "left")
left.add(0);
else if (direction == "right")
right.add(disk_size - 1);
for (int i = 0; i < size; i++)
{
if (arr[i] < head)
left.add(arr[i]);
if (arr[i] > head)
right.add(arr[i]);
}
// sorting left and right vectors
Collections.sort(left);
Collections.sort(right);
// run the while loop two times.
// one by one scanning right
// and left of the head
int run = 2;
while (run-- >0)
{
if (direction == "left")
{
for (int i = left.size() - 1; i >= 0; i--)
{
cur_track = left.get(i);
// appending current track to seek sequence
seek_sequence.add(cur_track);
// calculate absolute distance
distance = Math.abs(cur_track - head);
// increase the total count
seek_count += distance;
// accessed track is now the new head
head = cur_track;
}
direction = "right";
}
else if (direction == "right")
{
for (int i = 0; i < right.size(); i++)
{
cur_track = right.get(i);
// appending current track to seek sequence
seek_sequence.add(cur_track);
// calculate absolute distance
distance = Math.abs(cur_track - head);
// increase the total count
seek_count += distance;
// accessed track is now new head
head = cur_track;
}
direction = "left";
}
}
System.out.print("Total number of seek operations = "
+ seek_count + "\n");
System.out.print("Seek Sequence is" + "\n");
for (int i = 0; i < seek_sequence.size(); i++)
{
System.out.print(seek_sequence.get(i) + "\n");
}
}
//
public static void main(String[] args)
{
// request array
int arr[] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
String direction = "left";
SCAN(arr, head, direction);
}
}
OUTPUT:
LOOK ALGORITHM:
FCFS ALGORITHM
Implementation of FCFS is given below. Note that distance is used to store absolute distance between head and current track position.
CODE IN JAVA
// FCFS Disk Scheduling algorithm
class GFG
{
static int size = 8;
static void FCFS(int arr[], int head)
{
int seek_count = 0;
int distance, cur_track;
for (int i = 0; i < size; i++)
{
cur_track = arr[i];
// calculate absolute distance
distance = Math.abs(cur_track - head);
// increase the total count
seek_count += distance;
// accessed track is now new head
head = cur_track;
}
System.out.println("Total number of " +
"seek operations = " +
seek_count);
// Seek sequence would be the same
// as request array sequence
System.out.println("Seek Sequence is");
for (int i = 0; i < size; i++)
{
System.out.println(arr[i]);
}
}
//
public static void main(String[] args)
{
// request array
int arr[] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
FCFS(arr, head);
}
}
OUTPUT: