Experiment 7 Simulation and implementation of disk scheduling algorithm

Posted by gfX on Tue, 21 Dec 2021 16:55:48 +0100

1. Experimental purpose

(1) Understand the disk structure and how the data on the disk is organized.
(2) Master the calculation method of disk access time.
(3) Master common disk scheduling algorithms and their related characteristics.

2. Basic knowledge and principle of experiment

(1) Organization of disk data
Each physical record on the disk has a unique address, which includes three parts: head number (disk number) and cylinder number (magnetic number)
Road code) and sector code. Given these three quantities, an address can be uniquely determined.
(2) How disk access time is calculated
The disk rotates at a constant rate during operation. To ensure reading or writing, the head must move to the required track when required
Start reading or writing data when the start position of the sector is rotated under the head. The access time to the disk includes seek time and rotation delay
Delay time and transmission time.
(3) Disk scheduling algorithm
The purpose of disk scheduling is to reduce the seek time of disk as much as possible to improve the performance of disk I/O system.
FIFO algorithm: scheduling according to the arrival order of access requests.
Shortest service time first algorithm: give priority to the disk I/O requests that make the head arm move the least from the current position for scheduling.
SCAN (elevator algorithm): the head arm is required to move in one direction first and meet all outstanding requests on the way until it
The latter improvement is sometimes referred to as reaching the last track in this direction, or until there are no other requests in this direction
LOOK strategy. Then reverse the service direction, scan in the opposite direction, and complete all requests in the same order.
C-SCAN (cyclic scan) algorithm: during disk scheduling, the scan is limited to one direction. When the most important data is accessed along a certain direction
In the latter track, the head arm returns to the other end of the disk and starts scanning again.

3. Experimental content

This experiment realizes several common disk scheduling algorithms through programming simulation.

c + + implementation:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
/*
2021-12-16 @Vast
 Track simulation algorithm
*/
void  FIFO(vector<int> v, int  currentLocation)
{
    int x,sum=0;
    float len = v.size();
    cout<<"\nFIFO Algorithm:\n"<<"Access track order:";
    for(int i=0; i<len; i++)
    {
        cout<<v[i]<<" ";
    }
    cout<<"\n Number of tracks spanned:";
    for(int i=0; i<len; i++ )
    {
        x=abs(currentLocation-v[i]);
        cout<<x<<" ";
        sum+= x;
        currentLocation=v[i];
    }
    cout<<"\n Average seek length:"<<(sum/len)<<endl;
    return ;
}

void  SSTF(vector<int> v, int  currentLocation)
{
    int x,j=0,sum=0;
    int a[1000] = {0}; //Record the number of tracks spanned
    bool arr[100]= {false} ; //Flag array to record whether the track has completed seek. The initial values are all false
    int m=0;//Number of completed seek
    int index ;  //Record corner mark position

    float len = v.size();
    cout<<"\nSSTF Algorithm:\n"<<"Access track order:";
    while(m<len)
    {
        int min_length=INT_MAX;//Reset the minimum track distance before each cycle
        for(int i=0; i<len; i++)
        {
            if(arr[i]==true)continue;
            else
            {
                x=abs(currentLocation-v[i]);
                if(x<min_length)
                {
                    min_length=x;
                    index = i;
                }
            }

        }
        a[j]=abs(currentLocation-v[index]);
        sum+=a[j] ;
        j++;
        arr[index]=true;
        currentLocation=v[index];
        cout<<v[index]<<" ";
        m++;
    }
    cout<<"\n Number of tracks spanned:";
    for(int j=0; j<len; j++)
    {
        cout<<a[j]<<" ";
    }
    cout<<"\n Average seek length:"<<(sum/len)<<endl;
    return ;
}

