C-Look disk scheduling algorithm

" The C-Look disk scheduling algorithm is a disk scheduling technique designed to reduce seek time and improve overall disk I/O performance in computer systems. In C-Look, disk requests are serviced in a continuous cycle, moving the disk arm in a single direction until the last requested sector is reached, at which point the arm is immediately returned to the first sector without servicing any requests in the reverse direction."

Key Points:

                - C-Look is a variant of the Look disk scheduling algorithm designed to minimize seek time in disk I/O operations.
                - It features continuous movement of the disk arm in one direction until the last requested sector is reached.
                - Unlike Look, C-Look does not service requests in the reverse direction upon reaching the end of the disk, enhancing efficiency.
                - C-Look adopts a cyclic service pattern, servicing requests from the beginning to the end of the disk and then back to the beginning.
                - Its primary objective is to reduce seek time by minimizing the distance traveled by the disk arm to fulfill requests.
                - C-Look is particularly efficient when handling contiguous requests, as it services them without unnecessary movement in the opposite direction.
                - Requests that arrive after the disk arm has passed their location are queued and serviced in the subsequent cycle.
                - While offering simplicity and efficiency, C-Look may not always be the most optimal choice, especially in scenarios where request patterns vary significantly.

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

void CLOOK(vector& arr, int head) {
    int size = arr.size();
    int seek_count = 0;
    int distance, cur_track;
    vector left, right;
    vector seek_sequence;

    for (int i = 0; i < size; i++) {
        if (arr[i] < head)
            left.push_back(arr[i]);
        if (arr[i] > head)
            right.push_back(arr[i]);
    }

    std::sort(left.begin(), left.end());
    std::sort(right.begin(), right.end());

    for (int i = 0; i < right.size(); i++) {
        cur_track = right[i];
        seek_sequence.push_back(cur_track);
        distance = abs(cur_track - head);
        seek_count += distance;
        head = cur_track;
    }

    seek_count += abs(head - left[0]);
    head = left[0];

    for (int i = 0; i < left.size(); i++) {
        cur_track = left[i];
        seek_sequence.push_back(cur_track);
        distance = abs(cur_track - head);
        seek_count += distance;
        head = cur_track;
    }

    cout << "Total number of seek operations = " << seek_count << endl;
    cout << "Seek Sequence is" << endl;

    for (int i = 0; i < seek_sequence.size(); i++) {
        cout << seek_sequence[i] << endl;
    }
}

int main() {
    int size, head;
    cout << "Enter the number of requests: ";
    cin >> size;
    vector arr(size);
    cout << "Enter the requests: ";
    for (int i = 0; i < size; ++i) {
        cin >> arr[i];
    }
    cout << "Enter the initial position of the head: ";
    cin >> head;

    cout << "Initial position of head: " << head << endl;
    CLOOK(arr, head);

    return 0;
}
                            

CLOOK Disk Scheduling