Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write in Java but if not can be done in C. Write a program that implement

ID: 3851207 • Letter: P

Question

Please write in Java but if not can be done in C. Write a program that implements the following disk-scheduling algorithms:

FCFS

SSTF

SCAN

C-SCAN

LOOK

C-LOOK

Your program will service a disk with n cylinders numbered 0 to n-1. The program will read a data file (“input.txt”). The first entry in the data file is the number of cylinders for your disk. The next entry is the cylinder number the disk position at the beginning of the simulation. Next, comes a line with a string of numbers representing the numbers of cylinders with I/O requests and service them according to each of the algorithms listed above. It will output the total amount of head movement required by each algorithm. The output should look something like the following:

For FCFS, the total head movement was xxx cylinders.

For SSTF, the total head movement was xxx cylinders.

For SCAN, the total head movement was xxx cylinders.

For C-SCAN, the total head movement was xxx cylinders.

For LOOK, the total head movement was xxx cylinders.

For C-LOOK, the total head movement was xxx cylinders.

The data in this first set is that used on pages 447 – 451 in your text and may be used to check your code. Then your program will read a second similar set of data from the same file and run that. Note that the data second set has different number of cylinders.

Please make extensive comments explaining as much as possible

Do any of the algorithms look especially good or bad? Data will be posted below.

* Data *

Explanation / Answer

FCFS

-------------------------------

/*
FCFS Disk Scheduling Algorithm
  */

#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 *** ");
printf("Enter the size of Queue ");
scanf("%d",&n);
printf("Enter the Queue ");
for(i=1;i<=n;i++)
{
scanf("%d",&queue[i]);
}
printf("Enter the initial head position ");
scanf("%d",&head);
queue[0]=head;
printf(" ");
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 ",queue[j],queue[j+1],diff);
}
printf(" Total Seek Time is %d ",seek);
avg=seek/(float)n;
printf(" Average Seek Time is %f ",avg);
getch();
}

-----------------------------------------------------

SSTF

----------------------------------------

/*
SSTF Disk Scheduling Algorithm
*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
// clrscr();
printf("*** SSTF Disk Scheduling Algorithm *** ");
printf("Enter the size of Queue ");
scanf("%d",&n);
printf("Enter the Queue ");
for(i=0;i<n;i++)
{
scanf("%d",&queue[i]);
}
printf("Enter the initial head position ");
scanf("%d",&head);
for(i=1;i<n;i++)
t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(t[i]>t[j])
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(i=1;i<n-1;i++)
{
seek=seek+abs(head-queue[i]);
head=queue[i];
}
printf(" Total Seek Time is%d ",seek);
avg=seek/(float)n;
printf(" Average Seek Time is %f ",avg);
getch();
}

-------------------------------------------------------------

SCAN

----------------------------------------

#include<stdio.h>

#include<conio.h>

void main()

{

int a[20],b[20],n,i,j,temp,p,s,m,x,t=0;

clrscr();

printf("Enter head pointer position:");

scanf("%d",&a[0]);

s=a[0];

printf(" Enter previous head position:");

scanf("%d",&p);

printf(" Enter max track limit:");

scanf("%d",&m);

printf(" Enter number of processes:");

scanf("%d",&n);

printf(" Enter processes in request order");

for(i=1;i<=n;i++)

{

  scanf("%d",&a[i]);

}

a[n+1]=m;

a[n+2]=0;

for(i=n+1;i>=0;i--)

{

  for(j=0;j<=i;j++)

  {

   if(a[j]>a[j+1])

   {

    temp=a[j];

    a[j]=a[j+1];

    a[j+1]=temp;

   }

  }

}

for(i=1;i<=n+1;i++)

{

  if(s==a[i])

  x=i;

}

j=0;

if(s<p)

{

  for(i=x;i>0;i--)

  {

   t+=(a[i]-a[i-1]);

   b[j++]=a[i];

  }

  t+=a[x+1]-a[0];

  b[j++]=a[0];

  for(i=x+1;i<n+1;i++)

  {

   t+=(a[i+1]-a[i]);

   b[j++]=a[i];

  }

  b[j++]=a[i];

}

else

{

  for(i=x;i<n+2;i++)

  {

   t+=(a[i+1]-a[i]);

   b[j++]=a[i];

  }

  t+=a[n+2]-a[x-1];

  b[j++]=a[n+2];

  for(i=x-1;i>1;i--)

  {

   t+=(a[i]-a[i-1]);

   b[j++]=a[i];

  }

  b[j++]=a[i];

}

printf(" Processing order:");

for(i=0;i<=n+1;i++)

printf(" %d",b[i]);

printf(" Total Head Movement:%d",t);

getch();

}

----------------------------------------------------------------

C-SCAN

------------------------------------------------

#include<iostream.h>
#include<conio.h>
void scan(int left[],int right[],int i,int head,int n)
{
int ans[20];
int a,b,c,d;
a=i-1;
d=0;
c=n-1-i;
b=i;
while(a>-1)
{ cout<<“ a is “<<a;
cout<<“ left[a] is”<<left[a];

ans[d]=left[a];
cout<<“ answer is”<<ans[d];
a–;
d++;
}
cout<<“d is”<<d;
while(d<n)
{
ans[d]=right[c];
cout<<“ right c is”<<right[c];
cout<<“ ans[d] is “<<ans[d];
c–;
d++;
}
cout<<“order is”;
getch();
for(int j=0;j<n;j++)
{
cout<<“ ”<<ans[j];
}
}
void divide(int d[],int n,int head)
{
cout<<“array is”;
for(int q=0;q<n;q++)
{
cout<<“ “<<d[q];
}
int left[10],right[10];
for(int i=0;i<n;i++)
{
if(d[i]>head)
{
cout<<“breaking at “<<d[i];
break;
}
}
cout<<“value of i”<<i;
int k,l,m;
l=1;
k=0;
m=n;
left[0]=d[0];
cout<<“left is”<<left[0];
while(l<i)
{
cout<<“ d[l] value” <<d[l];
left[l]=d[l];
cout<<“ left is “<<left[l];
l++;
cout<<“l is “<<l;
}
int o;
o=i;
while(o<m)
{
right[k]=d[o];
cout<<“ right is”<<right[k];
cout<<“ d[i] is”<<d[o];
k++;
o++;
}
scan(left,right,i,head,n);
}
void sort(int d[],int n)
{
int temp,small,pos;
for(int i=0;i<n-1;i++)
{
small=d[i];
pos=i;
for(int j=i+1;j<n;j++)
{
if(small>d[j])
{
small=d[j];
pos=j;
}
}
temp=d[pos];
d[pos]=d[i];
d[i]=temp;
}
}
void main()
{
clrscr();
int head,d[20],n;
cout<<” enter number of disk schedulings you want to do”;
cin>>n;
cout<<“enter head”;
cin>>head;
for(int i=0;i<n;i++)
{
cout<<“ enter”;
cin>>d[i];
}
sort(d,n);
divide(d,n,head);
getch();
}

-----------------------------------------------

LOOK

--------------------------------------

#include <alloc.h>

struct reqblock
{ int block;
   struct reqblock *next;
} *first,*curr,*prev;

int n,currpos,headmove=0;
char direction;

void get_req_blocks();
void scan();
void main()
{
clrscr();
   printf(" Enter total number of blocks in the disk:");
   scanf("%d",&n);
   printf(" Enter request block numbers string terminated by -1 ");
    get_req_blocks();
    //print req string
    curr=first;
    while(curr!=NULL)
    { printf("%d ",curr->block);
    curr=curr->next;
    }
    printf(" Enter direction of head movement:(F-Forwad,B-Backward):");
    flushall(); scanf("%c",&direction);
    printf(" Enter block no. as current head position:");
    scanf("%d",&currpos);
    scan();
    printf(" Number of headmovements:%d",headmove);
}

void get_req_blocks()
{ struct reqblock *t,*pt;
    int blockno;
    first=NULL;
    scanf("%d",&blockno);
    while(blockno!=-1)
    { curr=(struct reqblock *) malloc(sizeof(struct reqblock));
    curr->block=blockno;
    curr->next=NULL;
    if (first==NULL) //req str is empty
        first=curr;
    else
        prev->next=curr;
    prev=curr;
    scanf("%d",&blockno);
    }//while
}

void scan()
{ int selblock;
    printf(" List of request served: ");

    while(first!=NULL)
    {
       if (!look())
        direction=(direction=='F')?'B':'F';
       selblock=get_next_block();
       if (selblock!=-1)
           printf("%d ",selblock);
        if (direction=='F')
        { if (currpos==n-1)
            direction='B';
        else
        { currpos++; headmove++;
        }
    }
    else
    {
        if (currpos==0)
            direction='F';
        else
        { currpos--; headmove++;}
       }
    }//while
    headmove--;
   }

int look()
{
   if (direction=='F')
   { curr=first;
    while(curr!=NULL)
    {   if (curr->block>currpos)
        return(1);
        curr=curr->next;
       }
       return(0);
    }
   else //direction='B'
   {   curr=first;
    while(curr!=NULL)
    { if (curr->block<currpos)
        return(1);
        curr=curr->next;
       }
       return(0);
    }
}

get_next_block()
   {
        int selblock;
        curr=first;
        while(curr->block!=currpos)
           { prev=curr;
            curr=curr->next;
            if (curr==NULL) return(-1);
            }
            selblock=curr->block;
            if (curr==first)
               first=first->next;
            else
               prev->next=curr->next;
            free(curr);
            return(selblock);
}