Wednesday 25 April 2012

OS Programs


bankers algorithm


[cse2b523@localhost ~]$ vi bankers.cpp
#include<iostream>
using namespace std;
class bankalg
{
        int **alloc;
        int **max;
        int *work;
        int *avail;
        int **need;
        int *safe;
        int *flag;
        int p,r;
        public:
        void read();
        void safe_sequence();
        void request_resource();
};
void bankalg::read()
{
        int i,j;
        cout<<"\nEnter the total number of processes\n";
        cin>>p;
        cout<<"\nEnter the number of resource types\n";
        cin>>r;
        alloc=new int* [p];
        max=new int* [p];
        need=new int* [p];
        avail=new int[r];
        work=new int[r];
        for(i=0;i<p;i++)
        {
                alloc[i]=new int[r];
                max[i]=new int[r];
                need[i]=new int[r];
        }
        cout<<"\nEnter the allocated resources of each process\n";
        for(i=0;i<p;i++)
        {
                cout<<"P"<<i<<":";
                for(j=0;j<r;j++)
                        cin>>alloc[i][j];
        }
        cout<<"\nEnter the Maximum number of resources for each process\n";
        for(i=0;i<p;i++)
        {
                cout<<"P"<<i<<":";
                for(j=0;j<r;j++)
                        cin>>max[i][j];
        }
        cout<<"\nEnter the available resources of each type\n";
        for(i=0;i<r;i++)
                cin>>avail[i];
        for(i=0;i<r;i++)
                work[i]=avail[i];
}
void bankalg::safe_sequence()
{
        int i,j;
        safe=new int[p];
        flag=new int[p];
        for(i=0;i<p;i++)
        {
                flag[i]=0;

        }

        for(i=0;i<p;i++)
                for(j=0;j<r;j++)
                        need[i][j]=max[i][j]-alloc[i][j];
        int l=0;
        for(int z=0;z<(p*p);z++)
        {
        for(int i=0;i<p;i++)
        {       if(flag[i]==0)
                {

                j=0;
                while(j<r)
                {       if(need[i][j]<=avail[j])
                        {
                                if(j==r-1)
                                {       flag[i]=1;
                                        for(int k=0;k<r;k++)
                                                avail[k]+=alloc[i][k];
                                        safe[l++]=i;
                                        cout<<"\n"<<i<<"\n";
                                } j++;

                        }
                        else break;
                }
                }
        }
        for(int i=0;i<p;i++)
        {
                if(flag[i]==1)
                {       if(i==r-1)
                        {
                                cout<<"\nThe safe sequence is:\n";
                                for(j=0;j<p;j++)
                                        cout<<"P"<<safe[j]<<" ";
                                return;
                       }
                }
        }
        }
        cout<<"\nSystem is not in safe state\n";
}
void bankalg::request_resource()
{
        int m;
        int *request=new int[r];
        cout<<"Enter the the new request:\nProcess which is requesting:";
        cin>>m;
        cout<<"Request:";
        for(int i=0;i<r;i++)
                cin>>request[i];
        int i=0;
        while(i<r)
        {
                if(request[i]<=need[m][i])
                        i++;
                else
                {
                        cout<<"Error.";
                        exit(1);
                }
        }
        i=0;
        while(i<r)
        {
                if(request[i]<avail[i])
                        i++;
                else
                        break;
        }
        if(i==3)
        {
                for(int i=0;i<r;i++)
                        avail[i]=work[i];
                for(int i=0;i<r;i++)
                {
                        avail[i]=avail[i]-request[i];
                        alloc[m][i]+=request[i];
                        need[m][i]-=request[i];
                }
                safe_sequence();
        }
        else
                cout<<"Request cannot be granted.";
}
int main()
{
        int choice;
        bankalg ob;
        ob.read();
        ob.safe_sequence();
        cout<<"\nIf you want to enter a new request press 1. Else press 2.\n";
        cin>>choice;
        switch(choice)
        {
                case 1:ob.request_resource();
                        break;
                case 2:exit(1);
        }
}


Detection algorithm



