Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that implements the following disk-scheduling algorithms from ch

ID: 3712831 • Letter: W

Question

Write a program that implements the following disk-scheduling algorithms from chapter 9 in your text:

Your program will service a disk with n cylinders numbered 0 to n-1. The program will read a data file (“Asg5Data.txt)found in the assignment section which has two sets of scheduling data. The first entry in the data file is the number of cylinders for your disk. The next entry is the cylinder number the disk position at the beginning of the simulation. Next, comes a line with a string of numbers representing the numbers of cylinders with I/O requests and service them according to each of the algorithms listed above. Following that is second set of this data in the file but with different values including a different number of cylinders and a different start position. Your program will output the total amount of head movement required by each algorithm. The output should consist of two sets of the following:

For FCFS, the total head movement was xxx cylinders.

For SSTF, the total head movement was xxx cylinders.

For SCAN, the total head movement was xxx cylinders.

For C-SCAN, the total head movement was xxx cylinders.

For LOOK, the total head movement was xxx cylinders.

For C-LOOK, the total head movement was xxx cylinders.

  

Make certain to remove or turn off any debug output before you generate the output file.

Assume the disk will move from the smaller to larger cylinder numbers for the scan and look algorithms. Do not forget to count the “back to the start” movement for the C-SCAN and C-LOOK algorithms.

The data in this first set is that used on pages 463 – 467 in your text and may be used to check some of your code. Then your program will read a second similar set of data from the same file and run that. Note that the data second set has different number of cylinders.

Make comments - do any of the algorithms look especially good or bad?  How many hours did the assignment take? What did you learn from this assignment?


Asg5Data.txt file

Explanation / Answer

import java.io.*;
import java.util.*;
public class HelloWorld{
    int cyl,head,h;
    int n;
  
