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

Popular posts from this blog

40 Redis interview questions for 2021 - 2022

  Redis interview questions 1.What is Redis?. 2. What is the data type of Redis? 3. What are the benefits of using Redis? 4. What are the advantages of Redis over Memcached? 5. What are the differences between Memcache and Redis? 6. Is Redis single-process and single-threaded? 7. What is the maximum storage capacity of a string type value? 8. What is the persistence mechanism of Redis? Their advantages and disadvantages? 9. Redis common performance problems and solutions: 10. What is the deletion strategy of redis expired keys? 11. Redis recycling strategy (elimination strategy)? 12. Why does edis need to put all data in memory? 13. Do you understand the synchronization mechanism of Redis? 14. What are the benefits of Pipeline? Why use pipeline? 15. Have you used Redis cluster? What is the principle of cluster? 16. Under what circumstances will the Redis cluster solution cause the entire cluster to be unavailable? 17. What are the Java clients supp...

How does cloud computing work, and what are the benefits and drawbacks of using it?

  Cloud computing is a model for delivering information technology services over the internet. Instead of having software and hardware installed locally on a user's device, cloud computing allows users to access applications and data through a network of remote servers, usually maintained by a third-party provider . This provider is responsible for managing and maintaining the infrastructure, including servers, storage, databases, and software . Here is a more detailed explanation of how cloud computing works: Remote servers: Cloud computing relies on a network of remote servers that are maintained and managed by a third-party provider . These servers can be physically located anywhere in the world. Internet connectivity: To access cloud computing services, users must have an internet connection. This connection allows them to remotely access the resources on the remote servers. Virtualization: Cloud providers use virtualization technology to create multiple virtual machines ...

30 Best free 3D modeling software