Skip to content

Instantly share code, notes, and snippets.

@mukeshkdangi
Created January 12, 2014 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mukeshkdangi/8384207 to your computer and use it in GitHub Desktop.
Save mukeshkdangi/8384207 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<conio.h>
int isless(int k, int m,int ned[10][10],int wrk[] ){
int i;
for(i=0;i<m;i++){
if(ned[k][i]>wrk[i])
return 0;
}
return 1;
}
int allfin(int fin[],int n){
int i;
for(i=0;i<n;i++){
if(fin[i]==0)
return 0;
}
return 1;
}
int main(){
int i,j,k,ned[10][10],max[10][10],seq[10],avl[10],alloc[10][10],fin[10],wrk[10],n,m,l=0,req[10],req1;
printf("\nEnter teh no of process :");
scanf("%d",&n);
printf("\nEnter the no of recource :");
scanf("%d",&m);
printf("\nEntre the available resources ::");
for(i=0;i<m;i++){
scanf("%d",&avl[i]);
}
printf("\nEnter the process detaile onr by one -----:");
for(i=0;i<n;i++){
printf("for process %d",i+1);
for(j=0;j<m;j++){
printf("\nEnter the: max :amount for recorce R%d",j+1);
scanf("%d",&max[i][j]);
printf("\nEnter the:allocate :amount for recorce R%d",j+1);
scanf("%d",&alloc[i][j]);
ned[i][j]=max[i][j]-alloc[i][j];
}
fin[i]=0;
}
printf("\nEnter to which proces s u want to take :");
scanf("%d",&req1);
printf("\neNTRE THE REQUESTED AMOUNT :");
for(i=0;i<m;i++){
scanf("%d",&req[i]);
}
int less=0;
for(i=0;i<m;i++){
if(ned[req1-1][i]<req[i]){
less=1;
break;
}
}
if(less){
printf("\nCon not go :");
return 0;
}
less=0;
for(i=0;i<m;i++){
if(avl[i]<req[i]) {
less=1;
break;
}
}
if(less){
printf("\nProcess p%d have to wait ",req1);
return 0;
}
for(i=0;i<m;i++){
avl[i]-=req[i];
ned[req1-1][i]-=req[i];
alloc[req1-1][i]+=req[i];
wrk[i]=avl[i];
}
/*for(i=0;i<n;i++){
wrk[i]=avl[i];
}*/
for(i=0;i<n;i++){
if((!fin[i])&&(isless(i,m,ned,wrk))){
for(j=0;j<m;j++)
wrk[i]+=alloc[i][j];
fin[i]=1;
seq[l++]=i ;
i=i-1;
}}
if(allfin(fin,n)){
printf("\nour sys is in free dead:");
for(i=0;i<l;i++){
printf("p%d",seq[i]+1);
}
}
else {
printf("\nSystem is in dead lock:");
}
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment