Original
Previous blog wrote the shortest Seek first algorithm (SSTF)--Disk scheduling management: http://www.cnblogs.com/chiweiming/p/9073312.html
This article introduces the scanning algorithm (scan)-Disk scheduling management, which has similar fragments to the previous code, but is more difficult than the shortest path-seeking first algorithm.
(The topic is a blog post)
Randomly select a track number as the starting point to start the search, first from the track sequence to filter out the track number than the beginning of the track number, and then in this batch of track number to filter out
The smallest track number, accesses it, and continues with it as the starting point (the access track from the Inside out) until the maximum track number is accessed.
Filter the maximum track number in the track number that has not been accessed, and then start with it, filtering out the largest magnet from the remaining unused track number
Logos Access, and then use it as a starting point to continue the above operation (from the outside of the access track) until the full track is accessed.
#include <stdio.h>#include<math.h>#include<stdlib.h>#include<time.h>#defineMAX 50//Maximum number of tracks that can be accessed#defineN 20//number of track numbersintTrack[n];//A sequence of track numbers randomly generated to seek accessintNum_track[n];//record the distance of other tracks from the currently visited trackintTotal=0;//count the track numbers that have been accessedintall_track=0;//total number of tracks movedDoubleAver_track;//average number of seek pathsintff=0;//the ff==0 represents an outward scan, and the ==1 represents a scan from the outside.voidSCAN (intOrder) {//order is the track's currently accessed tracks subscriptprintf"%d", Track[order]); Num_track[order]=-1; Total++;//track number +1 has been accessed if(total==N) { return; } intI=0; for(i=0; i<=n-1; i++) {//calculate the distance from the other track to the currently visited track if(num_track[i]!=-1) {Num_track[i]=abs (track[order]-Track[i]); } } if(ff==0){//moving from the Inside Out intmin=999; intx=-1; for(i=0; i<=n-1; i++){ if(num_track[i]!=-1){ if(track[i]>=Track[order]) { if(num_track[i]<min) {//Select the smallest of the track numbers larger than the Track[order]min=Num_track[i]; X=i; } } } } if(x==-1){//X==-1 representative can not find the number of greater than or equal to Track[order], next time should be scanned from the outwardff=1; intmax=-999; intx; for(i=0; i<=n-1; i++) {//move from the outside, find the first inaccessible disk and scan it as a starting point if(num_track[i]!=-1){ if(track[i]>max) {Max=Track[i]; X=i; } }} all_track+=abs (track[order]-track[x]); SCAN (x); } Else{all_track+=abs (track[order]-track[x]); SCAN (x); } } Else{//moving from the outside. intmin=999; intx; for(i=0; i<=n-1; i++){ if(num_track[i]!=-1){ if(track[i]<=Track[order]) { if(num_track[i]<min) {min=Num_track[i]; X=i; } }}} All_track+=abs (track[order]-track[x]); SCAN (x); }}intMain () {intI=0; Srand (Time (0)); printf ("the sequence of track numbers is:"); for(i=0; i<=n-1; i++) {//randomly generates a sequence of track numbers for seeking accessTrack[i]=rand ()% (max+1); printf ("%d", Track[i]); } printf ("\ n"); printf ("the Seek sequence is:"); SCAN (rand ()%N);//randomly selects the starting trackprintf"\ nthe total number of tracks moved:%d\n", All_track); printf ("average seek total:%0.2lf",(Double) all_track/N); return 0;}
(Run Results section)
19:33:25
2018-05-22
Scanning algorithm (scan)--Disk scheduling management