Write a C Program to Implement the Disk Scheduling Algorithm for LOOK

Aim:

To write a C Program to Implement the Disk Scheduling Algorithm for LOOK.

Description:

  • Disk Scheduling is the process of deciding which of the cylinder request is in the ready queue is to be accessed next.
  • The access time and the bandwidth can be improved by scheduling the servicing of disk I/O requests in good order.

Access Time:

The access time has two major components: Seek time and Rotational Latency.

Seek Time:

Seek time is the time for disk arm to move the heads to the cylinder containing the desired sector.

Rotational Latency:

Rotational latency is the additional time waiting for the disk to rotate the desired sector to the disk head.

Bandwidth:

The disk bandwidth is the total number of bytes transferred, divided by the total time between the first request for service and the completion of the last transfer.

Algorithm:

  1. Input the maximum number of cylinders and work queue and its head starting position.
  2. LOOK Scheduling 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.
  3. At the other end, the direction of head movement is reversed and servicing continues.
  4. The head continuously scans back and forth across the disk.
  5. The seek time is calculated.
  6. Display the seek time and terminate the program.

Exercise:

  • Consider that a disk drive has 5,000 cylinders, numbered 0 to 4,999.
  • The drive is currently serving a request at cylinder 2,150, and the previous request was at cylinder 1,805.
  • The queue of pending requests, in FIFO order, is: 2,069, 1,212, 2,296, 2,800, 544, 1,618, 356, 1,523, 4,965, 3681

Starting from the current head position, what is the total distance (in cylinders) that the disk arm moves to satisfy all the pending requests for each of the following disk-scheduling algorithms?

  1. FCFS
  2. SSTF
  3. SCAN
  4. LOOK
  5. C-SCAN
  6. C-LOOK

Program:

#include<stdio.h>
int request[50];
int SIZE;
int pre;
int head;
int uptrack;
int downtrack;
struct max{
int up;
int down;
}kate[50];
int dist(int a,int b)
{
if(a>b)
return a-b;
return b-a;
}
void sort(int n)
{
int i,j;
for (i = 0; i < n-1; i++)      
{     
for (j = 0; j < n-i-1; j++) 
{
if (request[j] > request[j+1])
{
int temp=request[j];
request[j]=request[j+1];
request[j+1]=temp;
}
}
}
j=0;
i=0;
while(request[i]!=head)
{
kate[j].down=request[i];
j++;
i++;
}
downtrack=j;
i++;
j=0;
while(i<n)
{
kate[j].up=request[i];
j++;
i++;
}
uptrack=j;
}
void look(int n)
{
int i;
int seekcount=0;
sort(n);
printf("SEEK SEQUENCE = ");
if(pre<head){
for(i=0;i<uptrack;i++)
{
printf(" %d",head);
seekcount=seekcount+dist(head,kate[i].up);
head=kate[i].up;
}
for(i=downtrack-1;i>=0;i--)
{
printf(" %d",head);
seekcount=seekcount+dist(head,kate[i].down);
head=kate[i].down;
}
}
else
{
for(i=downtrack-1;i>=0;i--)
{
printf(" %d",head);
seekcount=seekcount+dist(head,kate[i].down);
head=kate[i].down;
}
for(i=0;i<uptrack;i++)
{
printf(" %d",head);
seekcount=seekcount+dist(head,kate[i].up);
head=kate[i].up;
}	
}
printf(" %d\nTOTAL DISTANCE :%d",head,seekcount);	
}
int main()
{
int n,i;
printf("ENTER THE DISK SIZE :\n");
scanf("%d",&SIZE);
printf("ENTER THE NO OF REQUEST SEQUENCE :\n");
scanf("%d",&n);
printf("ENTER THE REQUEST SEQUENCE :\n");
for(i=0;i<n;i++)
scanf("%d",&request[i]);
printf("ENTER THE CURRENT HEAD :\n");
scanf("%d",&head);
request[n]=head;
printf("ENTER THE PRE REQUEST :\n");
scanf("%d",&pre);
look(n+1);	
}

Execution:

Input:
5000 10 2069 1212 2296 2800 544 1618 356 1523 4965 3681 2150 4555


Output:

ENTER THE DISK SIZE :
ENTER THE NO OF REQUEST SEQUENCE :
ENTER THE REQUEST SEQUENCE :
ENTER THE CURRENT HEAD :
ENTER THE PRE REQUEST :
SEEK SEQUENCE =  2150 2069 1618 1523 1212 544 356 2296 2800 3681 4965
TOTAL DISTANCE :6403

Result:

Thus Disk Scheduling Algorithm for LOOK program was executed Successfully.

Leave a Comment