void SCAN(vector<int> v, int  currentLocation)
{
    int x,j=0,sum=0;
    float len = v.size();
    int index=0 ;  //Record corner mark position
    int a[1000]={0}; //Record the number of tracks spanned

    sort(v.begin(),v.end());
    for(int i=0; i<len ; i++)
    {
        if(v[i]<currentLocation)index++;
        else break;
    }
    v.insert(v.begin()+index,currentLocation);
    vector<int>::iterator iter;
    cout<<"\nSCAN Algorithm:\n";
    cout<<"Please enter the initial direction of access: 1 for increment, 0 for decrement:";
    cin>>x;
    cout<<"Access track order:\n";
    if(x==1)
    {
        for(iter=v.begin()+index+1; iter!=v.end(); iter++)
        {
            cout<<*iter<<" ";
            a[j]=abs(*iter-currentLocation);
            sum+=a[j] ; j++;
            currentLocation=*iter;
        }
        for(iter=v.begin()+index-1; iter!=v.begin()-1; iter--)
        {
            cout<<*iter<<" ";
            a[j]=abs(*iter-currentLocation);
            sum+=a[j] ; j++;
            currentLocation=*iter;
        }
    }
    else if(x==0)
    {
        for(iter=v.begin()+index-1; iter!=v.begin()-1; iter--)
        {
            cout<<*iter<<" ";
            a[j]=abs(*iter-currentLocation);
            sum+=a[j] ; j++;
            currentLocation=*iter;
        }
        for(iter=v.begin()+index+1; iter!=v.end(); iter++)
        {
            cout<<*iter<<" ";
            a[j]=abs(*iter-currentLocation);
            sum+=a[j] ; j++;
            currentLocation=*iter;
        }
    }
    cout<<"\n Number of tracks across:";
    for(int i =0; i<len;i++){
        cout<<a[i]<<" ";
    }
    cout<<"\n Average seek length:"<<(sum/len)<<endl;
}
z
void  C_SCAN(vector<int> v, int  currentLocation)
{
    int x,j=0,sum=0;
    int a[1000]={0};
    float len = v.size();
    int index=0 ;  //Record corner mark position

    sort(v.begin(),v.end());
    for(int i=0; i<len ; i++)
    {
        if(v[i]<currentLocation)index++;
        else break;
    }
    v.insert(v.begin()+index,currentLocation);
    vector<int>::iterator iter;
    cout<<"\nC_SCAN Algorithm:\n";
    cout<<"Please enter the initial direction of access: 1 for increment, 0 for decrement:";
    cin>>x;
    cout<<"Access track order:\n";
    if(x==1)
    {
        for(iter=v.begin()+index+1; iter!=v.end(); iter++)
        {
            cout<<*iter<<" ";
            a[j]=abs(*iter-currentLocation);
            sum+=a[j] ; j++;
            currentLocation=*iter;
        }
        for(iter=v.begin(); iter!=v.begin()+index; iter++)
        {
            cout<<*iter<<" ";
            a[j]=abs(*iter-currentLocation);
            sum+=a[j] ; j++;
            currentLocation=*iter;
        }
    }
    else if(x==0)
    {
        for(iter=v.begin()+index-1; iter!=v.begin()-1; iter--)
        {
            cout<<*iter<<" ";
            a[j]=abs(*iter-currentLocation);
            sum+=a[j] ; j++;
            currentLocation=*iter;
        }
        for(iter=v.end(); iter!=v.begin()+index; iter--)
        {
            cout<<*iter<<" ";
            a[j]=abs(*iter-currentLocation);
            sum+=a[j] ; j++;
            currentLocation=*iter;
        }
    }
    cout<<"\n Number of tracks across:";
    for(int i =0; i<len;i++){
        cout<<a[i]<<" ";
    }
    cout<<"\n Average seek length:"<<(sum/len)<<endl;
}

vector<int >  v;
int main()
{
    float n;
    int diskNum = 0;//Total number of tracks
    int firstLocation;//Head initial position

    cout << "Disk scheduling algorithm" << endl;
    cout<<"Total number of input tracks:";
    cin>>diskNum;
    cout<<"Input head initial position:";
    cin>>firstLocation;
    cout<<"Enter the number of data to look for:";
    cin>>n;
    cout<<"Enter the track sequence to look for:";
    int x;
    int i = 0 ;
    int m=n;
    while(m--)
    {
        cin>>x;
        v.push_back(x);
        i++;
    }
    FIFO(v,firstLocation);
    SSTF(v,firstLocation);
    SCAN(v,firstLocation);
    C_SCAN(v,firstLocation);
    return 0;
}

Topics: C++ Algorithm Operating System