There are many disk scheduling algorithms. LOOK is one of them. It is an efficient and simple disk scheduling algorithm. In this article, we will see how to write a C program to simulate the LOOK Disk Scheduling algorithm. The code is actually very similar to that of the SCAN Disk Scheduling algorithm C program that we have published earlier. There is only a few small tweaks required to make it act as the LOOK algorithm. So, let's first understand how the LOOK algorithm works.
Web Design for Beginners in Malayalam - Full Course (മലയാà´³ം)
WATCH VIDEO
In the case of SCAN algorithm,
The reading sequence will be as follows:
In the case of LOOK algorithm,
The reading sequence will be as follows:
You can clearly see the difference here. That is, the disk head doesn't have to unnecessarily move to the extreme ends (outermost cylinder and innermost cylinder) for no reason. Therefore, the LOOK algorithm is considered to be a slightly more efficient version of the SCAN disk scheduling algorithm.
Now let's see the C program to implement this.
Don't worry about understanding the code logic because I have tried my best to comment out literally every single line of code in order to make the logic easy to understand.
What we are doing here is that we read the maxrange(not necessary in this case but we still read it to make the code adaptable to other scheduling algorithms), initial headposition and then the request queue.
We simply perform a condition check when reading the request queue and put it into two different arrays called queue1 and queue2. Any value greater than initial headposition goes to queue1 and any value less than headposition goes to queue2. And then we simply sort these arrays so that the queue1 array is in ascending order since we have to move the disk head towards the outer end and queue2 in the descending order.
This is basically the logic of this code. Now you may read the comments on the code itself to understand the rest of it.
Open in OnlineGDB
// C Program to Simulate LOOK Disk Scheduling Algorithm //visit www.nanogalaxy.org for more programs. #include<stdio.h> int absoluteValue(int); // Declaring function absoluteValue void main() { int queue[25],n,headposition,i,j,k,seek=0, maxrange, difference,temp,queue1[20],queue2[20],temp1=0,temp2=0; float averageSeekTime; // Reading the maximum Range of the Disk. printf("Enter the maximum range of Disk: "); scanf("%d",&maxrange); // Reading the number of Queue Requests(Disk access requests) printf("Enter the number of queue requests: "); scanf("%d",&n); // Reading the initial head position.(ie. the starting point of execution) printf("Enter the initial head position: "); scanf("%d",&headposition); // Reading disk positions to be read in the order of arrival printf("Enter the disk positions to be read(queue): "); for(i=1;i<=n;i++) // Note that i varies from 1 to n instead of 0 to n-1 { scanf("%d",&temp); //Reading position value to a temporary variable //Now if the requested position is greater than current headposition, //then pushing that to array queue1 if(temp>headposition) { queue1[temp1]=temp; //temp1 is the index variable of queue1[] temp1++; //incrementing temp1 } else //else if temp < current headposition,then push to array queue2[] { queue2[temp2]=temp; //temp2 is the index variable of queue2[] temp2++; } } //Now we have to sort the two arrays //SORTING array queue1[] in ascending order for(i=0;i<temp1-1;i++) { for(j=i+1;j<temp1;j++) { if(queue1[i]>queue1[j]) { temp=queue1[i]; queue1[i]=queue1[j]; queue1[j]=temp; } } } //SORTING array queue2[] in descending order for(i=0;i<temp2-1;i++) { for(j=i+1;j<temp2;j++) { if(queue2[i]<queue2[j]) { temp=queue2[i]; queue2[i]=queue2[j]; queue2[j]=temp; } } } //Copying first array queue1[] into queue[] for(i=1,j=0;j<temp1;i++,j++) { queue[i]=queue1[j]; }s //Copying second array queue2[] after that first one is copied, into queue[] for(i=temp1+1,j=0;j<temp2;i++,j++) { queue[i]=queue2[j]; } //At this point, we have the queue[] with the requests in the //correct order of execution as per LOOK algorithm. //Now we have to set 0th index of queue[] to be the initial headposition. queue[0]=headposition; // Calculating SEEK TIME. seek is initially set to 0 in the declaration part. for(j=0; j<n; j++) //Loop starts from headposition. (ie. 0th index of queue) { // Finding the difference between next position and current position. difference = absoluteValue(queue[j+1]-queue[j]); // Adding difference to the current seek time value seek = seek + difference; // Displaying a message to show the movement of disk head printf("Disk head moves from position %d to %d with Seek %d \n", queue[j],
queue[j+1], difference); } // Calculating Average Seek time averageSeekTime = seek/(float)n; //Display Total and Average Seek Time(s) printf("Total Seek Time= %d\n", seek); printf("Average Seek Time= %f\n", averageSeekTime); } // Defining function absoluteValue int absoluteValue(int x) { if(x>0) { return x; } else { return x*-1; } } // END OF CODE // Code by Nived Kannada
Output
I hope you found this article helpful! If you did, please let me know your opinions in the comments below. Happy Coding!
No comments:
Post a Comment