Skip to main content

Disk scheduling algorithm java code




 The first step is to create a CSCAN class


public class CSCAN {


    public int m=0; //used to store the initial position of the head


     public boolean Run=true;


     public int sum=0;


     public void Check(int a[],int n,int position){


         int temp;


         for (int i = n; i> 0; i--)


         {    // Bubble Sort


              for (int j = 0; j <i; j++){


    if (a[j]> a[j + 1]) {// Compare the sizes in order


         temp = a[j]; // put the big number in front


         a[j] = a[j + 1];


         a[j + 1] = temp;


    }


              }


         }


         while (Run)


         {


             for (int i = 0; i <= n; i++){


                 if (a[i] == position){ m=i;


                 Run=false;


                 }


             }


         }


         System.out.println("CSCAN algorithm sequence:");


         for(int i=m+1;i<=n;i++)


         {//The head moves to the large


              sum=sum+a[i]-a[i-1];


     System.out.print(a[i]+" ");


         }


        sum=sum+200-a[n];


         sum=sum+199;


        sum=sum+a[0]-1;


     for(int i=0;i<=m-1;i++){


         if(i!=m-1){


             sum=sum+a[i+1]-a[i];


         }


         System.out.print(a[i]+" ");


     }


     System.out.println();


     System.out.println("Seek length is "+sum);


     System.out.println("The average seek length is "+sum/n);}


}




The second step is to create a DiskManage class




import java.util.Scanner;




public class DiskManage {




    public static void main (String [] args){


        int a[]=new int[20];


        int n=0;


        int m=0;


        double sum=0;


        double temp=0;


        int position=0;


        SSTF sstf=new SSTF();


        SCAN scan = new SCAN();


        CSCAN cscan = new CSCAN();


        System.out.println("Please enter the number of disk requests:");


        try{


        Scanner reader=new Scanner(System.in); n=reader.nextInt();}


        catch(Exception e){}


        System.out.println("Please enter the disk request sequence:");


        Scanner reader = new Scanner(System.in);


        for (int i = 1; i <= n; i++) {try{


            a[i]=reader.nextInt();}


            catch(Exception e){}}


        System.out.print("Please enter the initial position of the magnetic head:");




       // try{


            Scanner re = new Scanner(System.in);


            a[0]=re.nextInt(); position=a[0]; //}


    // catch(for (int i = 1; i <= n; i++){Exception e){}


     // System.out.print(a[i] + "");}


      System.out.println("******************************************* **********************");


        System.out.println("Hint: The sequence range of the head is 1~200! The head seeks from small to large!");


        System.out.println("Please enter the corresponding selection operation::");


        System.out.println("1, first come, first served algorithm (FCFS)");


        System.out.println("2, the shortest seek time priority algorithm (SSTF)");


        System.out.println("3, scanning algorithm (SCAN)");


        System.out.println("4, cyclic scanning algorithm (CSCAN)");


        Scanner read = new Scanner(System.in);


        try{ m=read.nextInt();}


        catch(Exception e){}


        System.out.println("The value of m" + m);


        read.close();


        switch (m) {


   case 1: sum=0;


   System.out.println("FCFS algorithm sequence:");


   for (int i = 1; i <= n; i++){


       System.out.print(a[i] + "");


       temp = a[i]-a[i-1];


       if (temp >= 0);


       else if (temp <0) {temp=-temp;}


       sum=sum+temp;} System.out.println();


   System.out.println("The value of sum" + sum);


   System.out.println("The average found length is" + sum / n);break;


   case 2: sstf.Calculate(a, n, position); break;


   case 3: scan.Check(a, n, position); break;


   case 4: cscan.Check(a, n, position); break;}


    }


}


The third step is to create a SCAN class




public class SCAN {


    public int m=0; //used to store the initial position of the head


     public boolean Run=true;


     public int sum=0;


