Articles by "Disk Scheduling Algorithm"
Showing posts with label Disk Scheduling Algorithm. Show all posts

C-LOOK Disk Scheduling Algorithm-

  • Circular-LOOK Algorithm is an improved version of the LOOK Algorithm.
  • Head starts from the first request at one end of the disk and moves towards the last request at  the other end servicing all the requests in between.
  • After reaching the last request at the other end, head reverses its direction.
  • It then returns to the first request at the starting end without servicing any request in between.
  • The same process repeats.


Advantages-

  • It does not causes the head to move till the ends of the disk when there are no requests to be serviced.
  • It reduces the waiting time for the cylinders just visited by the head.
  • It provides better performance as compared to LOOK Algorithm.
  • It does not lead to starvation.
  • It provides low variance in response time and waiting time.

Disadvantages-

  • There is an overhead of finding the end requests.


C++ program for C-LOOK Disk Scheduling Algorithm

#include <iostream>

#include <stdlib.h>

using namespace std;

class clook_disk

{

    int ref[100];

    int ttrk, cur, size, prev;

    int sort();

public:
    void getdata();

    void total_move();
};

int clook_disk::sort()

{

    int temp;

    for (int i = 0; i < size - 1; i++)

        for (int y = 0; y < size - 1; y++)

            if (ref[y] > ref[y + 1])

            {

                temp = ref[y];

                ref[y] = ref[y + 1];

                ref[y + 1] = temp;
            }

    for (int i = 0; i < size; i++)

        if (ref[i] > cur)

            return i;

    return size;
}

void clook_disk::getdata()

{

    cout << "Enter total number of tracks : ";

    cin >> ttrk;

    ttrk--;

    cout << "Enter the current position of head : ";

    cin >> cur;

    cout << "Enter previous position of head : ";

    cin >> prev;

    cout << "Enter the size of queue : ";

    cin >> size;

    cout << "Enter the request for tracks : ";

    for (int i = 0; i < size; i++)

        cin >> ref[i];
}

void clook_disk::total_move()

{

    int num = cur, move = 0, ind, dir = cur - prev;

    ind = sort();

    if (dir > 0)

    {

        for (int i = ind; i < size; i++)

        {

            move += ref[i] - num;

            num = ref[i];
        }

        if (ind != 0)

        {

            num = ref[0];

            cout << move << " ";

            for (int i = 0; i <= ind - 1; i++)

            {

                move += ref[i] - num;

                num = ref[i];
            }
        }
    }

    else

    {

        for (int i = ind - 1; i >= 0; i--)

        {

            move += num - ref[i];

            num = ref[i];
        }

        num = ref[size - 1];

        for (int i = size - 2; i >= ind; i--)

        {

            move += num - ref[i];

            num = ref[i];
        }
    }

    cout << "Total head movements : " << move;
}

int main()

{

    clook_disk clook;

    clook.getdata();

    clook.total_move();

    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming

Look Disk Scheduling Algorithm

  • LOOK Algorithm is an improved version of the SCAN Algorithm.
  • Head starts from the first request at one end of the disk and moves towards the last request at the other end servicing all the requests in between.
  • After reaching the last request at the other end, the head reverses its direction.
  • It then returns to the first request at the starting end servicing all the requests in between.
  • The same process repeats.


C++ program for LOOK Disk Scheduling Algorithm

#include <iostream>

#include <stdlib.h>

using namespace std;

class look_disk

{

    int ref[100];

    int ttrk, cur, size, prev;

    int sort();

public:
    void getdata();

    void total_move();
};

int look_disk::sort()

{

    int temp;

    for (int i = 0; i < size - 1; i++)

        for (int y = 0; y < size - 1; y++)

            if (ref[y] > ref[y + 1])

            {

                temp = ref[y + 1];

                ref[y + 1] = ref[y];

                ref[y] = temp;
            }

    for (int i = 0; i < size; i++)

        if (ref[i] > cur)

            return i;

    return size;
}

void look_disk::getdata()

{

    cout << "Enter total number of tracks : ";

    cin >> ttrk;

    ttrk--;

    cout << "Enter the current position of head : ";

    cin >> cur;

    cout << "Enter previous position of head : ";

    cin >> prev;

    cout << "Enter the size of queue : ";

    cin >> size;

    cout << "Enter the request for tracks : ";

    for (int i = 0; i < size; i++)

        cin >> ref[i];
}

void look_disk::total_move()

{

    int num = cur, move = 0, ind, dir = cur - prev;

    ind = sort();

    if (dir > 0)

    {

        for (int i = ind; i < size; i++)

        {

            move += ref[i] - num;

            num = ref[i];
        }

        if (ind != 0)

        {

            if (ind == size)

                move += num - ref[ind - 1];

            else

                move += ref[size - 1] - ref[ind - 1];

            num = ref[ind - 1];

            for (int i = ind - 2; i >= 0; i--)

            {

                move += num - ref[i];

                num = ref[i];
            }
        }
    }

    else

    {

        for (int i = ind - 1; i >= 0; i--)

        {

            move += num - ref[i];

            num = ref[i];
        }

        if (ind == 0)

            move += ref[ind] - num;

        else if (ind != size)

            move += ref[ind] - ref[0];

        num = ref[ind];

        for (int i = ind + 1; i < size; i++)

        {

            move += ref[i] - num;

            num = ref[i];
        }
    }

    cout << "Total head movements : " << move;
}

int main()

{

    look_disk look;

    look.getdata();

    look.total_move();

    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming

Introduction to SSTF disk scheduling :

SSTF stands for Shortest Time First which very uses full of learning about how the disk drive manages the data having the shortest seek time.



Algorithm of the SSTF is given below:-

  • Find the positive distance of all tracks in the request array from the head.
  • Find a track from the requested array which has not been accessed/serviced yet and has a minimum distance from the head.
  • Increment the total seek count with this distance.
  • Currently serviced track position now becomes the new head position.
  • Go to step 2 until all tracks in request array have not been serviced.

C program for Shortest Seek Time Next Disk Scheduling Algorithm 



#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
    int queue[100], t[100], head, seek = 0, n, i, j, temp;
    float avg;
    // clrscr();
    printf("*** SSTF Disk Scheduling Algorithm ***\n");
    printf("Enter the size of Queue\t");
    scanf("%d", &n);
    printf("Enter the Queue\t");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &queue[i]);
    }
    printf("Enter the initial head position\t");
    scanf("%d", &head);
    for (i = 1; i < n; i++)
        t[i] = abs(head - queue[i]);
    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (t[i] > t[j])
            {
                temp = t[i];
                t[i] = t[j];
                t[j] = temp;
                temp = queue[i];
                queue[i] = queue[j];
                queue[j] = temp;
            }
        }
    }
    for (i = 1; i < n - 1; i++)
    {
        seek = seek + abs(head - queue[i]);
        head = queue[i];
    }
    printf("\nTotal Seek Time is%d\t", seek);
    avg = seek / (float)n;
    printf("\nAverage Seek Time is %f\t", avg);
    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming


Introduction to First Come First Serve (FCFS) disk scheduling :

                    The simplest form of disk scheduling is, of course, the first-come, first-served (FCFS) algorithm. This algorithm is intrinsically fair, but it generally does not provide the fastest service. Consider, for example, a disk queue with requests for I/O to blocks on cylinders 98, 183, 37, 122, 14, 124, 65, 67. If the disk head is initially at cylinder 53, it will first move from 53 to 98, then to 183, 37, 122, 14, 124, 65, and finally to 67, for a total head movement of 640 cylinders.



C program for First Come First Serve (FCFS) disk scheduling algorithm


#include <stdio.h>

#include <math.h>

int main()

