The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Tuesday, May 31, 2011

Disk Scheduling Algorithm (Complete)


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

main()
{
int gd=DETECT,gm,x,y,a1,x1,y1,a,b;
int req[30],head,i,k,move,n,ch,t1,t2,max,min,g[30],c,h;
initgraph(&gd,&gm,"z:\tcpp\bgi");
/*int req[30],head,i,k,move,n,ch,t1,t2,max,min,g[30],c,h;*/
clearviewport();
printf("(1) LOOK (2) CLOOK (3) FCFS (0) EXIT
Enter your choice:
");
scanf("%d",&ch);

while(ch)
{
printf("Enter the current head position: ");
scanf("%d",&head); h=head;
printf("Enter the number of requests: ");
scanf("%d",&n); k=0; move=0;
printf("Total number of cylinders: 200.
");
printf("Enter the requests (1 to 200):
");
req:
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
if(req[i]>200||req[i]<1)
{ printf("Enter requests only between 1-200."); goto req; }
}

switch(ch)
{
case 1:
outtextxy(270,10,"SEEK PATTERN FOR LOOK");
max=req[0]; min=req[0]; t2=1; c=0;
for(i=1;i<n;i++)
{
max=max>req[i]?max:req[i];
min=min<req[i]?min:req[i];
}
if(max<=head) t2=0;
while(k<n)
{
t1=0;
for(i=0;i<n;i++)
{
if(t2)
{
if(req[i]&&req[i]>=head)
{
if(req[i]<req[t1]) t1=i;
if(req[i]==max)
{ t2=0; g[c]=1; c++; }
}
}

else
{
if(req[i]&&req[i]<=head)
{
if(req[i]>req[t1]) t1=i;
if(req[i]==min)
{ t2=1; g[c]=200; c++; }
}
}
}/*for*/
printf("%d ",req[t1]);
g[c]=req[t1]; c++;
move+=abs(head-req[t1]);
req[t1]=0; k++;
head=req[t1];
if(head==max) t2=0;
}/*while*/
break;

case 2:
outtextxy(270,10,"SEEK PATTERN FOR CLOOK");
max=req[0]; min=req[0]; t2=1; c=0;
for(i=1;i<n;i++)
{
max=max>req[i]?max:req[i];
min=min<req[i]?min:req[i];
}
while(k<n)
{
t1=0;
if(!t2) head=min;
for(i=0;i<n;i++)
{
if(req[i]&&req[i]>=head)
{
if(req[i]<req[t1]) t1=i;
else if(req[i]==max)
{ head=1; g[c]=1; c++; }
}
}/*for*/
printf("%d ",req[t1]);
move+=abs(head-req[t1]);
head=req[t1];
g[c]=req[t1]; c++;
req[t1]=0; k++;
if(head==max) t2=0;
}/*while*/
break;

case 3:
outtextxy(270,10,"SEEK PATTERN FOR FCFS");
for(i=0,c=0;i<n;i++,c++)
{
printf("%d ",req[i]);
move+=abs(head-req[i]);
head=req[i];
g[c]=req[i];
}
break;
}/*switch*/
printf("Order of servicing:
");
printf("%d: ",h);
for(i=0;i<n;i++)
printf("%d ",g[i]);
printf("
Total Head Movements: %d
",move);
x=getmaxx();
y=getmaxy();
rectangle(0,20,x-5,y-5);

a1=(x-30)/10;

b=110; a=h+(3*a1); y1=125;
fillellipse(a,b,2,2);


for(i=0;i<n;i++)
{
int x1=g[i]+(3*a1);
fillellipse(x1,y1,2,2);
line(a,b,x1,y1);
a=x1; b=y1;
y1+=15;
}

getch();
clrscr(); clearviewport();
printf("
(1) LOOK (2) CLOOK (3) FCFS (0) EXIT
Enter your choice:
");
scanf("%d",&ch);
}/*while*/
getch();
}/*main*/

Wednesday, May 25, 2011

C-LOOK Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
int cyposn[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);
}

void CLOOK()
{
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i=0,j=cp;
 cposn = cyposn[cp];
 do
 {
  if(cp == req)
  { nposn = cyposn[0]; cp = 0; }
  else
   nposn = cyposn[++cp];
  printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn == cyposn[req] ? cyposn[0] : nposn ;
 }while(nposn != cyposn[j-1]);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 CLOOK();
 getch();
}

Saturday, May 21, 2011

FCFS Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,req,ttl_tracks=0,cposn;
int cyposn[max];

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);
}

void FCFS()
{
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 for(i=0;i<req;i++)
 {
  printf(" %d\t\t%d\t\t%d\t\t%d\n",i+1,cyposn[i],cyposn[i+1],abs(cyposn[i]-cyposn[i+1]));
  ttl_tracks += (abs(cyposn[i]-cyposn[i+1]));
 }
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 FCFS();
 getch();
}

SSTF Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<limits.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn;
int cyposn[max],ind[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
 {
  scanf("%d",&cyposn[i]);
  ind[i] = 0;
 }

}

void search_short()
{
 int npl,npr,tmpr,tmpl;
 if(cp == 0)
 {
  np = cp;
  while(ind[np] == 1 && np < req)
   np++;
 }
 else if(cp == req)
 {
  np = cp;
  while(ind[np] == 1 && np > 0)
   np--;
 }
 else
 {
  npl = cp;
  while(ind[npl] == 1 && npl > 0)
   npl--;
  if(npl == 0 && ind[npl] == 1)
   tmpl = INT_MAX;
  else
   tmpl = cyposn[npl];
  npr = cp;
  while(ind[npr] == 1 && npr < req)
   npr++;
  if(npl == req && ind[npr] == 1)
   tmpr = INT_MAX;
  else
   tmpr = cyposn[npr];
  if(abs(cyposn[cp] - tmpl) < abs(cyposn[cp] - tmpr))
   np = npl;
  else
   np = npr;
 }
 ind[np] = 1;
}

void SSTF()
{
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 ind[cp] = 1;
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i = 0 ;
 do
 {
  search_short();
  printf(" %d\t\t%d\t\t%d\t\t%d\t\t%d\n",i+1,cyposn[cp],cyposn[np],abs(cyposn[cp]-cyposn[np]),np);
  ttl_tracks += (abs(cyposn[cp]-cyposn[np]));
  cp = np;
  i++;
 }while(i!=req);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 SSTF();
 getch();
}

Thursday, May 12, 2011

LOOK Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
int cyposn[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);
}

void LOOK()
{
 int ind = 0;
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 int tmp = cp;
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i=0;
 cposn = cyposn[cp];
 do
 {
  if(ind == 0)
  {
   if(cp == 0)
   {
    cp = tmp;
    nposn = cyposn[++cp]; ind = 1;
   }
   else
    nposn = cyposn[--cp];
  }
  else
    nposn = cyposn[++cp];

  printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn;
 }while(nposn!=cyposn[req]);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 LOOK();
 getch();
}

Wednesday, May 11, 2011

C-SCAN Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
int cyposn[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);
}

void CSCAN()
{
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i=0,j=cp;
 cposn = cyposn[cp];
 do
 {
  if(cposn == cyposn[req])
  { nposn = 199; cp = -1; }
  else
   nposn = cyposn[++cp];
  printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn == 199 ? 0 : nposn;
 }while(nposn != cyposn[j-1]);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
CSCAN();
 getch();
}

Monday, May 2, 2011

SCAN Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
int cyposn[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);



}

void SCAN()
{
 int ind = 0;
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 int tmp = cp;
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i=0;
 cposn = cyposn[cp];
 do
 {
  if(ind == 0)
  {
   if(cp == 0)
   { nposn = 0; ind = 1; }
   else
    nposn = cyposn[--cp];
  }
  else
  {
   if(cp == 0)
    cp = tmp;
   nposn = cyposn[++cp];
  }


  printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn;
 }while(nposn!=cyposn[req]);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 SCAN();
 getch();
}