Operating System—Simulation and Implementation of Disk Scheduling Algorithm

1. Experimental topic

Simulation and Implementation of Disk Scheduling Algorithm

2. The purpose of the experiment

(1) Understand the disk structure and how data is organized on the disk.

(2) Master the calculation method of disk access time.

(3) Master common disk scheduling algorithms and their related characteristics.

3. Overall design (including background knowledge or basic principles and algorithms, or module introduction, design steps, etc.)

A typical representative of a shared device is a disk. The address of the physical block of the disk is specified by the cylinder number, head number, and sector number. The access to a certain physical block of the disk needs to go through three stages: seek time Ts, rotation delay time Tw and Read and write time Trw.

The seek time Ts is the time required for the head to move from the current track to the target track; the rotation delay time Tw is the time for the target physical block to rotate from the current position to the head position after the head stays on the target track; the read and write time Trw is the target The time when the physical block content is exchanged with the corresponding in memory. The principle of disk scheduling is fairness and high throughput. The metrics include access time T and average access time Ta:

T=Ts+Tw+Trw

Ta=Tsa+Twa+Trwa

Seek time and rotational delay time become the main considerations of the scheduling algorithm. Reducing access time means reducing seek time and rotational latency.

(1) First Come First Service Algorithm (FCFS) First Come First Service

This is a relatively simple disk scheduling algorithm. It schedules according to the order in which processes request access to the disk. The advantage of this algorithm is that it is fair and simple, and the requests of each process can be processed in turn, and there will be no situation where the request of a certain process cannot be satisfied for a long time. Since this algorithm does not optimize the seek, in the case of a large number of disk access requests, this algorithm will reduce the throughput of the device service, resulting in a longer average seek time, but the response time of each process to get the service The change is small.

(2) Shortest Seek Time First Algorithm (SSTF) Shortest Seek Time First

The algorithm selects such a process that requires the track to be accessed to be the closest to the track where the current head is located, so that the seek time is the shortest each time. The algorithm can get better throughput, but it cannot guarantee the shortest average seek time. . Its disadvantage is that the chances of responding to the user's service request are not equal, resulting in a large variation in response time. In the case of a large number of service requests, requests to the inner and outer edge tracks will be delayed indefinitely, and the response time of some requests will be unpredictable.

(3) Scanning Algorithm (SCAN) Elevator Scheduling

The scanning algorithm not only considers the distance between the track to be accessed and the current track, but also the current moving direction of the magnetic head. For example, when the magnetic head is moving from the inside out, the next access object selected by the scanning algorithm should be that the track it wants to visit is not only outside the current track, but also the closest. Access from the inside to the outside in this way, until there is no more outer magnetic track to be accessed, the magnetic arm is reversed and moves from the outside to the inside. At this time, such a process is also selected for scheduling each time, that is, the track to be accessed is within the current track, thereby avoiding the occurrence of starvation. Because the movement of the magnetic head in this algorithm is quite similar to the operation of the elevator, it is also called the elevator scheduling algorithm. This algorithm basically overcomes the shortcomings of the shortest seek time first algorithm that the service is concentrated in the middle track and the response time varies greatly, and has the advantages of the shortest seek time first algorithm, that is, the throughput is large and the average response time is small, but Due to the wobble scanning method, the frequency of accessing the tracks on both sides is still lower than that of the middle track.

(4) Cyclic scanning algorithm (C-SCAN)

    On the basis of the scanning algorithm, it is stipulated that the head moves in one direction to provide services, and when returning, it directly moves quickly to the starting end without serving any requests.

4. Detailed design (including main data structure, program flow chart, key code, etc.)

 

key code:

void FCFS()

{

  printf("First come first serve FCFS\n");

  printf("The next track to be accessed\t\t\ttrack number moving distance\n");

    int su=kai;

      sum=0;

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

      { if(su<s[i])

      s1[i]=s[i]-su;

      else

      s1[i]=su-s[i];

      su=s[i];

      sum+=s1[i];

      }

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

       {

         printf("\t%d\t\t\t\t\t%d\t\t\n",s[i],s1[i]);

       }

      printf("Seek length: %d\n", sum);

}

void SSTF()

{

      printf("Shortest seek SSTF:\n");

      printf("The next track to be accessed\t\t\ttrack number moving distance\n");

      int su=kai;

      int s2[100];

      sum=0;

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

      s2[i]=c1[i];

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

      s2[i+m]=c2[i];

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

      { if(su<s2[i])

      s1[i]=s2[i]-su;

      else

      s1[i]=su-s2[i];

      su=s2[i];

      sum+=s1[i];

      }

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

       {

         printf("\t%d\t\t\t\t\t%d\t\t\n",s2[i],s1[i]);

       }

       printf("Seek length: %d\n", sum);

    }

void SCAN()

{

     printf("Scan algorithm SCAN:\n");

     printf("The next track to be accessed: \t\t\t Track number moving distance:\n");

      int su=kai;

      int s2[100];

      sum=0;

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

      s2[i]  =c2[i];

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

      s2[i+n]=c1[i];

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

      { if(su<s2[i])

        s1[i]=s2[i]-su;

        else

        s1[i]=su-s2[i];

        su=s2[i];

        sum+=s1[i];

      }

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

       {

         printf("\t%d\t\t\t\t\t%d\t\t\n",s2[i],s1[i]);

       }

       printf("Seek length: %d\n", sum);

    }

void CSAN()

{

     printf("Loop scan CSAN:\n");

     printf("The next track to be accessed: \t\t\t Track number moving distance:\n");

      int su=kai;

      int j=0;

      int s2[100];

      sum=0;

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

      s2[i]  =c2[i];

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

      s2[j+n]=c1[i];

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

      { if(su<s2[i])

        s1[i]=s2[i]-su;

 else

        s1[i]=su-s2[i];

        su=s2[i];

        sum+=s1[i];

      }

      //am=am/am;

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

       {printf("\t%d\t\t\t\t\t%d\t\t\n",s2[i],s1[i]);

       }

       printf("Seek length: %d\n", sum);

        }

5. Experimental results and analysis

First create the track:

 

> After creating the track, select the algorithm and sort it. And calculate the order of the track to be accessed, the moving distance of the track number, and the length of the search.

 

 

6. Summary and experience

This experiment implements a simple disk scheduling algorithm simulation program. It provides the following functions:

Create track: The user can input the starting position of the track, the longest track number and the number of tracks, and check the legitimacy of the input.

First-come-first-served (FCFS): Access the tracks sequentially in the order of requests, and calculate the moving distance of the track number and the seek length.

Shortest seek (SSTF): Select the track closest to the current head position to access, and calculate the moving distance of the track number and the seek length.

Scanning Algorithm (SCAN): The magnetic head starts from the starting position and moves in one direction in turn until it reaches the extreme boundary, then changes direction, and calculates the moving distance of the track number and the seek length.

Cyclic Scanning Algorithm (CSCAN): The magnetic head starts from the starting position, moves in one direction in turn, until it reaches the limit, and then returns to the starting position, and calculates the moving distance of the track number and the seek length.

The program implements the basic disk scheduling algorithm, which can effectively simulate the disk access process and calculate the seek length.

*Different scheduling algorithms may have different performances in different disk access modes, and you can select the appropriate algorithm through multiple tests and comparisons.

*The code implementation is relatively simple, without adding error handling and exception handling, the code can be further improved to improve the stability and robustness of the program.

*Through the implementation of this program, you can better understand the principle and practical application of the disk scheduling algorithm, and have a deeper understanding of the disk management of the operating system.

Guess you like

Origin blog.csdn.net/CSH__/article/details/131382987