     public static void main(String []args){
         int i=0,j=0,total=0,flag1;
        String fileName = "Asg5Data.txt";
        // This will reference one line at a time
        String line = null;
        try {
            // FileReader reads text files in the default encoding.
            FileReader fr =new FileReader(fileName);
            // Always wrap FileReader in BufferedReader.
            BufferedReader br =new BufferedReader(fileReader);
            while((line = br.readLine()) != null) {
                cyl=Integer.parseInt(line);
                head=Integer.parseInt(br.readLine());
                h=head;
                line=br.readLine();
                String[] sa = line.split(" ");
                int[] cyls = new int[sa.length];
                int[] cylscpy = new int[sa.length+1];
                n=sa.length;
                int[] flag=new int[n];
                int[] arr=new int[n];
                int[] a=new int[n];
                for(i = 0; i <n; i++) {
                cyls[i] = Integer.parseInt(sa[i]);
                cylscpy[i]=cyls[i];
                flag[i]=0;
                }
                for(i=0;i<n;i++)
                {
                    if(cyls[i] > head)
                    total=total+cyls[i]-head;
                    else
                    total=total+head-cyls[i];
                    flag=cyls[i];
                }
                System.out.println(“For FCFS, the total head movement was "+ total+" cylinders”);
                i=total=0;
                head=h;
                int x,min,loc;
                for(i=0;i<n;i++)
                {               
                x=0; min=0;loc=0;
                for(j=0;j<n;j++)
                    {
                        if(flag[j]==0)
                        {
                        if(x==0)
                        {
                             arr[j]=head-d[j].num;
                            if(arr[j]<0)
                            {
                                arr[j]=cyls[j]-head;
                            }
                        min=ar[j];loc=j;x++;
                        }
                        else
                        {
                            arr[j]=head-cyls[j];
                            if(arr[j]<0){ arr[j]=cyls[j]-head;}
                        }
                        if(min>arr[j]){ min=arr[j]; loc=j;}
                    }
                }
                flag[loc]=1;
                 a[i]=cyls[loc]-head;
                 if(a[i]<0){a[i]=head-cyls[loc];}
                head=cyls[loc];
                }
             for(i=0;i<n;i++)
                    total=total+a[i];    
                System.out.println(“For SSTF, the total head movement was "+ total+" cylinders”);
             i=total=0;
            head=h;
            int cn=n+1,temp;
        for(i=0;i<cn;i++)    // sorting disk locations
        {
            for(j=i;j<cn;j++)
            {
                if(cylscpy[i]>cylscpy[j])
                {
                    temp=cylscpy[i];
                    cylscpy[i]=cylscpy[j];
                    cylscpy[j]=temp;
                 }
             }
        }
         for(i=0;i<n;i++)   // to find loc of disc in array
        {
             if(head==cylscpy[i]) { loc=i; break; }
         }
        total=head+cylscpy[n];
       System.out.println(“For SCAN, the total head movement was "+ total+" cylinders”);
       i=total=0;
        head=h;
        int t,k=0,t1=0,t2=0;
        while(i < n)
      {
          t=cyls[k]
            if(t >= head)
            {
                  arr[t1] = t;
                  t1++;
            }
            else
            {
                  a[t2] = t;
                  t2++;
            }
            i++;
      }
      i = 0;
      while(i < t1 - 1)
      {
            j = i + 1;
            while(j < t1)
            {
                  if(arr[i] > arr[j])
                  {
                        t = arr[i];
                        arr[i] = arr[j];
                        arr[j] = t;
                  }
                  j++;
            }
            i++;
      }
      i = 0;
      while(i < t2 - 1)
      {
            j = i + 1;
            while(j < t2)
            {
                  if(a[i] > a[j])
                  {
                        t = a[i];
                        a[i] = a[j];
                        a[j] = t;
                  }
                  j++;
            }
            i++;
      }
      i = 1;
      j = 0;
      while(j < t1)
      {
            cylscpy[i] = arr[j];
            cylscpy[i] = cyl;
            cylscpy[i + 1] = 0;
            i++;
            j++;
      }
      i = t1 + 3;
      j = 0;
      while(j < t2)
      {
            cylscpy[i] = a[j];
            cylscpy[0] = head;
            i++;
            j++;
      }
      for(j = 0; j <= n + 1; j++)
      {
            total = seek_time + abs(cylscpy[j + 1] - cylscpy[j]);
     }
      System.out.println(“For C-SCAN, the total head movement was "+ total+" cylinders”);
      head=h;
      i=j=total=k=0;
       for(i=0;i<n;i++)
    {
        if(cyls[i]<head)
        {
            arr[j]=cyls[i];
            j++;
        }
        if(cyls[i]>cp)
        {
            a[k]=cyls[i];
            k++;
        }
    }
    //sort the lower array in reverse order
    for(i=0;i<j;i++)
    {
        for(int l=0;l<j-1;l++)
        {
            if(arr[l]<arr[l+1])
            {
                t=arr[l];
                arr[l]=arr[l+1];
                arr[l+1]=t;
            }
        }
    }
    // sort the upper array in ascending order
    for(i=0;i<=k;i++)
    {
        for(int l=0;l<k-1;l++)
        {
            if(a[l]>a[l+1])
            {
                t=a[l];
                a[l]=a[l+1];
                a[l+1]=temp;
            }
        }
    }
    int x=0;
            for(i=0;i<k;i++)
            {     
                cylscpy[x]=arr[i];
                x++;         
            }
            for(i=0;i<j;i++)
            {     
                cylscpy[x]=arr[i];
                x++;             
            }
    total=total+abs(head-cylscpy[0]);
    for(i=1;i<x;i++)
    {
        total=total+abs(cylscpy[i]-cylscpy[i-1]);
    }
     System.out.println(“For LOOK, the total head movement was "+ total+" cylinders”);
       head=h;
      i=j=total=k=0;
        int pos=0;
     for(i=0;i<n;i++)
        for(j=0;j<n-i-1;j++)
           if(cyls[j]>cyls[j+1])
            {x=cyls[j];
             cyls[j]=cyls[j+1];
             cyls[j+1]=x;
          }
           for(i=0;i<n;i++)
              if(cyls[i]<head)
                   pos++;
                    x=head;
                for(i=pos;i<n;i++)
                    {total+=abs(x,cyls[i]);
                         x=cyls[i];
                        }
                for(i=0;i<pos;i++)
                     {total+=abs(x,a[i]);
                        x=a[i];
                      }
                  
     System.out.println(“For C-LOOK, the total head movement was "+ total+" cylinders”);
            // Always close files.
            bufferedReader.close();       
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" +
                fileName + "'");              
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '"
                + fileName + "'");                
     }
}

NOTE: if abs is not working, try replacing it with Math.abs();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote