Question 1: Disk Scheduling using FCFS algorithm (Slip No: 1, 21, 26, 30)
#include<stdio.h>
#include<math.h>
void main()
{
int max_blocks, no_request, request_str[20], head, i, tot_head_moves=0;
int diff, first_blk, last_blk;
printf("\nEnter the total number of disk blocks:\n");
scanf("%d",&max_blocks);
first_blk=0;
last_blk=max_blocks-1;
printf("Enter the length of disk request string:\n");
scanf("%d",&no_request);
printf("Enter the disk request string: ");
for (i=1;i<=no_request;i++)
scanf("%d",&request_str[i]);
printf("Enter the current head position : ");
scanf("%d",&head);
request_str[0]=head;
for (i=1; i<=no_request; i++)
{
diff = abs(request_str[i]-request_str[i-1]);
tot_head_moves += diff;
printf("\nServing request %d ", request_str[i]);
}
printf("\n\nTotal Head Movement is %d\n", tot_head_moves);
}
Question 2: Disk Scheduling using SCAN algorithm (Slip No: 4,7,13, 18, 20)
#include<stdio.h>
void main()
{
int max_blocks, no_request, request_str[20], head, direction;
int i, j, diff, temp, tot_head_moves=0, index, first_blk, last_blk;
printf("\nEnter the total number of disk blocks\n");
scanf("%d",&max_blocks);
first_blk=0;
last_blk=max_blocks-1;
printf("Enter the length of disk request string\n");
scanf("%d",&no_request);
printf("Enter the disk request string\n");
for (i=0; i<no_request; i++)
scanf("%d",&request_str[i]);
printf("Enter the current head position \n");
scanf("%d", &head);
printf("Enter the direction of head movement (1 for right and 0 for left)\n");
scanf("%d", &direction);
// sort request string
// sort request string
for (i=0; i< no_request-1; i++)
{
for (j=i+1; j < no_request; j++)
{
if (request_str[i]>request_str[j])
{
temp=request_str[i];
request_str[i]=request_str[j];
request_str[j]=temp;
}
}
}
printf("Requests in sorted order\n");
for (i=0; i<no_request; i++)
printf("%d ",request_str[i]);
printf("\n");
for (i=0; i<no_request; i++)
{
if (head < request_str[i])
{
index=i;
break;
}
}
if (direction == 1)
{
for (i=index; i<no_request; i++)
printf("Serving Request: %d\n",request_str[i]);
printf("Head moved to %d\n", last_blk);
for (i=index-1; i>=0; i--)
printf("Serving Request: %d\n",request_str[i]);
tot_head_moves=(last_blk - head) + (last_blk - request_str[0]);
}
else
{
for (i=index-1; i>=0; i--)
printf("Serving Request: %d\n",request_str[i]);
printf("Head moved to %d\n", first_blk);
for (i=index;i<no_request; i++)
printf("Serving Request: %d\n",request_str[i]);
tot_head_moves= (head - first_blk) +
(request_str[no_request-1] - first_blk);
}
printf("Total head movement is %d",tot_head_moves);
}
Question 3: Disk Scheduling using C-SCAN algorithm (Slip No:6,10,15, 19)
#include<stdio.h>
void main()
{
int max_blocks, no_request, request_str[20], head, direction;
int i, j, diff, temp, tot_head_moves=0, index;
int first_blk, last_blk;
printf("\nEnter the total number of disk blocks\n");
scanf("%d",&max_blocks);
first_blk = 0;
last_blk = max_blocks-1;
printf("Enter the length of disk request string\n");
scanf("%d",&no_request);
printf("Enter the disk request string\n");
for (i=0; i<no_request; i++)
scanf("%d",&request_str[i]);
printf("Enter the current head position \n");
scanf("%d", &head);
printf("Enter the direction of head movement (1 for right and 0 for left)\n");
scanf("%d", &direction);
// sort request string
for (i=0; i< no_request-1; i++)
{
for (j=i+1; j < no_request; j++)
{
if (request_str[i]>request_str[j])
{
temp=request_str[i];
request_str[i]=request_str[j];
request_str[j]=temp;
}
}
}
printf("Requests in sorted order\n");
for (i=0; i<no_request; i++)
printf("%d ",request_str[i]);
printf("\n");
for (i=0; i<no_request; i++)
{
if (head < request_str[i])
{
index=i;
break;
}
}
if (direction == 1)
{
for (i=index; i<no_request; i++)
printf("Serving Request: %d\n",request_str[i]);
printf("Head moved to %d\n", last_blk);
printf("Head moved to %d\n", first_blk);
for (i=0; i<index; i++)
printf("Serving Request: %d\n",request_str[i]);
tot_head_moves= (last_blk - head) + (last_blk - first_blk) +
(request_str[index-1] - first_blk);
}
else
{
for (i=index-1; i>=0; i--)
printf("Serving Request: %d\n",request_str[i]);
printf("Head moved to %d\n", first_blk);
printf("Head moved to %d\n", last_blk);
for (i=no_request-1; i >= index; i--)
printf("Serving Request: %d\n",request_str[i]);
tot_head_moves= (head - first_blk) + (last_blk - first_blk)+ (last_blk -
request_str[index]);
}
printf("Total head movement is %d",tot_head_moves);
}
Question 4: Disk Scheduling using Look algorithm (Slip No: 9,17,25,27)
include<stdio.h>
#include<math.h>
void main()
{
int max_blocks, no_request, request_str[20], head, direction;
int i, j, temp, tot_head_moves=0, index,first_blk, last_blk;
printf("\nEnter the total number of disk blocks\n");
scanf("%d",&max_blocks);
first_blk=0;
last_blk=max_blocks-1;
printf("Enter the length of disk request string\n");
scanf("%d",&no_request);
printf("Enter the disk request string\n");
for (i=0; i<no_request; i++)
scanf("%d",&request_str[i]);
printf("Enter the current head position \n");
scanf("%d", &head);
printf("Enter the direction of head movement (1 for right and 0 for left)\n");
scanf("%d", &direction);
// sort request string
for (i=0; i< no_request-1; i++)
{
for (j=i+1; j < no_request; j++)
{
if (request_str[i]>request_str[j])
{
temp=request_str[i];
request_str[i]=request_str[j];
request_str[j]=temp;
}
}
}
printf("Requests in sorted order\n");
for (i=0; i<no_request; i++)
printf("%d ",request_str[i]);
printf("\n");
for (i=0; i<no_request; i++)
{
if (head < request_str[i])
{
index=i;
break;
}
}
if (direction == 1)
{
for (i=index; i<no_request; i++)
printf("Serving Request: %d\n",request_str[i]);
for (i=index-1; i>=0; i--)
printf("Serving Request: %d\n",request_str[i]);
tot_head_moves = (request_str[no_request-1]-head) +
(request_str[no_request-1] - request_str[0]);
}
else
{
for (i=index-1; i>=0; i--)
printf("Serving Request: %d\n",request_str[i]);
for (i=index;i<no_request; i++)
printf("Serving Request: %d\n",request_str[i]);
tot_head_moves= (head – request_str[0]) + (request_str[no_request-1]-
request_str[0]);
}
printf("Total head movement is %d",tot_head_moves);
}
Question 5: Disk Scheduling using C-Look algorithm (Slip No:12, 28,29)
#include<stdio.h>
#include<math.h>
void main()
{
int max_blocks, no_request, request_str[20], head, direction;
int i, j, temp, tot_head_moves=0, index, first_blk, last_blk;
printf("\nEnter the total number of disk blocks\n");
scanf("%d",&max_blocks);
first_blk=0;
last_blk=max_blocks-1;
printf("Enter the length of disk request string\n");
scanf("%d",&no_request);
printf("Enter the disk request string\n");
for (i=0; i<no_request; i++)
scanf("%d",&request_str[i]);
printf("Enter the current head position \n");
scanf("%d", &head);
printf("Enter the direction of head movement (1 for right and 0 for left)\n");
scanf("%d", &direction);
// sort request string
for (i=0; i< no_request-1; i++)
{
for (j=i+1; j < no_request; j++)
{
if (request_str[i]>request_str[j])
{
temp=request_str[i];
request_str[i]=request_str[j];
request_str[j]=temp;
}
}
}
printf("Requests in sorted order\n");
for (i=0; i<no_request; i++)
printf("%d ",request_str[i]);
printf("\n");
for (i=0; i<no_request; i++)
{
if (head < request_str[i])
{
index=i;
break;
}
}
if (direction == 1)
{
for (i=index; i<no_request; i++)
printf("Serving Request: %d\n",request_str[i]);
for (i=0; i<=index-1; i++)
printf("Serving Request: %d\n",request_str[i]);
tot_head_moves = (request_str[no_request-1]-head) +
(request_str[no_request-1] - request_str[0])+
(request_str[index-1]-request_str[0]);
}
else
{
for (i=index-1; i>=0; i--)
printf("Serving Request: %d\n",request_str[i]);
for (i=no_request-1; i>=0; i--)
printf("Serving Request: %d\n",request_str[i]);
tot_head_moves= (head - request_str[0]) +
(request_str[no_request-1]-request_str[0]) +
(request_str[no_request-1] - request_str[index]);
}
printf("Total head movement is %d",tot_head_moves);
}
Question 5: Disk Scheduling using SSTF algorithm (Slip No: 8, 14, 23)
#include<stdio.h>
#include<math.h>
void main()
{
int count, request_str[20],no_request,head;
int i, tot_head_moves=0, index, max_blocks, diff, min, first_blk, last_blk;
printf("\nEnter the total number of disk blocks:\n");
scanf("%d",&max_blocks);
first_blk=0;
last_blk= max_blocks – 1;
printf("Enter the length of disk request string:\n");
scanf("%d",&no_request);
printf("Enter the disk request string: ");
for (i=0;i<no_request;i++)
scanf("%d",&request_str[i]);
printf("Enter the current head position : ");
scanf("%d",&head);
count=0;
while (count!=no_request)
{
min=10000;
for (i=0;i<no_request;i++)
{
diff = abs(request_str[i]-head);
if (min>diff)
{
min = diff;
index = i;
}
}
tot_head_moves = tot_head_moves + min;
printf("\nServicing request %d",request_str[index]);
head=request_str[index];
request_str[index]=10000;
count++;
}
printf("\n\nTotal head movement is %d\n",tot_head_moves);
}