Life of student

All about Programming , C++ ,Fedora

  • Blog Stats

    • 17,927 hits
  • Categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email. I too can\'t see who had subscribed my blog :(

    Join 11 other subscribers

My second year OS assignment DEAD LOCK DETECTION

Posted by pankaj4u4m on August 9, 2009

// Author:pankaj
#include <iostream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <list>
#include <stack>
#include <vector>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <queue>
#include <iterator>
#include <sstream>
#include <deque>
#include <set>
#include <map>
#include <numeric>
#include <functional>

using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define FORR(i,a,b) for (int i=(a);i>=(b);i--)
#define REP(i,a,b) for (i=(a);i<(b);i++)
#define REPR(i,a,b) for (i=(a);i>=(b);i--)
#define SET(A,p) memset(A,p,sizeof(A))
#define CPY(A,B) memcpy(A,B,sizeof(A))
#define ALL(A) A.begin(), A.end()
#define EACH(i,A) for(typeof(A.begin()) i=A.begin();i!=A.end();i++)
#define pb push_back
#define sz size()
#define cl clear()
#define fi first()
#define se second

typedef long long int ill;
typedef unsigned long long int ull;
typedef vector <int> VI;
typedef vector <double> VD;
typedef pair <int, int>IP;
typedef vector<IP> VP;

class proc
{
public:
int allocated[100],max[100],used[100],need[100];

};
void dead()                   //if dead lock detected program exit
{
cout<<"Deadlocak detected";
exit(1);
}
int main(int argc, char** argv)
{

proc p[100];
int n,m,temp=0,flag[100]={0};int resource[100],free[100];
cout<<"\nEnter the no total number of resource:";
cin>>n;
cout<<"\nEnter resources for:\n";
FOR(i,0,n)
{
cout<<"\n "<<i+1<<" :";
cin>>resource[i];

}
cout<<"\nEnter the number of process:";
cin>>m;
cout<<"\nEnter the resources for each processes";
cout<<"\n   allocated     maximux_required";
FOR(i,0,m)
{
cout<<"\np "<<i+1<<" :";
FOR(j,0,n)
{
cin>>p[i].allocated[j];
}
FOR(j,0,n)
{
cin>>p[i].max[j];
}
FOR(j,0,n)
{
p[i].need[j]=p[i].max[j]-p[i].allocated[j];

}
}
FOR(j,0,n)
{
free[j]=resource[j];

}
FOR(i,0,n)
{
if(free[i]<0){
dead();}
}
FOR(i,0,m)
{
FOR(j,0,n)
{
if(free[j]>=p[i].need[j]&&flag[i]==0)
{
flag[i]=1;
}
else if(free[j]<p[i].need[j])

{
flag[i]=0;break;
}

}
if(flag[i]==1)
{
cout<<"P"<<i+1<<" ";
flag[i]=2;
FOR(j,0,n)
{
free[j]+=p[i].max[j];

}
i=-1;

}
}
FOR(i,0,m)
{
if(flag[i]==0)
dead();
}

return (EXIT_SUCCESS);
}

Leave a comment