Showing posts with label operating system. Show all posts
Showing posts with label operating system. Show all posts

Thursday, March 6, 2014

Implementation of Banker's algorithm in C++

It is a deadlock avoidance strategy utilized to check where whether the given state of the system having certain maximum request and availability of the resources is safe or if it can lead to a deadlock;

For the Banker's algorithm to work, it needs to know three things:

  • Maximum no. of instances a process is allowed to hold [MAX]
  • Resources each process is currently holding[ALLOCATED]
  • Resources currently available in the system [AVAILABLE]
Either MAX is given or REQUEST is given

if  REQUEST are given  then MAX=allocated+request;

From these [NEED] of each process is calculated.
need=max-allocated

Resources are allocated to a process  if :
  • need≤ available

#include<iostream.h>
#include<conio.h>
void main()
{
int instance[5],count,sequence[10],safe,s=0,j,completed;
int available[5],allocation[10][5],max[10][5];
int need[10][5],process,P[10],countofr,countofp,running[10];
clrscr();
cout<<"\n Enter the number of resources (<=5): ";
cin>> countofr;
for(int i=0;i<countofr;i++)
{  cout<<"\n enter the max instances of  resource R["<<i<<"] :";
   cin>>instance[i];
   available[i]=instance[i];
}
cout<<"\n Enter the number of processes (<=10): ";
cin>> countofp;
cout<<"\n Enter the allocation matrix \n     ";
 
for(i=0;i<countofp;i++)
   { cout<<"FOR THE PROCESS :P["<<i<<"]"<<endl;
     for(int j=0;j<countofr;j++)
    {  cout<<"allocation of resource R["<<j<<"] is : " ;
  cin>>allocation[i][j];
  available[j]-=allocation[i][j];
}
   }
cout<<"\nEnter the MAX matrix \n\n";

    for(i=0;i<countofp;i++)
       { cout<<"FOR THE PROCESS P["<<i<<"]"<<endl;
for(int j=0;j<countofr;j++)
 {   cout<<"max demand of resource R["<<j<<"] is : ";
     cin>>max[i][j];
 }
       }
clrscr();
cout<<"\n the given data are : \n";

cout<<endl<<"\nTotal resources in system : \n\n  ";
for(i=0;i<countofr;i++)
   cout<<" R["<<i<<"]  ";
   cout<<endl;
for(i=0;i<countofr;i++)
   cout<<"     "<<instance[i];

cout<<"\n\n ALLOCATION matrix \n\n\t";
for(j=0;j<countofr;j++)
   cout<<"R["<<j<<"]  ";
   cout<<endl;

for(i=0;i<countofp;i++)
{  cout<<"P["<<i<<"]  ";
   for(j=0;j<countofr;j++)
      cout<<"    "<<allocation[i][j];
   cout<<endl;
 }

 cout<<"\n\n MAX matrix \n\n\t";
for(j=0;j<countofr;j++)
   cout<<"R["<<j<<"]  ";
   cout<<endl;

for(i=0;i<countofp;i++)
{  cout<<"P["<<i<<"]  ";
   for(j=0;j<countofr;j++)
      cout<<"    "<<max[i][j];
   cout<<endl;
 }
    for(i=0;i<countofp;i++)
       { 
for(j=0;j<countofr;j++)
 {   
     need[i][j]=max[i][j]-allocation[i][j];
 }
}

 cout<<"\n\n NEED matrix \n\n\t";
for(j=0;j<countofr;j++)
   cout<<"R["<<j<<"]  ";
   cout<<endl;

for(i=0;i<countofp;i++)
{  cout<<"P["<<i<<"]  ";
   for(j=0;j<countofr;j++)
      cout<<"    "<<need[i][j];
   cout<<endl;
 }

 cout<<"\n NOW to  check whether above state is safe";
 cout<<"\n sequence in which above requests can be fulfilled";
 cout<<"\n press any key to continue";
 getch();

count=countofp;

for(i=0;i<countofp;i++)
    { running[i]=1;}

while(count)
    {   safe=0;
        for(i=0;i<countofp;i++)
  { if(running[i])
      {  completed=1;
 for(j=0;j<countofr;j++)
    {   if(need[i][j]> available[j])
  { completed=0;
    break;
  }
    }
if(completed)
                {
   running[i]=0;
                    count--;
   safe=1;
                    for(j=0;j<countofr;j++) 
                    {
                        available[j]+=allocation[i][j];
                    }
   sequence[s++]=i;
   cout<<"\n\n Running process P["<<i<<"]";
   cout<<endl<<"\n\nTotal resources now available:\n\n";
   for(i=0;i<countofr;i++)
   cout<<" R["<<i<<"]  ";
   cout<<endl;
   for(i=0;i<countofr;i++)
   cout<<"     "<<available[i];
                    break;
                }
            }

        }    
         if(!safe)
         break;
     }

if(safe)
 {
            cout<<"\nThe System is in safe state";
   cout<<"\nSafe sequence is :";
            for(i=0;i<countofp;i++)
            {
                cout<<"\t"<<"P["<<sequence[i]<<"]";
            }
 }
else
 {
   cout<<"\nThe System is in unsafe state";
 }
       getch();
}


Monday, November 21, 2011

Implementation of First Come First Serve Sceduling in c++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
 struct process
 {  float t ;
    int i;
 };
 int main()
 { process p[4];
  int n=4,temp;
  float t[4],w[4],ta=0,wa=0;
  clrscr();
  for( int j=0;j<n;j++)
  { cout<<"\n enter the the burst time of process p"<<j+1<<" : ";
  cin>>p[j].t;  }
  t[0]=p[0].t;
  for(j=1;j<4;j++)
  { t[j]=t[j-1]+p[j].t;
  }
   for (j=0;j<4;j++)
   { w[j]=t[j]-p[j].t;
   ta+=t[j];
   wa+=w[j];
   p[j].i=j;
   }
   // tubular display
   cout<<"\n\n        FCFS ALGO OF 4 PROCESSES \n    (\"All times are in milliseconds)\n\n\n -: RESULT :-\n";
   for(j=0;j<60;j++)
   cout<<"-";
   cout<<"\n\n     PROCESS     |     BURSTS    |    TAT     |    WT      |  \n\n";
   for(j=0;j<60;j++)
   cout<<"-";
   cout<<"\n";
   for(j=0;j<4;j++)
   {  cout<<"        "<<p[j].i+1<<"              "<<p[j].t<<"            "<<t[j]<<"            "<<w[j]<<endl;
     for(int m=0;m<60;m++)
     cout<<"-";
     cout<<endl;
   }
        float taa=ta/4,waa=wa/4;
   cout<<"\n\r      AVERAGE    :|                   "<<taa<<"           "<<waa;;
   cout<<endl;
   for(int m=0;m<60;m++)
   cout<<"-";
   getch();
   return 0;
   }

