- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- Operating System Processes
- Process Control Block
- Operations on Processes
- Inter Process Communication
- Context Switching
- Multi-threading
- Scheduling Algorithms
- Process Scheduling
- Preemptive and Non-Preemptive Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- Turn Around Time & Waiting Time
- Burst Time in SJF Scheduling
- Process Synchronization
- Process Synchronization
- Critical Section Problem
- Critical Section Synchronization
- Mutual Exclusion Synchronization
- Semaphores
- Counting Semaphores
- Mutex
- Turn Variable
- Bounded Buffer Problem
- Reader Writer Locks
- Test and Set Lock
- Peterson's Solution
- Monitors
- Sleep and Wake
- Race Condition
- OS Deadlock
- Introduction to Deadlock
- Conditions for Deadlock
- Deadlock Handling
- Deadlock Prevention
- Deadlock Avoidance (Banker's Algorithm)
- Deadlock Detection and Recovery
- Deadlock Ignorance
- Memory Management
- Memory Management
- Contiguous Memory Allocation
- Non-Contiguous Memory Allocation
- First Fit Algorithm
- Next Fit Algorithm
- Best Fit Algorithm
- Worst Fit Algorithm
- Fragmentation
- Virtual Memory
- Segmentation
- Buddy System
- Slab Allocation
- Overlays
- Paging and Page Replacement
- Paging
- Demand Paging
- Page Table
- Page Replacement Algorithms
- Optimal Page Replacement Algorithm
- Belady's Anomaly
- Thrashing
- Storage and File Management
- File Systems
- File Attributes
- Structures of Directory
- Linked Index Allocation
- Indexed Allocation
- Disk Scheduling Algorithms
- FCFS Disk Scheduling
- SSTF Disk Scheduling
- SCAN Disk Scheduling
- LOOK Disk Scheduling
- I/O Systems
- I/O Hardware
- I/O Software
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
Operating System - LOOK Disk Scheduling
The disk scheduling algorithms are used to determine the order in which input and output (I/O) requests of the disk are to be processed. In this chapter, we will discuss the LOOK and C-LOOK disk scheduling algorithm, with examples, and practice questions.
- LOOK Disk Scheduling Algorithm
- C-LOOK Disk Scheduling Algorithm
- Implementation of LOOK and C-LOOK Algorithms
LOOK Disk Scheduling Algorithm
The LOOK disk scheduling is an optimization on top of the SCAN disk scheduling algorithm. In the LOOK algorithm, the disk arm does not go to the end of the disk if there are no requests to be served in that direction. Meaning, the disk arm will reverse its direction as soon as it reaches the last request in the current direction.
Algorithm Steps
- Start from the current head position.
- Decide the direction of movement (left or right) by checking the nearest request.
- Move in the chosen direction and serve all the requests until the last request in that direction is reached.
- Reverse the direction and serve all the requests on the way back until the starting position is reached.
Example of LOOK Disk Scheduling
Consider a disk with 200 cylinders numbered from 0 to 199. The current head position is at cylinder 70, and the requests are for cylinders 82, 170, 43, 140, 34, and 190. Assume that the head is moving towards the right direction. The image below shows how the disk head will move to serve the requests using the LOOK algorithm.
The total head movement can be calculated as follows −
$$\mathrm{\text{Initial Position of Head} = 70}$$
$$\mathrm{\text{Requests} = [82, 170, 43, 140, 34, 190]}$$
$$\mathrm{\text{Move from 70 to 190} = 190 - 70 = 120}$$
$$\mathrm{\text{Move from 190 to 34} = 190 - 34 = 156}$$
$$\mathrm{\text{Total Head Movement} = 120 + 156 = 276}$$
C-LOOK Disk Scheduling Algorithm
The C-LOOK disk scheduling is the further optimization of the LOOK disk scheduling algorithm. In the C-LOOK algorithm, the disk arm will jump to the other end of the disk when it reaches the last request in the current direction. Meaning there is no revering of direction, as the disk is assumed to be circular.
Algorithm Steps
- Start from the current head position.
- Decide the direction of movement (left or right) by checking the nearest request.
- Move in the chosen direction and serve all the requests until the last request in that direction is reached.
- Jump to the other end of the disk and continue serving requests in the same direction until the starting position is reached.
Example of C-LOOK Disk Scheduling
Consider a disk with 200 cylinders numbered from 0 to 199. The current head position is at cylinder 70, and the requests are for cylinders 82, 170, 43, 140, 34, and 190. Assume that the head is moving towards the right direction. The image below shows how the disk head will move to serve the requests using the C-LOOK algorithm.
The total head movement can be calculated as follows −
$$\mathrm{\text{Initial Position of Head} = 70}$$
$$\mathrm{\text{Requests} = [82, 170, 43, 140, 34, 190]}$$
$$\mathrm{\text{Move from 70 to 190} = 190 - 70 = 120}$$
$$\mathrm{\text{Jump from 190 to 34} = 190 - 34 = 156}$$
$$\mathrm{\text{Move from 34 to 43} = 43 - 34 = 9}$$
$$\mathrm{\text{Total Head Movement} = 120 + 156 + 9 = 285}$$
Implementation of LOOK and C-LOOK Algorithms
The implementation of LOOK and C-LOOK disk scheduling algorithms is given below in C++
def look_disk_scheduling(requests, head, direction):
requests.sort()
total_head_movement = 0
index = 0
for i in range(len(requests)):
if head < requests[i]:
index = i
break
else:
index = len(requests)
if direction == "left":
left = requests[:index]
right = requests[index:]
left.reverse()
for r in left:
total_head_movement += abs(head - r)
head = r
for r in right:
total_head_movement += abs(head - r)
head = r
elif direction == "right":
left = requests[:index]
right = requests[index:]
for r in right:
total_head_movement += abs(head - r)
head = r
left.reverse()
for r in left:
total_head_movement += abs(head - r)
head = r
return total_head_movement
def clook_disk_scheduling(requests, head, direction):
requests.sort()
total_head_movement = 0
index = 0
for i in range(len(requests)):
if head < requests[i]:
index = i
break
else:
index = len(requests)
if direction == "right":
left = requests[:index]
right = requests[index:]
for r in right:
total_head_movement += abs(head - r)
head = r
# Jump to the lowest request (not to 0)
if left:
total_head_movement += abs(head - left[0])
head = left[0]
for r in left:
total_head_movement += abs(head - r)
head = r
elif direction == "left":
left = requests[:index]
right = requests[index:]
left.reverse()
for r in left:
total_head_movement += abs(head - r)
head = r
# Jump to the highest request (not to disk end)
if right:
total_head_movement += abs(head - right[-1])
head = right[-1]
right.reverse()
for r in right:
total_head_movement += abs(head - r)
head = r
return total_head_movement
# Example usage
requests = [82, 170, 43, 140, 34, 190]
head = 70
direction = "right"
print("Total head movement using LOOK:", look_disk_scheduling(requests, head, direction))
print("Total head movement using C-LOOK:", clook_disk_scheduling(requests, head, direction))
The output of the above code will be −
Total head movement using LOOK: 276 Total head movement using C-LOOK: 285
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int look_disk_scheduling(vector<int> requests, int head, string direction) {
sort(requests.begin(), requests.end());
int total_head_movement = 0;
int index = 0;
for (int i = 0; i < requests.size(); i++) {
if (head < requests[i]) {
index = i;
break;
}
index = requests.size();
}
if (direction == "left") {
vector<int> left(requests.begin(), requests.begin() + index);
vector<int> right(requests.begin() + index, requests.end());
reverse(left.begin(), left.end());
for (int r : left) {
total_head_movement += abs(head - r);
head = r;
}
for (int r : right) {
total_head_movement += abs(head - r);
head = r;
}
}
else if (direction == "right") {
vector<int> left(requests.begin(), requests.begin() + index);
vector<int> right(requests.begin() + index, requests.end());
for (int r : right) {
total_head_movement += abs(head - r);
head = r;
}
reverse(left.begin(), left.end());
for (int r : left) {
total_head_movement += abs(head - r);
head = r;
}
}
return total_head_movement;
}
int clook_disk_scheduling(vector<int> requests, int head, string direction) {
sort(requests.begin(), requests.end());
int total_head_movement = 0;
int index = 0;
for (int i = 0; i < requests.size(); i++) {
if (head < requests[i]) {
index = i;
break;
}
index = requests.size();
}
if (direction == "right") {
vector<int> left(requests.begin(), requests.begin() + index);
vector<int> right(requests.begin() + index, requests.end());
for (int r : right) {
total_head_movement += abs(head - r);
head = r;
}
if (!left.empty()) {
total_head_movement += abs(head - left[0]);
head = left[0];
for (int r : left) {
total_head_movement += abs(head - r);
head = r;
}
}
}
else if (direction == "left") {
vector<int> left(requests.begin(), requests.begin() + index);
vector<int> right(requests.begin() + index, requests.end());
reverse(left.begin(), left.end());
for (int r : left) {
total_head_movement += abs(head - r);
head = r;
}
if (!right.empty()) {
total_head_movement += abs(head - right.back());
head = right.back();
reverse(right.begin(), right.end());
for (int r : right) {
total_head_movement += abs(head - r);
head = r;
}
}
}
return total_head_movement;
}
int main() {
vector<int> requests = {82, 170, 43, 140, 34, 190};
int head = 70;
string direction = "right";
cout << "Total head movement using LOOK: "
<< look_disk_scheduling(requests, head, direction) << endl;
cout << "Total head movement using C-LOOK: "
<< clook_disk_scheduling(requests, head, direction) << endl;
return 0;
}
The output of the above code will be −
Total head movement using LOOK: 276 Total head movement using C-LOOK: 285
import java.util.*;
public class DiskSchedulingLook {
public static int lookDiskScheduling(List<Integer> requests, int head, String direction) {
Collections.sort(requests);
int totalHeadMovement = 0;
int index = 0;
for (int i = 0; i < requests.size(); i++) {
if (head < requests.get(i)) {
index = i;
break;
}
index = requests.size();
}
if (direction.equals("left")) {
List<Integer> left = new ArrayList<>(requests.subList(0, index));
List<Integer> right = new ArrayList<>(requests.subList(index, requests.size()));
Collections.reverse(left);
for (int r : left) {
totalHeadMovement += Math.abs(head - r);
head = r;
}
for (int r : right) {
totalHeadMovement += Math.abs(head - r);
head = r;
}
}
else if (direction.equals("right")) {
List<Integer> left = new ArrayList<>(requests.subList(0, index));
List<Integer> right = new ArrayList<>(requests.subList(index, requests.size()));
for (int r : right) {
totalHeadMovement += Math.abs(head - r);
head = r;
}
Collections.reverse(left);
for (int r : left) {
totalHeadMovement += Math.abs(head - r);
head = r;
}
}
return totalHeadMovement;
}
public static int clookDiskScheduling(List<Integer> requests, int head, String direction) {
Collections.sort(requests);
int totalHeadMovement = 0;
int index = 0;
for (int i = 0; i < requests.size(); i++) {
if (head < requests.get(i)) {
index = i;
break;
}
index = requests.size();
}
if (direction.equals("right")) {
List<Integer> left = new ArrayList<>(requests.subList(0, index));
List<Integer> right = new ArrayList<>(requests.subList(index, requests.size()));
for (int r : right) {
totalHeadMovement += Math.abs(head - r);
head = r;
}
if (!left.isEmpty()) {
totalHeadMovement += Math.abs(head - left.get(0));
head = left.get(0);
for (int r : left) {
totalHeadMovement += Math.abs(head - r);
head = r;
}
}
}
else if (direction.equals("left")) {
List<Integer> left = new ArrayList<>(requests.subList(0, index));
List<Integer> right = new ArrayList<>(requests.subList(index, requests.size()));
Collections.reverse(left);
for (int r : left) {
totalHeadMovement += Math.abs(head - r);
head = r;
}
if (!right.isEmpty()) {
totalHeadMovement += Math.abs(head - right.get(right.size() - 1));
head = right.get(right.size() - 1);
Collections.reverse(right);
for (int r : right) {
totalHeadMovement += Math.abs(head - r);
head = r;
}
}
}
return totalHeadMovement;
}
public static void main(String[] args) {
List<Integer> requests = Arrays.asList(82, 170, 43, 140, 34, 190);
int head = 70;
String direction = "right";
System.out.println("Total head movement using LOOK: " + lookDiskScheduling(requests, head, direction));
System.out.println("Total head movement using C-LOOK: " + clookDiskScheduling(requests, head, direction));
}
}
The output of the above code will be −
Total head movement using LOOK: 276 Total head movement using C-LOOK: 285
Pros and Cons of LOOK and C-LOOK Algorithms
Following are the advantages of LOOK and C-LOOK disk scheduling algorithms −
- The LOOK and C-LOOK algorithms reduce the total head movement by avoiding unnecessary traversal to the ends of the disk.
- Both algorithms provide better performance compared to SCAN and C-SCAN in terms of average seek time.
Following are the disadvantages of LOOK and C-LOOK disk scheduling algorithms −
- Both algorithms can lead to starvation for requests that are far from the current head position.
- They may not be suitable for systems with a high number of requests
- The implementation of these algorithms is more complex compared to simpler algorithms like FCFS and SSTF.