{

    int queue[20], n, head, i, j, k, seek = 0, max, diff;

    float avg;

    printf("Enter the max range of disk\n");

    scanf("%d", &max);

    printf("Enter the size of queue request\n");

    scanf("%d", &n);

    printf("Enter the queue of disk positions to be read\n");

    for (i = 1; i <= n; i++)

        scanf("%d", &queue[i]);

    printf("Enter the initial head position\n");

    scanf("%d", &head);

    queue[0] = head;

    for (j = 0; j <= n - 1; j++)

    {

        diff = abs(queue[j + 1] - queue[j]);

        seek += diff;

        printf("Disk head moves from %d to %d with seek %d\n", queue[j],
        queue[j + 1], diff);
    }

    printf("Total seek time is %d\n", seek);

    avg = seek / (float)n;

    printf("Average seek time is %f\n", avg);

    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming



Introduction to SCAN disk scheduling :

                   In the SCAN algorithm, the disk arm starts at one end of the disk and moves toward the other end, servicing requests as it reaches each cylinder, until it gets to the other end of the disk. At the other end, the direction of the head movement is reversed, and servicing continues. The head continuously scans back and forth across the disk. The SCAN algorithm is sometimes called the elevator algorithm, since the disk arm behaves just like an elevator in a building, first servicing all the requests going up and then reversing to service requests the other way.



Different Types of Disk Scheduling Algorithm:

Different types of Disk Scheduling Algorithm

C Program for SCAN disk scheduling algorithm


#include <stdio.h>

#include <math.h>

int main()

{

    int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20],
    queue2[20], temp1 = 0, temp2 = 0;

    float avg;

    printf("Enter the max range of disk\n");

    scanf("%d", &max);

    printf("Enter the initial head position\n");

    scanf("%d", &head);

    printf("Enter the size of queue request\n");

    scanf("%d", &n);

    printf("Enter the queue of disk positions to be read\n");

    for (i = 1; i <= n; i++)

    {

        scanf("%d", &temp);

        if (temp >= head)

        {

            queue1[temp1] = temp;

            temp1++;
        }

        else

        {

            queue2[temp2] = temp;

            temp2++;
        }
    }

    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;
            }
        }
    }

    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;
            }
        }
    }

    for (i = 1, j = 0; j < temp1; i++, j++)

        queue[i] = queue1[j];

    queue[i] = max;

    for (i = temp1 + 2, j = 0; j < temp2; i++, j++)

        queue[i] = queue2[j];

    queue[i] = 0;

    queue[0] = head;

    for (j = 0; j <= n + 1; j++)

    {

        diff = abs(queue[j + 1] - queue[j]);

        seek += diff;

        printf("Disk head moves from %d to %d with seek %d\n", queue[j],
        queue[j + 1], diff);
    }

    printf("Total seek time is %d\n", seek);

    avg = seek / (float)n;

    printf("Average seek time is %f\n", avg);

    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming



Introduction to C-SCAN disk scheduling :

                   Circular SCAN (C-SCAN) scheduling is a variant of SCAN designed to provide a more uniform wait time. Circular-SCAN The algorithm is an improved version of the SCAN Algorithm.




Different Types of Disk Scheduling Algorithm:

Different types of Disk Scheduling Algorithm


Here are some steps followed to use C-SCAN Algorithm: -

  • Like SCAN, C-SCAN moves the head from one end of the disk to the other, servicing requests along the way.
  • After reaching the other end, the head reverses its direction. 
  • It then returns to the starting end without servicing any request in between.
  • This process repeats time and again until the scanning is completed.

The C-SCAN scheduling algorithm essentially Treats the cylinders as a circular list that wraps around from the final cylinder to the first one.


Here are Some Advantages:-

  • The waiting time for the cylinders just visited by the head is reduced as compared to the SCAN Algorithm.
  • It provides uniform waiting time and better response time.

Here are Some Disadvantages:-

  • It causes more seek movements as compared to SCAN Algorithm.
  • It causes the head to move till the end of the disk even if there are no requests to be serviced.

Also Read: Disk Scheduling For Shortest Seek Time First (SSTF)


C Program for C-SCAN Disk Scheduling Algorithm


#include <stdio.h>

#include <math.h>

int main()

{

    int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20],
    queue2[20], temp1 = 0, temp2 = 0;

    float avg;

    printf("Enter the max range of disk\n");

    scanf("%d", &max);

    printf("Enter the initial head position\n");

    scanf("%d", &head);

    printf("Enter the size of queue request\n");

    scanf("%d", &n);

    printf("Enter the queue of disk positions to be read\n");

    for (i = 1; i <= n; i++)

    {

        scanf("%d", &temp);

        if (temp >= head)

        {

            queue1[temp1] = temp;

            temp1++;
        }

        else

        {

            queue2[temp2] = temp;

            temp2++;
        }
    }

    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;
            }
        }
    }

    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;
            }
        }
    }

    for (i = 1, j = 0; j < temp1; i++, j++)

        queue[i] = queue1[j];

    queue[i] = max;

    queue[i + 1] = 0;

    for (i = temp1 + 3, j = 0; j < temp2; i++, j++)

        queue[i] = queue2[j];

    queue[0] = head;

    for (j = 0; j <= n + 1; j++)

    {

        diff = abs(queue[j + 1] - queue[j]);

        seek += diff;

        printf("Disk head moves from %d to %d with seek %d\n", queue[j],
        queue[j + 1], diff);
    }

    printf("Total seek time is %d\n", seek);

    avg = seek / (float)n;

    printf("Average seek time is %f\n", avg);

    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming