0% found this document useful (0 votes)
2K views16 pages

C Program For C-SCAN Disk Scheduling

This document contains code for several disk scheduling algorithms - C-SCAN, FCFS, SSTF, SCAN, LOOK, and C-LOOK. It includes code to simulate each algorithm, taking input for number of cylinders, requests, current position, and request queue. The code calculates total head movement for each algorithm and outputs the service order. There is also a menu-driven section to choose which algorithm to run.

Uploaded by

Akhil Tripathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views16 pages

C Program For C-SCAN Disk Scheduling

This document contains code for several disk scheduling algorithms - C-SCAN, FCFS, SSTF, SCAN, LOOK, and C-LOOK. It includes code to simulate each algorithm, taking input for number of cylinders, requests, current position, and request queue. The code calculates total head movement for each algorithm and outputs the service order. There is also a menu-driven section to choose which algorithm to run.

Uploaded by

Akhil Tripathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

C program for C-SCAN disk scheduling :

#include<stdio.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;
}

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

FCFS Disk Scheduling:

/*
FCFS Disk Scheduling Algorithm
Created by: Pirate
*/

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

SCAN Disk Scheduling:

#include<conio.h>
#include<stdio.h>
int main()
{
int i,j,sum=0,n;
int d[20];
int disk; //loc of head
int temp,max;
int dloc; //loc of disk in array
clrscr();
printf("enter number of location\t");
scanf("%d",&n);
printf("enter position of head\t");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&d[i]);
}
d[n]=disk;
n=n+1;
for(i=0;i<n;i++) // sorting disk locations
{
for(j=i;j<n;j++)
{
if(d[i]>d[j])
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
max=d[n];
for(i=0;i<n;i++) // to find loc of disc in array
{
if(disk==d[i]) { dloc=i; break; }
}
for(i=dloc;i>=0;i--)
{
printf("%d -->",d[i]);
}
printf("0 -->");
for(i=dloc+1;i<n;i++)
{
printf("%d-->",d[i]);
}
sum=disk+max;
printf("\nmovement of total cylinders %d",sum);
getch();
return 0;
}

ROUND ROBIN:
Shortest Seek Time First(SSTF) Disk Scheduling

#include<conio.h>
#include<stdio.h>
struct di
{
int num;
int flag;
};
int main()
{
int i,j,sum=0,n,min,loc,x,y;
struct di d[20];
int disk;
int ar[20],a[20];
clrscr();
printf("enter number of location\t");
scanf("%d",&n);
printf("enter position of head\t");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&d[i].num); d[i]. flag=0;
}
for(i=0;i<n;i++)
{ x=0; min=0;loc=0;
for(j=0;j<n;j++)
{
if(d[j].flag==0)
{
if(x==0)
{
ar[j]=disk-d[j].num;
if(ar[j]<0){ ar[j]=d[j].num-disk;}
min=ar[j];loc=j;x++; }
else
{
ar[j]=disk-d[j].num;
if(ar[j]<0){ ar[j]=d[j].num-disk;}
}
if(min>ar[j]){ min=ar[j]; loc=j;}
}
}
d[loc].flag=1;
a[i]=d[loc].num-disk;
if(a[i]<0){a[i]=disk-d[loc].num;}

disk=d[loc].num;
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("\nmovement of total cylinders %d",sum);
getch();
return 0;
}

Output:-
enter number of location 8
enter position of head 53
enter elements of disk queue
98
183
37
122
14
124
65
67
movement of total cylinders 236

LOOK Disk Scheduling:

#include<math.h>
#include<stdio.h>
int main()
{
int i,n,j=0,k=0,x=0,l,req[50],mov=0,cp,ub,end, lower[50],upper[50],
temp,a[50];
printf("enter the current position\n");
scanf("%d",&cp);
printf("enter the number of requests\n");
scanf("%d",&n);
printf("enter the request order\n");
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
}
printf("Enter the upper bound\n");
scanf("%d",&ub);
/*break the request array into two arrays : one with requests lower than
current and other with requests higher than current position. Also sort these two
new arrays*/
for(i=0;i<n;i++)
{
if(req[i]<cp)
{
lower[j]=req[i];
j++;
}
if(req[i]>cp)
{
upper[k]=req[i];
k++;
}
}

//sort the lower array in reverse order


for(i=0;i<j;i++)
{
for(l=0;l<j-1;l++)
{
if(lower[l]<lower[l+1])
{
temp=lower[l];
lower[l]=lower[l+1];
lower[l+1]=temp;
}
}
}

// sort the upper array in ascending order


for(i=0;i<=k;i++)
{
for(l=0;l<k-1;l++)
{
if(upper[l]>upper[l+1])
{
temp=upper[l];
upper[l]=upper[l+1];
upper[l+1]=temp;
}
}
}

printf("Enter the end to which the head is moving (0 - for lower end(zero) and
1 - for upper end\n");
scanf("%d",&end);
switch(end)
{
case 0:
for(i=0;i<j;i++)
{
a[x]=lower[i];
x++;
}

for(i=0;i<k;i++)
{
a[x]=upper[i];
x++;
}
break;
case 1:
for(i=0;i<k;i++)
{
a[x]=upper[i];
x++;
}

for(i=0;i<j;i++)
{
a[x]=lower[i];
x++;
}
break;
}

mov=mov+abs(cp-a[0]);
printf("%d -> %d",cp,a[0]);
for(i=1;i<x;i++)
{
mov=mov+abs(a[i]-a[i-1]);
printf(" -> %d",a[i]);
}
printf("\n");
printf("total head movement = %d\n",mov);
}