#include<iostream>
using namespace std;
class bankalg
{
        int **alloc;
        int **request;
        int *work;
        int *avail;
        int *safe;
        int *flag;
        int p,r;
        public:
        void read();
        void safe_sequence();
        void request_resource();
};
void bankalg::read()
{
        int i,j;
        cout<<"\nEnter the total number of processes\n";
        cin>>p;
        cout<<"\nEnter the number of resource types\n";
        cin>>r;
        alloc=new int* [p];
        request=new int* [p];
        avail=new int[r];
        work=new int[r];
        for(i=0;i<p;i++)
        {
                alloc[i]=new int[r];
                request[i]=new int[r];
        }
        cout<<"\nEnter the allocated resources of each process\n";
        for(i=0;i<p;i++)
        {
                cout<<"P"<<i<<":";
                for(j=0;j<r;j++)
                        cin>>alloc[i][j];
        }
        cout<<"\nEnter the request of each process\n";
        for(i=0;i<p;i++)
        {
                cout<<"P"<<i<<":";
                for(j=0;j<r;j++)
                        cin>>request[i][j];
        }
        cout<<"\nEnter the available resources of each type\n";
        for(i=0;i<r;i++)
                cin>>avail[i];
        for(i=0;i<r;i++)
                work[i]=avail[i];
}
void bankalg::safe_sequence()
{
        int i,j;
        safe=new int[p];
        flag=new int[p];
        for(i=0;i<p;i++)
        {
                flag[i]=0;

        }
        int l=0;
        for(int z=0;z<(p*p);z++)
        {
        for(int i=0;i<p;i++)
        {       if(flag[i]==0)
                {

                j=0;
                while(j<r)
                {       if(request[i][j]<=avail[j])
                        {
                                if(j==r-1)
                                {       flag[i]=1;
                                        for(int k=0;k<r;k++)
                                                avail[k]+=alloc[i][k];
                                        safe[l++]=i;
                                        cout<<"\n"<<i<<"\n";
                                } j++;

                        }
                        else break;
                }
                }
        }
        for(int i=0;i<p;i++)
        {
                if(flag[i]==1)
                {       if(i==r-1)
                        {
                                cout<<"\nThe safe sequence is:\n";
                                for(j=0;j<p;j++)
                                        cout<<"P"<<safe[j]<<" ";
                                return;
                       }
                }
                else break;
        }
        }
        cout<<"\nSystem is not in safe state\n";
}
void bankalg::request_resource()
{
        int *req=new int[r];
        int m;
        cout<<"Enter the the new request:\nProcess which is requesting:";
        cin>>m;
        cout<<"Request:";
        for(int i=0;i<r;i++)
        {
                cin>>req[i];
                request[m][i]+=req[i];
        }
        for(int i=0;i<r;i++)
                avail[i]=work[i];
        safe_sequence();

}
int main()
{
        int choice;
        bankalg ob;
        ob.read();
        ob.safe_sequence();
        cout<<"\nIf you want to enter a new request press 1. Else press 2.\n";
        cin>>choice;
        switch(choice)
        {
                case 1:ob.request_resource();
                        break;
                case 2:exit(1);
        }
}

FCFS:



#include<iostream>
using namespace std;
class theatre
{
        int a[10];
        int size;
        public:
        void tickets();
        void waitingtime();
};
void theatre::tickets()
{
        cout<<"Enter number of ppl:";
        cin>>size;
        cout<<"Enter number of tickets required by each person:";
        for(int i=0;i<size;i++)
                cin>>a[i];
        cout<<"Number of tickets for each person is:\n";
        for(int i=0;i<size;i++)
                cout<<a[i]<<"\n";
}
void theatre::waitingtime()
{
        float w=0,r=0,t=0;
        int i;
        for(i=0;i<size-1;i++)
        {
                t=t+a[i];
                w=w+t;
                r=r+t;
        }
        r=r+t+a[i];
        cout<<"Average waiting time is:"<<w/size;
        cout<<"Average turnaround time is:"<<r/size;
}
int main()
{
        theatre ob;
        ob.tickets();
        ob.waitingtime();
}


Priority

[cse2b523@localhost ~]$ vi priority.cpp
#include<iostream>
using namespace std;
class tkt
{
 int b[20],k,n,a[20],c[20];
 float awt,att,t,j;
 int co,temp,temp1;
 public:
  void read();
  void display();
  void time();
};
void tkt::read()
{
   cout<<"Enter the number of processes\n";
   cin>>n;
   cout<<"Enter the cpu burst time of each process\n";
   for(int i=1;i<n+1;i++)
   {
    cout<<"The cpu burst time " <<i<< " process is \n";
    cin>>b[i];
   }
   cout<<"Enter the priority of each order\n";
    for(int i=1;i<n+1;i++)
     {
      cout<<"The priority of "<<i<<" is ";
      cin>>c[i];
     }
    for(int i=1;i<n+1;i++)
       for(int j=1;j<i;j++)
        {
                if(c[j]>c[i])
                {
                        temp=b[j];
                        b[j]=b[i];
                        b[i]=temp;
                        temp1=c[j];
                        c[j]=c[i];
                        c[i]=temp1;
                }
        }
}
void tkt:: display()
{
   cout<<"The process in the queue is \n";
   for(int i=1;i<n+1;i++)
   {
     cout<<i<<" process  "<< b[i] <<"cpu burst time \n";
   }
}
void tkt::time()
{
   co=0;
   t=0;
   j=0;
   for(int i=1;i<=n;i++)
     {
       co=co+b[i];
       a[i]=co;
     }
 for(int i=1;i<n;i++)
      {
        t=t+a[i];
      }
        awt=t/n;
        cout<<"The average waiting time is "<<awt<<"\n";
        for(int i=1;i<=n;i++)
         {
           j=j+a[i];
         }
          att=j/n;
          cout<<"The average turn around time is "<<att;
}
int main()
{
   tkt ob;
   ob.read();
   ob.display();
   ob.time();
}

Round Robin


#include<iostream>
using namespace std;
class roundrobin
{
public:
 int a[100],b[100],c[100],d[100],e[100],m,p;
  void getinfo();
  void calculations();
float n;
};
void roundrobin::getinfo()
{
 cout<<"enter the no of processes\n";
 cin>>n;
 cout<<"enter each process burstime\n";
 for(int i=0;i<n;i++)
 {
         cin>>a[i];
         d[i]=a[i];
 }
 cout<<"enter the quantum time\n";
 cin>>m;
}
void roundrobin::calculations()
{
 int s=0;
for(int i=0;i<n;i++)
c[i]=0;
for(int i=0;i<n;i++)
 {
   if(a[i]%m==0)
   s=s+a[i]/m;
   else
   s=s+(a[i]/m+1);
 }

cout<<" the number titrations  " << s <<"\n";
int i=0,j=0;
 while(i<s)
 {
   if(j==n)
    {
     j=0;
    }
   else
    {
        if(a[j]==0)
         j++;
        else if(a[j]>m)
        {
           b[i++]=m;
           a[j++]=a[j]-m;

        }
        else if(a[j]==m)
        {
          b[i++]=m;
          int sum=0;
          for(int k=0;k<i;k++)
          sum=sum+b[k];
          c[j]=sum-d[j];
          e[j]=sum;
          a[j++]=0;

        }
        else if(a[j]<m)
        {
          b[i++]=a[j];
          int sum=0;
          for(int k=0;k<i;k++)
          sum=sum+b[k];
          c[j]=sum-d[j];
          e[j]=sum;
          a[j++]=0;

        }
     }
}
cout<<"\n";
for(int i=0;i<s;i++)
cout<<b[i]<<"\t";
cout<<"\n";
float awt,att; int wt=0,tt=0;
for(int i=0;i<n;i++)
wt=wt+c[i];
awt=wt/n;
cout<<"average waiting time is:"<<awt<<"\n";
for(int i=0;i<n;i++)
tt=tt+e[i];
att=tt/n;
cout<<"\naverage turn around time is:"<<att<<"\n";
}

int main()
{
 roundrobin r;
 r.getinfo();
 r.calculations();
}


SJF


#include<iostream>
using namespace std;
class theatre
{
        int a[10];
        int size;
        public:
        void tickets();
        void sort();
        void waitingtime();
};
void theatre::tickets()
{
        cout<<"Enter number of ppl:";
        cin>>size;
        cout<<"Enter number of tickets required by each person:";
        for(int i=0;i<size;i++)
                cin>>a[i];
        cout<<"Number of tickets for each person is:\n";
        for(int i=0;i<size;i++)
                cout<<a[i]<<"\n";
}
void theatre::sort()
{
        for(int i=0;i<size-1;i++)
        {
                int small=i;
                for(int j=i+1;j<size;j++)
                {
                        if(a[j]<a[small])
                                small=j;
                }
                int t=a[small];
                a[small]=a[i];
                a[i]=t;
        }
        for(int i=0;i<size;i++)
                cout<<a[i]<<"\t";
}
void theatre::waitingtime()
{
        float w=0,c=size-1,t=0;
        int i;
        for(i=0;i<size-1;i++)
        {
                w=w+a[i]*c;
                c--;
        }
        for(int i=0;i<size;i++)
                t=t+a[i];
        t=t+w;
        cout<<"Average waiting time is:"<<w/size;
        cout<<"Average turnaround time is:"<<t/size;
}
int main()
{
        theatre ob;
        ob.tickets();
        ob.sort();
        ob.waitingtime();
}





No comments:

Post a Comment