Question

Write a java program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will serv
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code (Please read comments for explanation) :-

1 2 import java.util.Random; import java.util.Arrays; import java.util.Scanner; class Main{ public static int[] remove TheEle// if the index is not // the removal element index anotherArray[k++] = arr[i]; Il return the resultant array return anotherAint index=0; for (int i=0;i size ;i++) { if (temparr[i]==firstNum) { index=i; break; while (totalNumScaned size) { if (index>temparr=remove TheElement(temparr , index); else if(index>0 && index<=size-totalNumScaned -1){ totalNumScaned++; totalmoves+=105 106 107 108 109 110 111 112 int totalmoves=0; Arrays.sort(arr); int index=0; for (int i=0;i<size ;i++) { if (arr[i]==firs132 133 134 135 136 137 138 139 140 Main m=new Main(); System.out.print(Total moves in FCFS= ); System.out.println(m.FCFS(a

Here is the output : -

Total moves in FCFS= 1651345 Total moves in SSTF= 6410 Total moves in SCAN= 9976 Total moves in FCFS= 1642693 Total moves in

Here is the code in text format : -

import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;
class Main{

   public static int[] removeTheElement(int[] arr, int index)
{
  
// If the array is empty
// or the index is not in array range
// return the original array
if (arr == null || index < 0 || index >= arr.length) {
return arr;
}
  
// Create another array of size one less
int[] anotherArray = new int[arr.length - 1];
  
// Copy the elements except the index
// from original array to the other array
for (int i = 0, k = 0; i < arr.length; i++) {
  
// if the index is
// the removal element index
if (i == index) {
continue;
}
  
// if the index is not
// the removal element index
anotherArray[k++] = arr[i];
}
  
// return the resultant array
return anotherArray;
}

   int FCFS(int[] arr, int size){
       int totalmoves=0;
       for (int i=0;i<size-1;i++){
           totalmoves+=Math.abs(arr[i]-arr[i+1]);
       }
       return totalmoves;
   }
   int SSTF(int[] arr, int size, int firstNum){
       int[] temparr=new int[size]; //duplicate array
       for (int i=0;i<size ;i++ ) { // store in duplicate array
           temparr[i]=arr[i];
       }
       int totalmoves=0;
       int totalNumScaned=0;
       Arrays.sort(temparr); //Sort temparr in asending order
       int index=0;
       for (int i=0;i<size ;i++ ) {
           if (temparr[i]==firstNum) {
               index=i;
               break;
           }
       }

       while (totalNumScaned<size) {
           if (index>0 && index<size-totalNumScaned-1) {
               // Calculate difference between current and left number
               int leftDifference=Math.abs(temparr[index]-temparr[index-1]);
               // Calculate difference between current and right number
               int rightDifference=Math.abs(temparr[index]-temparr[index+1]);
               // Check which neighbour is near (left or right)
               if (leftDifference<rightDifference) { //If left neighbour is near
                   totalNumScaned++;
                   totalmoves+=Math.abs(temparr[index]-temparr[index-1]);
                   // Remove current element
                   temparr=removeTheElement(temparr, index);
                   // Jump to left
                   index--;
               }
               else { //If right neighbour is near
                   totalNumScaned++;
                   totalmoves+=Math.abs(temparr[index]-temparr[index+1]);
                   temparr=removeTheElement(temparr ,index);
               }
           }
           else if(index>0 && index<=size-totalNumScaned-1){
               totalNumScaned++;
               totalmoves+=Math.abs(temparr[index]-temparr[index-1]);
               // Remove current element
               temparr=removeTheElement(temparr ,index);
               index--;
           }
           else if(index>=0 && index<size-totalNumScaned-1){
               totalNumScaned++;
               totalmoves+=Math.abs(temparr[index]-temparr[index+1]);
               // Remove current element
               temparr=removeTheElement(temparr ,index);
           }
           if (totalNumScaned==size-1) {
               removeTheElement(temparr, index);
               totalNumScaned++;
           }
       }
       return totalmoves;

   }
  
   int SCAN(int[] arr, int size, int firstNum){
       int totalmoves=0;
       Arrays.sort(arr);
       int index=0;
       for (int i=0;i<size ;i++ ) {
           if (arr[i]==firstNum) {
               index=i;
               break;
           }
       }
       if (index==0 || index==size-1) {
           return arr[size-1]-arr[0];
       }
       totalmoves+=arr[size-1]-arr[index];
       totalmoves+=arr[size-1]-arr[0];
       totalmoves+=arr[index-1]-arr[0];
       return totalmoves;
   }

   public static void main(String[] args) {
       int[] arr=new int[1000];
       Random rand = new Random(); //To generate random number
       for (int i=0;i<arr.length;i++) {
           // Generate random number between 0,4999
           arr[i]=rand.nextInt(5000);
       }
       int firstNum=arr[0];

       Main m=new Main();
       System.out.print("Total moves in FCFS= ");
       System.out.println(m.FCFS(arr,arr.length));
       System.out.print("Total moves in SSTF= ");
       System.out.println(m.SSTF(arr,arr.length,firstNum));
       System.out.print("Total moves in SCAN= ");
       System.out.println(m.SCAN(arr,arr.length,firstNum));
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a java program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a  program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN d. C-SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will...

    Write a  program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN d. C-SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. 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 and total number of change of direction required by each algorithm under each of the following cases: a)The program will generate a random...

  • Use any language Task two Write a program that implements the following disk-scheduling algorithms: a. FCFS...

    Use any language Task two Write a program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN d. C-SCAN e.LOOK f. C-LOOK Your program will service a disk with 500 cylinders numbered 0 to 499. The program will generate a random series of 20 cylinder requests and service them according to each of the algorithms listed above. The proram will be passed the initial position of the disk head (as a parameter on the command line) and...

  • *** SIMPLE C PROGRAM WILL RATE GOOD**** PLEASE LEAVE COMMENTS THROUGHOUT THE CODE SO I CAN...

    *** SIMPLE C PROGRAM WILL RATE GOOD**** PLEASE LEAVE COMMENTS THROUGHOUT THE CODE SO I CAN SEE WHAT IS HAPPENING AND LEARN, THANK YOU! Write a program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN d. C-SCAN e. LOOK f. C-LOOK Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 1,000 cylinder requests and service them according to each of the algorithms listed above....

  • I/O Scheduling Algorithms a) For each of the following scheduling algorithms, give a 1-2 sentence description...

    I/O Scheduling Algorithms a) For each of the following scheduling algorithms, give a 1-2 sentence description of how it works. The description should be precise enough to distinguish each algorithm from the others. Algorithms: FCFS, SSTF, SCAN, C-SCAN, C-LOOK. (1 point) b) Given a hard disk with 200 cylinders and a queue with jobs having the following cylinder requests: 80, 190, 70, 130, 30, draw a diagram of the movements of the head for each of the algorithms listed in...

  • Suppose that a disk drive has 10,000 cylinders, numbered 0 to 10,000. The drive is currently...

    Suppose that a disk drive has 10,000 cylinders, numbered 0 to 10,000. The drive is currently serving a request at cylinder 7,423 and the previous request was at cylinder 8213. The queue of pending requests, in FIFO order, is: 9324, 6324, 7232, 1304, 5621, 105, 5382, 3241, 2425, 9891. Starting from the current head position, what is the total distance (in cylinders) that the disk arm moves to satisfy all the pending requests for each of the following disk-scheduling algorithms?...

  • 8.) [12 pts.] Suppose that a disk drive is currently serving a request at cylinder 1010,...

    8.) [12 pts.] Suppose that a disk drive is currently serving a request at cylinder 1010, the previous red cylinder 1000. The queue of pending requests, in FIFO order, is: drive has 2,000 cylinders, numbered O to 1,999. The 1010, 1310, 1950, 800, 200, 750, 1000 Starting from the current head position, what is the total distance (in cylinders) that the disk arm moves to satisty all the pending requests for each of the following disk scheduling algorithms a.) FCFS...

  • Suppose that a disk drive has 6,000 cylinders, numbered 0 to 5999. The drive is currently...

    Suppose that a disk drive has 6,000 cylinders, numbered 0 to 5999. The drive is currently serving a request at cylinder 3150, and the previous request was at cylinder 1805 (Hint: this indicates the reading head’s moving direction). The queue of pending requests, in FIFO order, is: 3511, 2332, 2800, 3192, 658, 1296, 1918, 1356, 5936, 2527 Starting from the current head position, what is the total distance (in cylinders) that the disk arm moves to satisfy all the pending...

  • 1. Suppose that a disk drive has 5,000 cylinders, numbered 0 to 4,999. The drive is...

    1. Suppose that a disk drive has 5,000 cylinders, numbered 0 to 4,999. The drive is currently serving a request at cylinder 2,150, and the previous request was at cylinder 1,805. The queue of pending requests, in FIFO order, is: 2,069 1,212 2,296 2,800 544 1,618 356 1,523 4,956 3,681 Starting from the current head position, what is the total distance (in cylinders) that the disk arm moves to satisfy all the pending requests for each of the following disk-scheduling...

  • Disk requests come into the disk driver for cylinders 10, 22, 20, 2, 40, 6, and...

    Disk requests come into the disk driver for cylinders 10, 22, 20, 2, 40, 6, and 38, in that order. Assume that the disk has 100 cylinders numbered 0 through 99. The arm (head) is initially at cylinder 20. Based on the following disk scheduling algorithms, draw scheduling diagram indicating the movement of the head as arm (head) moves around the cylinders services the requests. i. (5 points) First Come First Served ii. (5 points) Shortest Seek Time First iii....

  • I need a full solution to this question. ( Operating Systems ) First Image: Second Image:...

    I need a full solution to this question. ( Operating Systems ) First Image: Second Image: Third Image: Fourth Image: thank you. Suppose that a disk has 5000 cylinders, numbered 0 to 1999. The drive is currently serving a request at cylinder 143, and the previous request was at cylinder 152. The queue of pending requests is as follows: 130, 1750 1022, 1509, 948, 1774, 913, 1470, 86 Starting from the current head position, what is total "seek time" for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT