Thursday, 28 November 2013

Banker's Algorithm (C++ Program)

#include<iostream.h>
#include<conio.h>
void main()
{
int alloc[10][10],max[10][10],avail[10],n,m,need[10][10],i,j,k,finish[10],work[10];
int SSeq[10];
clrscr();

cout<<"Enter no. of processes:";
cin>>n;
cout<<"Enter no. of resources:";
cin>>m;
       // cout<<"Enter allocation matrix:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<"\nEnter no. of instances of "<<j+1<<"allocated to "<<i+1<<":" ;
cin>>alloc[i][j];
}
}
       // cout<<"\nEnter max matrix";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<"\nEnter maximum instances of"<<j+1<<"allocated to"<<i+1<<":";
cin>>max[i][j];
}
}

for(i=0;i<m;i++)
{
cout<<"\nEnter available instances of"<<i+1;
cin>>avail[i];
}
//calculate need matrix
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
cout<<"\nAllocation matrix is:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<alloc[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\nMax matrix is:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<max[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\nNeed matrix is:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<need[i][j]<<"\t";
}
cout<<endl;
}
//initialize work and finish
for(i=0;i<n;i++)
work[i]=avail[i];
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(finish[i]==0 && need[i][j]<=work[j])
{
SSeq[k]=i+1;
k++;
work[i]=work[i]+alloc[i][j];
}
}
}
cout<<"safe sequence is:";
for(i=0;i<k;i++)
cout<<SSeq[i]<<",";
if(k==n)
cout<<"\nThe system is safe.\n";
else
cout<<"\nDeadlock occurence!!\n";
getch();
}

First Come First Serve scheduling (C++ Program)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,k,bt[10],pid[10],temp,temp1,wt[10],tat[10],twt=0,ttat=0;
cout<<"Enter no. of process:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter burst time for process "<<i+1<<":";
cin>>bt[i];
pid[i]=i;
}
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
tat[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tat[i];
}
float awt=(float)twt/n;
float atat=(float)ttat/n;
cout<<"Process ID          :";
for(i=0;i<n;i++)
cout<<pid[i]<<"\t";
cout<<"\nBurst Time        :";
for(i=0;i<n;i++)
cout<<bt[i]<<"\t";
cout<<"\nWaiting time      :";
for(i=0;i<n;i++)
cout<<wt[i]<<"\t";
cout<<"\nTurn around time  :";
for(i=0;i<n;i++)
cout<<tat[i]<<"\t";

cout<<"\n";

cout<<"Avg waiting time:"<<awt<<endl;
cout<<"Avg turn around time:"<<atat<<endl;
getch();
}

Shortest job first scheduling (C++ program)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,k,bt[10],pid[10],temp,temp1,wt[10],tat[10],twt=0,ttat=0;
//reading process ID and burst time
cout<<"Enter no. of process:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter burst time for process "<<i+1<<":";
cin>>bt[i];
pid[i]=i;
}
// sorting jobs according to burst time (ascending )
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if( bt[i] > bt[k] )
{
//swapping process id
temp=pid[i];
pid[i]=pid[k];
pid[k]=temp;
//swapping burst time
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
}
}
}
// caculating waiting time and turn around time for each process
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
tat[i]=wt[i]+bt[i];
}
//calculating total turn around time and total waiting time
for(i=0;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tat[i];
}
float awt=(float)twt/n;
float atat=(float)ttat/n;
// displaying output
cout<<"Process ID          :";
for(i=0;i<n;i++)
cout<<pid[i]<<"\t";
cout<<"\nBurst Time        :";
for(i=0;i<n;i++)
cout<<bt[i]<<"\t";
cout<<"\nWaiting time      :";
for(i=0;i<n;i++)
cout<<wt[i]<<"\t";
cout<<"\nTurn around time  :";
for(i=0;i<n;i++)
cout<<tat[i]<<"\t";

cout<<"\n";

cout<<"Avg waiting time:"<<awt<<endl;
cout<<"Avg turn around time"<<atat<<endl;
getch();
}