You are on page 1of 8

#include<stdio.h> #include<stdlib.

h> #define NOPROCESSES 4 #define NORESOURCES 3 int AvlRes[NORESOURCES]; int MaxDemandMtx[NOPROCESSES][NORESOURCES]; int AlcMtx[NOPROCESSES][NORESOURCES]; int NeedMtx[NOPROCESSES][NORESOURCES]; int SafePath[NOPROCESSES]; int NRes,NPrs,SafePathIndex=0,opt,i,j,k,trace=0; void List(int LAvlRes[NORESOURCES], int LAlcMtx[NOPROCESSES][NORESOURCES], int LNeedMtx[NOPROCESSES][NORESOURCES]); void Allocate(); void Free(); int main() { do{ printf("\nenter no.of resources(max %d):",NORESOURCES); scanf("%d",&NRes); }while(NRes>NORESOURCES); printf("enter quantity:"); for(i=0;i<NRes;i++) printf("R%d",i+1); printf(":"); for(i=0;i<NRes;i++) scanf("%d",&AvlRes[i]); do{ printf("enter no. of processes(Max %d):", NOPROCESSES); scanf("%d",&NPrs); }while(NPrs>NOPROCESSES); for(i=0;i<NPrs;i++){ printf("enter maximum requirment for P%d:",i+1); for(j=0;j<NRes;j++) printf("R%d",j+1); printf(":"); for(j=0;j<NRes;j++) do{ scanf("%d",&MaxDemandMtx[i][j]); if(MaxDemandMtx[i][j]>AvlRes[j]){ printf("resource %d requirement is mare than maximum available %d\n",j+1,MaxDemandMtx[i][j]); printf("enter properly\n");

continue; } }while(MaxDemandMtx[i][j]>AvlRes[j]); } printf("do you want tracing(1 for yes,0 for no):"); scanf("%d",&trace); for(i=0;i<NPrs;i++) for(j=0;j<NRes;j++) NeedMtx[i][j]=MaxDemandMtx[i][j]-AlcMtx[i][j]; while(1){ printf("1-allocation\n"); printf("2-free\n"); printf("3-list\n"); printf("4-quit:"); scanf("%d",&opt); switch(opt){ case 1:Allocate();break; case 2:Free();break; case 3:List(AvlRes,AlcMtx,NeedMtx);break; case 4:exit(0); default : printf("invalid option\n"); } } } void List(int LAvlRes[NORESOURCES], int LAlcMtx[NOPROCESSES][NORESOURCES],int LNeedMtx[NOPROCESSES][NORESOURCES]) { int i,j; printf("\nsystem resources available\n"); for(i=0;i<NRes;i++) printf("\tR%d",i+1); printf("\n"); for(i=0;i<NRes;i++) printf("\t%d",LAvlRes[i]); printf("\n"); printf("\t\tMaximum"); printf("\t\tAllocation"); printf("\t\tNeed of resource\n");

for(j=0;j<NRes;j++) printf("\tR%d",j+1); for(j=0;j<NRes;j++) printf("\tR%d",j+1); for(j=0;j<NRes;j++) printf("\tR%d",j+1); printf("\n"); for(j=0;j<NPrs;j++){ printf("P%d",j+1); for(i=0;i<NRes;i++) printf("\t%d",MaxDemandMtx[j][i]); for(i=0;i<NRes;i++) printf("\t%d",LAlcMtx[j][i]); for(i=0;i<NRes;i++) printf("\t%d",LNeedMtx[j][i]); printf("\n"); } } void Allocate() { int PrsNo,PrsNoPlus1; int ResReq[NORESOURCES]; int TmpAlcMtx[NOPROCESSES][NORESOURCES]; int TmpNeedMtx[NOPROCESSES][NORESOURCES]; int TmpAvlRes[NORESOURCES]; int PrsFinish[NOPROCESSES]; int DeadLock,npc; int i,j; do{ printf("enter process number(1 to %d):",NPrs); scanf("%d",&PrsNoPlus1); }while(PrsNoPlus1>NPrs); PrsNo=PrsNoPlus1-1; printf("enter P%d:",PrsNoPlus1); for(i=0;i<NRes;i++) printf("R%d",i+1); printf(":"); for(i=0;i<NRes;i++) scanf("%d",&ResReq[i]); for(i=0;i<NRes;i++){ if(NeedMtx[PrsNo][i]<ResReq[i]){ printf("more requirement than max asked \n");

return; } if(AvlRes[i]<ResReq[i]){ printf("resources are not available\n"); return; } } for(i=0;i<NRes;i++) TmpAvlRes[i]=AvlRes[i]; for(i=0;i<NPrs;i++) for(j=0;j<NRes;j++) TmpAlcMtx[i][j]=AlcMtx[i][j]; for(i=0;i<NPrs;i++) for(j=0;j<NRes;j++) TmpNeedMtx[i][j]=NeedMtx[i][j]; for(i=0;i<NRes;i++){ TmpAvlRes[i]=TmpAvlRes[i]-ResReq[i]; TmpNeedMtx[PrsNo][i]=TmpNeedMtx[PrsNo][i]-ResReq[i]; TmpAlcMtx[PrsNo][i]=TmpAlcMtx[PrsNo][i]+ResReq[i]; } for(i=0;i<NPrs;i++) SafePath[i]=-1; SafePathIndex=0; if(trace==1) List(TmpAvlRes,TmpAlcMtx,TmpNeedMtx); DeadLock=0;npc=0; for(i=0;i<NPrs;i++) PrsFinish[i]=0; for(i=0;(i<NPrs)&&(DeadLock==0)&&(npc<NPrs);j++){ DeadLock=1; for(j=0;j<NPrs;j++) if(PrsFinish[j]==0){ for(k=0;k<NRes;k++) if(TmpNeedMtx[j][k]>TmpAvlRes[k]) break; if(k==NRes){ int totres=0; PrsFinish[j]=1;npc++; DeadLock=0; SafePath[SafePathIndex++]=j; if(trace==1){ printf("process %d can be completed\n",j+1); for(totres=0,k=0;k<NRes;k++) totres+=TmpAlcMtx[j][k];

} for(k=0;k<NRes;k++){ TmpAvlRes[k]+=TmpAlcMtx[j][k]; TmpAlcMtx[j][k]=0;TmpNeedMtx[j][k]=NeedMtx[j][k]; } if(trace==1) if(totres>0){ printf("after completing process %d\n",j+1); List(TmpAvlRes,TmpAlcMtx,TmpNeedMtx); } } } }

if(DeadLock==1){ if(trace==1) for(i=0;i<NPrs;i++) if(PrsFinish[i]==0) printf("process %d cannot be completed\n",i+1); printf("unsafe state resources are not allocated \n"); return; } printf("safe state-resources are allocated\n"); for(j=0;j<NRes;j++){ AlcMtx[PrsNo][j]=AlcMtx[PrsNo][j]+ResReq[j]; NeedMtx[PrsNo][j]=NeedMtx[PrsNo][j]-ResReq[j]; AvlRes[j]=AvlRes[j]-ResReq[j]; } printf("safe path"); for(j=0;j<NPrs;j++) printf("%2d",(SafePath[j]+1)); printf("\n"); if(trace==1) List(AvlRes,AlcMtx,NeedMtx); } void Free() { int PrsNo,PrsNoPlus1; int ResRel[NORESOURCES]; int i,j; do{

printf("enter process num(1 to %d):",NPrs); scanf("%d",&PrsNoPlus1); }while(PrsNoPlus1>NPrs); PrsNo=PrsNoPlus1-1; printf("enter P %d:",PrsNoPlus1); for(i=0;i<NRes;i++) printf("R%d",i+1); printf(":"); for(i=0;i<NRes;i++) scanf("%d",&ResRel[i]); for(i=0;i<NRes;i++) if(AlcMtx[PrsNo][i]<ResRel[i]){ printf("more relese than allocated\n"); return; } for(i=0;i<NRes;i++){ AvlRes[i]=AvlRes[i]+ResRel[i]; NeedMtx[PrsNo][i]=NeedMtx[PrsNo][i]+ResRel[i]; AlcMtx[PrsNo][i]=AlcMtx[PrsNo][i]-ResRel[i]; } if(trace==1) List(AvlRes,AlcMtx,NeedMtx); }

