C Program to Simulate LOOK Disk Scheduling Algorithm | Logic Explained

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. 

Basically, the disk starts reading at an initial head position which in the program, we denoted with a variable called headposition.
Then, in the real world applications, it moves towards either the outer edge of the disk or towards the inner cylinder of the disk. But for the sake of simplicity, in this program we are assuming that the disk always moves towards the outermost cylinder initially. 
So, let us assume that the initial headposition is at 50. And let [15, 75, 35, 85] be our queue of requests.
Then if we perform the LOOK disk scheduling algorithm on this, the disk head will initially move towards the outermost cylinder reading the requests on its way. 

Web Design for Beginners in Malayalam - Full Course (മലയാളം)

Then, a doubt might arise, that is what exactly is the difference between SCAN algorithm and LOOK algorithm. The actual difference here is that, in the case of SCAN algorithm, the disk head moves till it reaches the outermost cylinder even if there are no requests on the way and then moves towards the innermost cylinder reading all remaining requests on the way. 
 Whereas in the case of LOOK algorithm, it is similar to the SCAN algorithm but the disk head does not go all the way to the outermost cylinder. Instead it moves until the largest disk position which was in the request queue is read and then it starts moving towards the innermost cylinder but it doesn't go all the way until the innermost cylinder. Instead it stops at the least disk position which is requested. 

In this example above, if we assume that the Maximum range is 100 and the disk head starts at 50,

In the case of SCAN algorithm,

The reading sequence will be as follows:

50    75  →  85  → 100  → 35  → 15

In the case of LOOK algorithm,

The reading sequence will be as follows:

50 → 75 → 85 → 35 → 15

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.

int absoluteValue(int); // Declaring function absoluteValue

void main()
    int queue[25],n,headposition,i,j,k,seek=0, maxrange,
    float averageSeekTime;

    // Reading the maximum Range of the Disk. 
    printf("Enter the maximum range of Disk: ");
    // Reading the number of Queue Requests(Disk access requests)
    printf("Enter the number of queue requests: ");
    // Reading the initial head position.(ie. the starting point of execution)
    printf("Enter the initial head position: ");
    // 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
            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[]
    //Now we have to sort the two arrays
    //SORTING array queue1[] in ascending order
    //SORTING array queue2[] in descending order
    //Copying first array queue1[] into queue[]
    //Copying second array queue2[] after that first one is copied, into queue[]

    //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. 
    // 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)
        return x;
        return x*-1;

// Code by Nived Kannada


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


Nanogalaxy is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for website owners to earn advertising fees by advertising and linking to amazon.com and any other website that may be affiliated with Amazon Service LLC Associates Program.