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

Defination of the essential properties of operating systems

Define the essential properties of the following types of operating sys-tems:  Batch  Interactive  Time sharing  Real time  Network  Parallel  Distributed  Clustered  Handheld ANSWERS: a. Batch processing:-   Jobs with similar needs are batched together and run through the computer as a group by an operator or automatic job sequencer. Performance is increased by attempting to keep CPU and I/O devices busy at all times through buffering, off-line operation, spooling, and multi-programming. Batch is good for executing large jobs that need little interaction; it can be submitted and picked up later. b. Interactive System:-   This system is composed of many short transactions where the results of the next transaction may be unpredictable. Response time needs to be short (seconds) since the user submits and waits for the result. c. Time sharing:-   This systems uses CPU scheduling and multipro-gramming to provide economical interactive use of a system. The CPU switches rapidl

What is a Fair lock in multithreading?

  Photo by  João Jesus  from  Pexels In Java, there is a class ReentrantLock that is used for implementing Fair lock. This class accepts optional parameter fairness.  When fairness is set to true, the RenentrantLock will give access to the longest waiting thread.  The most popular use of Fair lock is in avoiding thread starvation.  Since longest waiting threads are always given priority in case of contention, no thread can starve.  The downside of Fair lock is the low throughput of the program.  Since low priority or slow threads are getting locks multiple times, it leads to slower execution of a program. The only exception to a Fair lock is tryLock() method of ReentrantLock.  This method does not honor the value of the fairness parameter.