OUTPUT: enter no.of resources(max 3):3 enter quantity:R1R2R3:9 3 6 enter no. of processes(Max 4):4 enter maximum requirment for P1:R1R2R3:3 2 2 enter maximum requirment for P2:R1R2R3:6 1 2 enter maximum requirment for P3:R1R2R3:3 1 4 enter maximum requirment for P4:R1R2R3:4 2 2 do you want tracing(1 for yes,0 for no):0 1-allocation 2-free 3-list 4-quit:1 enter process number(1 to 4):1 enter P1:R1R2R3:1 0 0 safe state-resources are allocated safe path 1 2 3 4 1-allocation 2-free 3-list 4-quit:1 enter process number(1 to 4):2 enter P2:R1R2R3:6 1 2 safe state-resources are allocated safe path 1 2 3 4 1-allocation 2-free 3-list 4-quit:1 enter process number(1 to 4):3 enter P3:R1R2R3:2 1 1 safe state-resources are allocated safe path 2 3 4 1 1-allocation 2-free 3-list 4-quit:1 enter process number(1 to 4):4 enter P4:R1R2R3:0 0 2 safe state-resources are allocated safe path 2 3 4 1 1-allocation 2-free 3-list

4-quit:3 system resources available R1 R2 R3 0 1 1 Maximum Allocation R1 R2 R3 R1 R2 R3 P1 3 2 2 1 0 0 2 P2 6 1 2 6 1 2 0 P3 3 1 4 2 1 1 1 P4 4 2 2 0 0 2 4 1-allocation 2-free 3-list 4-quit:1 enter process number(1 to 4):1 enter P1:R1R2R3:0 0 1 safe state-resources are allocated safe path 2 4 1 3 1-allocation 2-free 3-list 4-quit:1 enter process number(1 to 4):2 enter P2:R1R2R3:0 0 1 more requirement than max asked 1-allocation 2-free 3-list 4-quit:3 system resources available R1 R2 R3 0 1 0 Maximum Allocation R1 R2 R3 R1 R2 R3 P1 3 2 2 1 0 1 2 P2 6 1 2 6 1 2 0 P3 3 1 4 2 1 1 1 P4 4 2 2 0 0 2 4 1-allocation 2-free 3-list 4-quit:4

Need of resource R1 R2 R3 2 2 0 0 0 3 2 0

Need of resource R1 R2 R3 2 1 0 0 0 3 2 0

You might also like