FIFO-Disk Scheduling

#include<iostream.h>
#include<conio.h>
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<<“\n enter”;
cin>>d[i];
}
sort(d,n);
cout<<“order is”;
for(int j=0;j<n;j++)
{
cout<<“\n”<<d[j];
}
getch();
}

 

 

FIFO Page Replacement Algorithm

#include<iostream.h>
#include<conio.h>
struct process
{
int page;
int value;
}stack[3];
process least()
{
process small;
small=stack[0];
for(int i=0;i<3;i++)
{
if(small.value>stack[i].value)
{
small=stack[i];
}
}
return small;
}
void inc(int p,int c)
{
for(int i=0;i<3;i++)
{
if(stack[i].page==p)
{
stack[i].value=c;
}
}
}
int compare(int p)
{
int flag=0;
for(int i=0;i<3;i++)
{
if(stack[i].page==p)
{
flag=1;
break;
}
}
if(flag==1)
{
return 1;
}
else
{
return 0;
}
}
void replace(process a,int p,int c)
{
process cmp;
for(int i=0;i<3;i++)
{
if(a.page==stack[i].page)
{
process b;
b.value=c;
b.page=p;
stack[i]=b;
}
}
}
void display()
{
cout<<“\n stack[0]”<<stack[0].page;
cout<<“\n stack[1]”<<stack[1].page;
cout<<“\n stack[2]”<<stack[2].page;
getch();
}
void main()
{
clrscr();
int p[30];
int n;
cout<<“enter n”;
cin>>n;
cout<<“enter “;
for(int i=0;i<n;i++)
{
cin>>p[i];
cout<<“enter”;
}
clrscr();
int pgf[20];
stack[0].page=p[0];
stack[0].value=0;
stack[1].page=p[1];
stack[1].value=1;
stack[2].page=p[2];
stack[2].value=2;
pgf[0]=p[0];
pgf[1]=p[1];
pgf[2]=p[2];
int count=3;
process ans2;
int temp=3,ans1;
while(count<n)
{ //cout<<“\n count is”<<count;
ans1=compare(p[count]);
/*if(count==10)
{cout<<“temp is”<<temp<<“\t value of page”<<p[count];}*/
if(ans1==1)
{
//cout<<“\n same number “;
inc(p[count],count);
}
if(ans1==0)
{
//cout<<“\n no match with selected”;
ans2=least();
replace(ans2,p[count],count);
pgf[temp]=p[count];
temp++;
//cout<<“\n value of temp”<<temp;
}
count++;
display();
}
cout<<” page faults are”;
for(int j=0;j<=temp;j++)
{
cout<<“\n “<<pgf[j];
}
getch();
}

 

 

LRU Page Replacement Algorithm

#include<iostream.h>
#include<conio.h>
struct process
{
int page;
int value;
}stack[3];
process least()
{
process small;
small=stack[0];
for(int i=0;i<3;i++)
{
if(small.value>stack[i].value)
{
small=stack[i];
}
}
return small;
}
void inc(int p,int c)
{
for(int i=0;i<3;i++)
{
if(stack[i].page==p)
{
stack[i].value=c;
}
}
}
int compare(int p)
{
int flag=0;
for(int i=0;i<3;i++)
{
if(stack[i].page==p)
{
flag=1;
break;
}
}
if(flag==1)
{
return 1;
}
else
{
return 0;
}
}
void replace(process a,int p,int c)
{
process cmp;
for(int i=0;i<3;i++)
{
if(a.page==stack[i].page)
{
process b;
b.value=c;
b.page=p;
stack[i]=b;
}
}
}
void display()
{
cout<<“\n stack[0]”<<stack[0].page;
cout<<“\n stack[1]”<<stack[1].page;
cout<<“\n stack[2]”<<stack[2].page;
getch();
}
void main()
{
clrscr();
int p[30];
int n;
cout<<“enter n”;
cin>>n;
cout<<“enter “;
for(int i=0;i<n;i++)
{
cin>>p[i];
cout<<“enter”;
}
clrscr();
int pgf[20];
stack[0].page=p[0];
stack[0].value=0;
stack[1].page=p[1];
stack[1].value=1;
stack[2].page=p[2];
stack[2].value=2;
pgf[0]=p[0];
pgf[1]=p[1];
pgf[2]=p[2];
int count=3;
process ans2;
int temp=3,ans1;
while(count<n)
{ //cout<<“\n count is”<<count;
ans1=compare(p[count]);
/*if(count==10)
{cout<<“temp is”<<temp<<“\t value of page”<<p[count];}*/
if(ans1==1)
{
//cout<<“\n same number “;
inc(p[count],count);
}
if(ans1==0)
{
//cout<<“\n no match with selected”;
ans2=least();
replace(ans2,p[count],count);
pgf[temp]=p[count];
temp++;
//cout<<“\n value of temp”<<temp;
}
count++;
display();
}
cout<<” page faults are”;
for(int j=0;j<=temp;j++)
{
cout<<“\n “<<pgf[j];
}
getch();
}

 

 

Optimal Page Replacement Algorithm

#include<iostream.h>
#include<conio.h>
int stack[3];
int occ(int pg[],int n,int count,int p)
{ int flag;
for(int i=count;i<n;i++)
{
if(pg[i]==p)
{
flag=1;
break;
}
}if(flag==1)
{
return i;
}
else
{
return 0;
}
}
int compare(int p)
{
int flag=0;
for(int i=0;i<3;i++)
{
if(stack[i]==p)
{
flag=1;
break;
}
}
if(flag==1)
{
return 1;
}
else
{
return 0;
}
}
void main()
{
int pg[20],pf[20];
int n;
cout<<“enter n”;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<“\n enter”;
cin>>pg[i];
}
stack[0]=pg[0];
stack[1]=pg[1];
stack[2]=pg[2];
pf[0]=pg[0];
pf[1]=pg[1];
pf[2]=pg[2];
int count=3;
int temp=3;
int ans1;
int a,b,c,d;
while(count<n)
{
ans1=compare(pg[count]);
if(ans1==0)
{
a=occ(pg,n,count,stack[0]);
b=occ(pg,n,count,stack[1]);
c=occ(pg,n,count,stack[2]);

if((a>b)&&(a>c))
{
stack[0]=pg[count];
}
if((c>b)&&(c>a))
{
stack[2]=pg[count];
}
if((b>a)&&(b>c))
{
stack[1]=pg[count];
}
pf[temp]=pg[count];
temp++;
}
count++;
}
for(int j=0;j<temp;j++)
{
cout<<“\n “<<pf[j];
}
getch();
}

SCAN disk scheduling

#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=0;
b=i+1;
while(a>-1)
{ cout<<“\n a is “<<a;
cout<<“\n left[a] is”<<left[a];

ans[d]=left[a];
cout<<“\n answer is”<<ans[d];
a–;
d++;
}
ans[d]=0;
while(b<n+1)
{
ans[b]=right[c];
c++;
b++;
}
cout<<“order is”;
getch();
for(int j=0;j<n+1;j++)
{
cout<<“\n”<<ans[j];
}
}
void divide(int d[],int n,int head)
{
cout<<“array is”;
for(int q=0;q<n;q++)
{
cout<<“\n “<<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<<“\n d[l] value” <<d[l];
left[l]=d[l];
cout<<“\n left is “<<left[l];
l++;
cout<<“l is “<<l;
}
int o;
o=i;
while(o<m)
{
right[k]=d[o];
cout<<“\n right is”<<right[k];
cout<<“\n 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<<“\n enter”;
cin>>d[i];
}
sort(d,n);
divide(d,n,head);
getch();
}

 

 

C-SCAN disk scheduling

#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<<“\n a is “<<a;
cout<<“\n left[a] is”<<left[a];

ans[d]=left[a];
cout<<“\n answer is”<<ans[d];
a–;
d++;
}
cout<<“d is”<<d;
while(d<n)
{
ans[d]=right[c];
cout<<“\n right c is”<<right[c];
cout<<“\n ans[d] is “<<ans[d];
c–;
d++;
}
cout<<“order is”;
getch();
for(int j=0;j<n;j++)
{
cout<<“\n”<<ans[j];
}
}
void divide(int d[],int n,int head)
{
cout<<“array is”;
for(int q=0;q<n;q++)
{
cout<<“\n “<<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<<“\n d[l] value” <<d[l];
left[l]=d[l];
cout<<“\n left is “<<left[l];
l++;
cout<<“l is “<<l;
}
int o;
o=i;
while(o<m)
{
right[k]=d[o];
cout<<“\n right is”<<right[k];
cout<<“\n 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<<“\n enter”;
cin>>d[i];
}
sort(d,n);
divide(d,n,head);
getch();
}

 

 

Calendar

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

Archives