     public void Check(int a[],int n,int position){


         int temp; for (int i = n; i> 0; i--) // bubble sort


              {for (int j = 0; j <i; j++){


                  if (a[j]> a[j + 1]) // compare the sizes in order


                    {temp = a[j]; // put the big number in front


                         a[j] = a[j + 1];


                         a[j + 1] = temp;}}}


                         while (Run) {//This loop is used to find where the initial position of the head is arranged


                              for (int i = 0; i <= n; i++)


                              {if (a[i] == position) {


                                  m = i;


                                  Run = false;


                              }


                              }


                         }


                         System.out.println("SCAN algorithm sequence:");


                         for(int i=m+1;i<=n;i++){//The head moves to the large


            sum=sum+a[i]-a[i-1];


            System.out.print(a[i]+" ");


                         }


            sum=sum+200-a[n];


                         sum=sum+200-a[m-1];


            for(int i=m-1;i>=0;i--)


            {if(i!=0){


                sum=sum+a[i]-a[i-1];


            }


                System.out.print(a[i]+" ");


            }


            System.out.println();


            System.out.println("The length found is "+sum);


            System.out.println("The average found length is "+sum/n);


     }


   }

The fourth step is to create an SSTF class




public class SSTF {




    public int m; // The index used to determine the initial position of the head after sorting


     public int b[]; public int rflag = 0, lflag = 0;


     public double SUM=0; public int mleft, mright;


     public SSTF(){ b=new int[20];}


     public void Calculate(int a[], int n, int position)


     {


         int temp;


         for (int i = n; i> 0; i--) // bubble sort


               {


                   for (int j = 0; j <i; j++)


                   {


       if (a[j]> a[j + 1]) // compare the sizes in order


              {temp = a[j]; // put the big number in front


                         a[j] = a[j + 1];


                         a[j + 1] = temp;}


                   }


               }


                         if (a[n] == position) {//If the initial position of the head is at the maximum number


                               System.out.println("SSTF sequence is:");


                               for (int i = 0; i <= n; i++)


                               {System.out.print(a[i] + "");


                               }


                         }


                         else if (a[0] == position)


                         {System.out.println("SSTF sequence is:");


                         for (int i = n; i >= 0; i--)


                         {System.out.print(a[i] + "");


                         }


                         }


                         else {


                             System.out.println("SSTF sequence is:");


                         Check(a, n, position);


                         }


     }


                  public void Check(int a[], int n, int position)


                  {


                      boolean Run = true, Running = true;


                  while (Run) {//This loop is used to find where the initial position of the head is arranged


                       for (int i = 0; i <= n; i++){


                           if (a[i] == position)


                           {


                               m = i;


                           System.out.println("The value of m" + m);


                           mleft = m-1;


                           mright = m + 1;


                           Run = false;


                           }


                       }


                  }


                  while (Running){


                      if (a[m]-a[mleft] >= a[mright]-a[m] && rflag != 1)


                      {


                          SUM=SUM+(a[mright]-a[m]); //Used to count the length of the search


                            m=mright;


                            mright++;


               if (mright> n){


                   rflag = 1;


               }


               System.out.print(a[m] + "");


                      }


                      else if (a[m]-a[mleft] <a[mright]-a[m] && lflag != 1)


                      {SUM=SUM+(a[m]-a[mleft]);


                      m = mleft;


                      mleft--;


                      if (mleft <0){


                          lflag = 1;


                      }


                      System.out.print(a[m] + "");


                      }


                      if (mleft != 0 && mright> n){


                          Running = false;


                      SUM=SUM+a[n]-a[mleft];


                      for (int j = mleft; j >= 0; j--) {


                          if(j!=0){


                          SUM=SUM+a[j]-a[j-1];}


                System.out.print(a[j] + "");


                      }


                      }


                      if (mright != n && mleft <0)


                      {


                          Running = false;


                          SUM=SUM+a[mright]-a[0];


                      for (int k = mright; k <= n; k++)


                      {


                          if(k!=0){


                              SUM=SUM+a[k+1]-a[k];


                          }


                      System.out.print(a[k] + "");


                      }


                      }


                  }


                  System.out.println();


                  System.out.println("The value of sum"+SUM);


                  System.out.println("The average search length is "+SUM/n);}




}

Comments