Implementation of Shortest Job First Scheduling in c++

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct process
{int t;
 int i;
};
int main()
{  process p[4];
   int n=4,temp ,tempi;
   float w[4],t[4],ta=0,wa=0;
   clrscr();
   for(int j=0;j<4;j++)
   { cout<<"Enter the Burst Time of the process"<<j+1<<"  :";
     cin>>p[j].t;
     p[j].i=j;
   }
   for(j=n-1;j>0;j--)
   { for(int k=0;k<j;k++)
      { if(p[k].t>p[k+1].t)
      { temp=p[k+1].t;
        tempi=p[k+1].i;

        p[k+1].t=p[k].t;
        p[k+1].i=p[k].i;

        p[k].t=temp;
        p[k].i=tempi;
      }
       }
   }

   t[0]=p[0].t;
   for(j=1;j<4;j++)
    t[j]=t[j-1]+p[j].t;
   for(j=0;j<4;j++)
   {w[j]=t[j]-p[j].t;
    ta+=t[j];
    wa+=w[j];
   }

   /* for(j=0;j<4;j++)
   {cout<<"\n THE TAT OF THE PROCESS P"<<p[j].i<<"is  :- "<<t[j]<<"ms";
   }
   cout<<"\n\n\n\n\n\n";
   for(j=0;j<4;j++)
      {cout<<"\n THE WT OF PROCESS P"<<p[j].i<<"is  :-"<<w[j]<<"ms";
      }
   cout<<"\n\n\n\n\n\n\n";
   for(j=0;j<4;j++)
   { cout<<"The Given BURST Of the process P"<<p[j].i<<"is  :-"<<p[j].t<<"ms";
   }
   getch();

   // tabular display
   clrscr();*/

   cout<<"\n\n       SJF ALGO of 4 processes \n    (All times are in milliseconds)\n\n  -: RESULT  :-\n";
   for(j=0;j<60;j++)
   cout<<"-";
   cout<<"\n\n PROCESS    | BURSTS   |  WT  |    TAT    |  \n\n";
   for(j=0;j<60;j++)
      cout<<"-";
      cout<<"\n";

   for(j=0;j<4;j++)
   { cout<<"     "<<p[j].i+1<<"          "<<p[j].t<<"          "<<w[j]<<"        "<<t[j]<<"\n";
     for(int m=0;m<60;m++)
     cout<<"-";
     cout<<"\n";
   }float atat=ta/4,awt=wa/4;
   cout<<"\r  AVERAGE  :  |            "<<awt<<"        "<<atat<<endl;
   for(j=0;j<60;j++)
       cout<<"-";
   getch();
   return 0;
}

Implementation of Priority Sceduling in c++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct process
{int pr,i;
float t;};

void main()
{ process p[4];
  int n=4,m,temp,tempi,temppr;
  float T[4],w[4],ta=0,wa=0;
  clrscr();
  for(int j=0;j<4;j++)
     {  cout<<"\n Enter the burst time of process P"<<j+1<<" :";
         cin>>p[j].t;
         p[j].i=j;
        cout<<"\nEnter the priority of process P"<<j+1<<" :";
        cin>>p[j].pr;
     }
  clrscr();
  for(j=n-1;j>0;j--)
    { for(int k=0;k<j;k++)
      { if(p[k].pr>p[k+1].pr)
      {
    temp=p[k+1].t;
    tempi=p[k+1].i;
    temppr=p[k+1].pr;
    p[k+1].t=p[k].t;
    p[k+1].i=p[k].i;
    p[k+1].pr=p[k].pr;
    p[k].t=temp;
    p[k].i=tempi;
    p[k].pr=temppr;
      }
    }
    }

    T[0]=p[0].t;
    for(j=1;j<=3;j++)
       T[j]=T[j-1]+p[j].t;
    for(j=0;j<4;j++)
      {w[j]=T[j]-p[j].t;
       ta=ta+T[j];
       wa=wa+w[j];
      }

cout<<"\n\n\n\n\n\n\n";
for(j=0;j<4;j++)
    {cout<<"\nThe BURSTS of process P"<<p[j].i+1<<"is :-"<<p[j].t<<"ms with PRIORITY="<<p[j].pr;
    }
getch();
clrscr();
cout<<"\n\n                    PRIORITY ALGO OF 4 PROCESS \n                (\"All times are in milliseconds\")\n\n\n\n\n      -:RESULT:-\n";
for(j=0;j<76;j++)
cout<<"-";
cout<<"\n";
cout<<"|  Process |  Burst Time    |   Priority    |   TAT     |     WT   |";
for(j=0;j<4;j++)
{
cout<<"\n    "<<p[j].i+1<<"          "<<p[j].t<<"               "<<p[j].pr<<"         "<<T[j]<<"            "<<w[j]<<endl;
for(m=0;m<76;m++)
cout<<"-";
cout<<endl;
}
ta=ta/4;
wa=wa/4;
cout<<"\n";
cout<<"\rAVERAGE TAT & WT ARE GIVEN AS :   |              "<<ta<<"            "<<wa;
cout<<endl;
for(m=0;m<76;m++)
cout<<"-";
getch();
}