Common disk scheduling algorithms for operating systems

0 Preface

When I read ClickHouse related books recently, I involved some knowledge of disks and file systems. I reviewed the operating system once again, which is hereby recorded.

1 Basic knowledge

As shown in the figure below, ordinary disks are composed of disk spindles, disks, magnetic heads, and read-write heads. When reading and writing on the disk, the disk rotates at a high speed, and the read-write head reaches the corresponding position for operation, so it is time-consuming to read and write the disk.
Insert picture description here
This time consists of the following parts:

  1. Wait for the device to be available and the channel to be available
  2. Seek time (most time consuming)
  3. Disk rotation time
  4. Read and write time

The disk scheduling algorithm is optimized for seek time.

2 Scheduling algorithm

2.1 First come first served (FIFO)

This algorithm is well understood, that is, all processes are treated fairly, and each time a request is reached, it is processed from the current position. Insert picture description here
The above figure is an example. According to the disk access sequence and initial position, the total distance of the head movement is 640. Interested readers can do the calculations by themselves.

2.2 Short time priority (SSTF)

The core of this algorithm is that each time the head selects the request closest to the current position for processing. This algorithm can get better throughput, but it cannot guarantee the shortest average seek time. The disadvantage is that the response opportunity to the user's service request is not equal, which results in a large variation in response time. In the case of many service requests, requests for inner and outer edge tracks will be delayed indefinitely, and the response time of some requests will be unpredictable.
Take the figure in 2.1 as an example, the total distance the head moves is 236.

2.3 Elevator Algorithm (SCAN)

The execution of the elevator algorithm is a bit like an elevator in real life. It scans in one direction and then reverses the direction to scan again. Take the picture in 2-1 as an example, scan to the left from 53, process 37, 14, and then reverse process 65, 67...
This algorithm basically overcomes the shortest seek time priority algorithm. The service is concentrated on the middle track and response The shortcoming of relatively large time change, and the advantage of the shortest seek time priority algorithm, that is, the throughput is larger and the average response time is smaller. However, due to the swing scanning method, the frequency of access to the tracks on both sides is still lower than that of the middle track. .

2.4 Cyclic Scan Algorithm (CSCAN)

The cyclic scanning algorithm is an improvement of the scanning algorithm. If the access requests to the tracks are evenly distributed, when the head reaches one end of the disk and moves in the reverse direction, there are relatively few access requests that fall behind the head. This is because these tracks have just been processed, and the request density at the other end of the disk is quite high, and these access requests wait for a long time. In order to solve this situation, the cyclic scanning algorithm stipulates that the magnetic head moves in one direction. For example, it only moves from the inside to the outside. When the head moves to the outermost track to be visited, the head immediately returns to the innermost track to be visited, that is, the smallest track number is followed by the largest track number to form a loop for scanning.

Guess you like

Origin blog.csdn.net/MoonWisher_liang/article/details/115297012