You are on page 1of 6

#include<bits/stdc++.

h>

using namespace std;

int maximum[10][10],need[10][10],allocation[10][10];

int available[10];

   vector<int> q;

bool check(int work[],int curr[],int m)

    for(int i=0;i<m;i++)

    {

    if(curr[i]>work[i])

    return false;

    }

    return true;

bool safety(int m,int n)

    int work[m];

    for(int i=0;i<m;i++)

    work[i]=available[i];

    bool finished[n];

    for(int i=0;i<n;i++)

    finished[i]=false;

    int k;

    while(k<n)

    {

    for(int i=0;i<n;i++)

    {

        int curr[m];

        for(int j=0;j<m;j++)

        curr[j]=need[i][j];
        bool b = check(work,curr,m);

        if(!finished[i]&&b)

        {

            finished[i]=1;

            for(int j=0;j<m;j++)

            work[j]+=allocation[i][j];

            q.push_back(i);

        }

    }

    k++;

    }

    for(int i=0;i<m;i++)

    {

        if(!finished[i])

        return false;

    }

    return true;

bool request_res(int request[],int i,int m)

    int curr[m];

    for(int j=0;j<m;j++)

    curr[j]=need[i][j];

    if(check(curr,request,m))

    {

        int allo[m];

        for(int j=0;j<m;j++)

        allo[j]=available[j];

        if(check(allo,request,m))
        {

            for(int k=0;k<m;k++)

            {

                available[k]-=request[k];

                allocation[i][k]+=request[k];

                need[i][k]-=request[k];

            }

            return true;

        }

        else

        {

            cout<<"Resources are currently not available. Please wait.";

            return false;

        }

    }

    else

    {

        cout<<"Request exceeded need capacity"<<endl;

        return false;

    }

int main()

    int n,m;

    cout<<"Enter number of resources:\t";

    cin>>m;

    cout<<"Enter number of processes:\t";

    cin>>n;

    for(int i=0;i<n;i++)

    {
        cout<<"Enter maximum need of "<<i<<"th process:"<<endl;

        for(int j=0;j<m;j++)

        {

            cout<<"Enter demand for "<<j<<"th resource:\t";

            cin>>maximum[i][j];

        }

    }

    for(int i=0;i<n;i++)

    {

        cout<<"Enter allocated resources to "<<i<<"th process:"<<endl;

        for(int j=0;j<m;j++)

        {

            cout<<"Enter allocation for "<<j<<"th resource:\t";

            cin>>allocation[i][j];

        }

    }

    for(int i=0;i<n;i++)

    {

        for(int j=0;j<m;j++)

        {

            need[i][j]=maximum[i][j]-allocation[i][j];

        }

    }

    cout<<"Enter number of available resources";

    for(int i=0;i<m;i++)

    cin>>available[i];

    cout<<"Maximum Demand Table\n";

    for(int i=0;i<n;i++)

    {
        for(int j=0;j<m;j++)

        cout<<maximum[i][j]<<" ";

        cout<<endl;

    }

    cout<<"Allocation Table\n";

    for(int i=0;i<n;i++)

    {

        for(int j=0;j<m;j++)

        cout<<allocation[i][j]<<" ";

        cout<<endl;

    }

    cout<<"Need Table\n";

    for(int i=0;i<n;i++)

    {

        for(int j=0;j<m;j++)

        cout<<need[i][j]<<" ";

        cout<<endl;

    }

    int ch;

    while(ch!=3)

    {

        cout<<"1.Safety Algorithm\n2.Request Resource Algorithm\n3. Exit"<<endl;

        cin>>ch;

        switch(ch)

        {

            case 1:

                if(safety(m,n))

                {

                cout<<"System is in safe state"<<endl;

                cout<<"Sequence of Processes"<<endl;
                for(int i=0;i<q.size();i++)

                    {

                    cout<<"Process "<<q[i]<<"->";

                    }

                }

                else

                cout<<"System is not in safe state"<<endl;

                break; 

            case 2:

                    int request[m];

                    int pn;

                    cout<<"Enter process number";

                    cin>>pn;

                    pn--;

                    cout<<"Enter request array";

                    for(int i=0;i<m;i++)

                    cin>>request[i];

                    if(request_res(request,pn,m))

                    cout<<"Request granted";

                    else

                    cout<<"Request denied";

                    break;

        }

    }

    

You might also like