Bankers algorithm (Oparating system)
Deadlock-
Banker's Algorithm
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_PROCESSES 50
//here, Allocation[100][10] Types of resources = 10 Maximum boundary assumed.
//Maximum of 50 processes & 10 resources can be handles by the system.
int Allocation[100][10],Max[100][10],Need[100][10],Available[10],Total[10]={0,0,0};
int no_of_resources,no_of_processes;
static int finish[MAX_PROCESSES];
int finishAll()
{
int i;
for(i=0 ; i<no_of_processes ; i++)
if(finish[i]==0)
return 0;
return 1;
}
int safe()
{
int i,j,flag=1;
int tAllocation[100][10],tNeed[100][10],tAval[10];
//Copy available resources to temporary available resources
for(i=0 ; i< no_of_resources ; i++)
tAval[i]=Available[i];
for(i=0 ; i< no_of_processes ; i++)
{
finish[i]=0; // all finish flags made false
// Allocation and need are copied to temporary tables
for(j=0; j<no_of_resources; j++)
{
tAllocation[i][j]=Allocation[i][j];
tNeed[i][j]=Need[i][j];
}
}
printf("\n\nSafe Sequence : ");
while(!finishAll())
{
flag=1;
for(i=0;i<no_of_processes;i++)
{
if(finish[i]==1)
continue;
//if the process "i" has been "finished"
//continue to next process
for(j=0;j<no_of_resources;j++)
if(tNeed[i][j]>tAval[j])
break;
//if need > available: request cannot be fulfilled
if(j==no_of_resources)// all needs are in availability range
{
finish[i]=1;
//make finish flag for i'th process true
printf(" P%d",i);
// the process number is appended to safe sequence
flag=0;
// flag : toggled to know if process is finished
//changes are refleceted to temporary tables
for(j=0;j<no_of_resources;j++)
{
tAval[j]-=tNeed[i][j];
tAllocation[i][j]+=tNeed[i][j];
tNeed[i][j]=0;
}
//When all required resources are with the process it will execute fully.
//this indicates process is finished
//deAllocation of resources
for(j=0;j<no_of_resources;j++)
{
tAval[j]+=tAllocation[i][j];
tAllocation[i][j]=0;
}
}
}//Outer for loop ends. It runs for all processes.
if(flag==1)return 0;
}
return 1;
}
void request()
{
int i,j,p,Req[10];
printf("\n\nRequest from which Process ? ");
scanf("%d",&i);
//i'th process is requesting resources
if(i>no_of_processes || i<0)
{
printf("\nError : Invalid Process ID, Process %d does not exist.",i);
return;
}
printf("\nEnter Request : ");
for(j=0;j<no_of_resources;j++)
scanf("%d",&Req[j]);
for(j=0;j<no_of_resources;j++)
if(Req[j]>Need[i][j])
break;
// process cannot request more than need
if(j<no_of_resources) printf("\nError : Invalid Request");
//if valid request
else
{
printf("\nRequest is Valid");
for(j=0;j<no_of_resources;j++)
if(Req[j] > Available[j])
break;
//If request is valid, but resources not available with system
// process will have to wait
if(j<no_of_resources)
printf("\nWAIT : Not enough resources available with system.");
//if request valid, and resources available
else
{
printf("\nRequired resources are Available.");
for(j=0;j<no_of_resources;j++)
{
Available[j]-=Req[j];
Allocation[i][j]+=Req[j];
Need[i][j]-=Req[j];
}
if(safe())
printf("\nSystem will remain in safe state.\nRequest granted.");
else
{
printf("\nSystem does not remain in safe state.");
printf("\nRequest NOT granted.");
//loading resources back
for(j=0;j<no_of_resources;j++)
{
Available[j]+=Req[j];
Allocation[i][j]-=Req[j];
Need[i][j]+=Req[j];
}
}
}
}
}
FindNeed()
{
int i,j,resourceCount;
char resource='A';
printf("\nNeed Table\n------------------------");
printf("\nProcess");
for(resourceCount=1; resourceCount<= no_of_resources; resourceCount++)
printf("\t%c ",resource++);
for(i=0;i<no_of_processes;i++)
{
printf("\n\nP%d : \t",i);
for(j=0;j<no_of_resources;j++)
{
Need[i][j]=Max[i][j]-Allocation[i][j];
printf("%d\t",Need[i][j]);
}
}
}
AcceptAllocationTable()
{
int i,j,resourceCount;
char resource='A';
printf("\nEnter instances of Resources Allocated to each process : ");
printf("\n\nAllocation\n-------------");
printf("\nProcess");
for(resourceCount=1; resourceCount<= no_of_resources; resourceCount++)
printf("\t%c ",resource++);
for(i=0;i<no_of_processes;i++)
{
printf("\nP%d : \t",i);
for(j=0;j<no_of_resources;j++)
{
scanf("%d",&Allocation[i][j]);
Total[j]+=Allocation[i][j];
}
}
}
AcceptMaxTable()
{
int i,j,resourceCount;
char resource='A';
printf("\n\nEnter Max Resource Requirements for each process : ");
printf("\n\nMAX\n-------------");
printf("\nProcess");
for(resourceCount=1; resourceCount<= no_of_resources; resourceCount++)
printf("\t%c ",resource++);
for(i=0;i<no_of_processes;i++)
{
printf("\nP%d : \t",i);
for(j=0;j<no_of_resources;j++)
{
scanf("%d",&Max[i][j]);
}
}
}
main()
{
int i,j,choice=1,resourceCount;
char resource='A';
printf("\nHow many types of resources ? ");
scanf("%d",&no_of_resources);
printf("\nHow many Processes ? ");
scanf("%d",&no_of_processes);
AcceptAllocationTable();
printf("\nEnter Available resources with system : ");
printf("\n\nAVAILABLE\n-------------");
printf("\nResType");
for(resourceCount=1; resourceCount<= no_of_resources; resourceCount++)
printf("\t%c ",resource++);
printf("\n\t ");
for(j=0;j<no_of_resources;j++)
{
scanf("%d",&Available[j]);
Total[j]+=Available[j];
}
resource='A'; // to print Total: again set to A
printf("\nTotal system Resources are : ");
printf("\n\nTOTAL\n-------------\n");
printf("\nTotal ");
for(resourceCount=1; resourceCount<= no_of_resources; resourceCount++)
printf("\t%c ",resource++);
printf("\n");
for(j=0;j<no_of_resources;j++)
printf("\t%d",Total[j]);
AcceptMaxTable();
FindNeed();
i(safe())
{
printf("\n\nStatus : System is in Safe state");
do
{
request();
printf("\n\nAny more request ? (yes=1/no=0): ");
scanf("%d",&choice);
}while(choice == 1);
}
else
printf("\nStatus : System is NOT in safe state");
//System is unsafe
}
Comments
Post a Comment