Disk scheduling is done by operating systems to schedule I/O requests arriving for the disk and the algorithm used for the disk scheduling is called Disk Scheduling Algorithm.
In this post, we will discuss the LOOK Disk Scheduling Algorithm and also write a program for LOOK disk scheduling algorithm. It is similar to the SCAN disk scheduling algorithm except for the difference that the disk arm in spite of going to the end of the disk goes only to the last request to be serviced in front of the head and then reverses its direction from there only. Thus it prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.
We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of LOOK Disk Scheduling Algorithm in C++, although, it’s very similar to C.
INPUT:
The first line is the size of the disk (m).
The second line is the number of I/O requests (n).
The third line is an array of I/O requests (a[n]).
The fourth line is the head position (h).
OUTPUT:
Print the total head movement.
The following is the LOOK disk scheduling program in C++.
#include<bits/stdc++.h> using namespace std; int main(){ int i,j,k,n,m,sum=0,x,y,h; cout<<"Enter the size of disk\n"; cin>>m; cout<<"Enter number of requests\n"; cin>>n; cout<<"Enter the requests\n"; vector <int> a(n),l; for(i=0;i<n;i++){ cin>>a[i]; } for(i=0;i<n;i++){ if(a[i]>m){ cout<<"Error, Unknown position\n"; return 0; } } cout<<"Enter the head position\n"; cin>>h; a.push_back(h); sort(a.begin(),a.end()); for(i=0;i<a.size();i++){ if(h==a[i]) break; } k=i; if(k<n/2){ for(i=k;i<a.size();i++){ l.push_back(a[i]); } for(i=k-1;i>=0;i--){ l.push_back(a[i]); } } else{ for(i=k;i>=0;i--){ l.push_back(a[i]); } for(i=k+1;i<a.size();i++){ l.push_back(a[i]); } } int temp=l[0]; cout<<temp; for(i=1;i<l.size();i++){ cout<<" -> "<<l[i]<<' '; sum+=abs(l[i]-temp); temp=a[i]; } cout<<'\n'; cout<<"Total head movements = "<< sum<<'\n'; return 0; }
OUTPUT:
Enter the size of disk 199 Enter number of requests 8 Enter the requests 98 183 37 122 14 124 65 67 Enter the head position 53 53 -> 65 -> 67 -> 98 -> 122 -> 124 -> 183 -> 37 -> 14 Total head movements = 481
Other disk scheduling algorithms:
- FCFS Disk Scheduling
- Shortest Seek Time First Disk Scheduling
- SCAN Disk Scheduling
- CLOOK Disk Scheduling
- LOOK Disk Scheduling
- CSCAN Disk Scheduling
Let us know in the comments if you are having any questions regarding this LOOK Disk Scheduling Algorithm.
And if you found this post helpful, then please help us by sharing this post with your friends. Thank You