ALL ALGORITHMS – MENU DRIVEN

SIMULATION OF DISK SCHEDULING ALGORITHMS

PROGRAM CODING:-
#include<stdio.h>
int absolute(int a,int b)
{int c;
c=a-b;
if(c<0)
return -c;
else
return c;
}

int main()
{int choice,m,n,x,start,i,j,pos,min,a[15],count;
count=0;
printf("\nEnter the number of cylinders :");
scanf("%d",&m);
printf("\nEnter the number of requests :");
scanf("%d",&n);
printf("\nEnter current position :");
scanf("%d",&start);
printf("\nEnter the request queue :");
for(i=0;i<n;i++)
{scanf("%d",&a[i]);
if(a[i]>=m)
{printf("\ninvalid input");
scanf("%d",&a[i]);
}
}
do
{printf("\n\nDISK SCHEDULING ALGORITHMS\n1. FCFS\n2. SSTF\n3. SCAN\n4. C-
SCAN\n5. LOOK\n6. C-LOOK");
printf("\nEnter choice :");
scanf("%d",&choice);
count=0;
x=start;
switch(choice)
{case 1:printf("\nFCFS :\n");
printf("Scheduling services the request in the order that
follows:\n%d\t",start);
for(i=0;i<n;i++)
{x-=a[i];
if(x<0)
x=-x;
count+=x;
x=a[i];
printf("%d\t",x);
}
printf("\nTotal Head Movement :%d Cylinders",count);
break;
case 2:printf("\nSSTF :\n");
printf("Scheduling services the request in the order that
follows:\n%d\t",start);
for(i=0;i<n;i++)
{min=absolute(a[i],x);
pos=i;
for(j=i;j<n;j++)
if(min>absolute(x,a[j]))
{pos=j;
min=absolute(x,a[j]);
}
count+=absolute(x,a[pos]);
x=a[pos];
a[pos]=a[i];
a[i]=x;
printf("%d\t",x);
}
printf("\nTotal Head Movement: %d Cylinders",count);
break;
case 3:printf("\nSCAN :\n");
printf("Scheduling services the request in the order that follows:\n");
count=0;
pos=0;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
for(i=0;i<n;i++)
if(a[i]<start)
pos++;
for(i=0;i<pos;i++)
for(j=0;j<pos-i-1;j++)
if(a[j]<a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
x=start;
printf("%d\t",x);
for(i=0;i<pos;i++)
{count+=absolute(a[i],x);
x=a[i];
printf("%d\t",x);
}
count+=absolute(x,0);
x=0;
printf("%d\t",x);
for(i=pos;i<n;i++)
{count+=absolute(a[i],x);
x=a[i];
printf("%d\t",x);
}
/*for(i=0;i<n;i++)
printf("%d\t",a[i]);*/
printf("\nTotal Head Movement: %d Cylinders",count);
break;
case 4:printf("\nC-SCAN :\n");
printf("Scheduling Services the request in the order that
follows:\n%d\t",start);
count=0;
pos=0;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
for(i=0;i<n;i++)
if(a[i]<start)
pos++;
x=start;
for(i=pos;i<n;i++)
{count+=absolute(x,a[i]);
x=a[i];
printf("%d\t",x);
}
count+=absolute(m-1,x);
x=0;
printf("%d\t%d\t",m-1,0);
for(i=0;i<pos;i++)
{count+=absolute(x,a[i]);
x=a[i];
printf("%d\t",x);
}
/*for(i=0;i<n;i++)
printf("%d\t",a[i]);*/
printf("\nTotal Head movement: %d Cylinders",count);
break;
case 5:printf("\nLOOK :\n");
printf("\nScheduling services the request in the order as follows
:\n%d\t",start);
count=0;
pos=0;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
for(i=0;i<n;i++)
if(a[i]<start)
pos++;
for(i=0;i<pos;i++)
for(j=0;j<pos-i-1;j++)
if(a[j]<a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
x=start;
for(i=0;i<pos;i++)
{count+=absolute(a[i],x);
x=a[i];
printf("%d\t",x);
}
for(i=pos;i<n;i++)
{count+=absolute(a[i],x);
x=a[i];
printf("%d\t",x);
}
printf("\nToal Head Movement: %d Cylinders",count);
break;
case 6:printf("\nC-LOOK :\n");
printf("Scheduling Services the request in the order that
follows:\n%d\t",start);
count=0;
pos=0;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
for(i=0;i<n;i++)
if(a[i]<start)
pos++;
x=start;
for(i=pos;i<n;i++)
{count+=absolute(x,a[i]);
x=a[i];
printf("%d\t",x);
}
for(i=0;i<pos;i++)
{count+=absolute(x,a[i]);
x=a[i];
printf("%d\t",x);
}
/*for(i=0;i<n;i++)
printf("%d\t",a[i]);*/
printf("\nTotal Head movement: %d Cylinders",count);
break;
}
printf("\nDo you want to continue(1 to continue) :");
scanf("%d",&choice);
}while(choice==1);
}

__________________________________________________________
Sequential File Allocation method.

#include<stdio.h>
void main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d:",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d:",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t %d\n",i+1,t[i],b[i]);
}

Indexed File Allocation method.

#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}

Linked File Allocation method.

#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}

You might also like