This requires that the
system has some information available up front. Each process declares the
maximum number of resources of each type which it may need. Dynamically examine
the resource allocation state to ensure that there can never be a circular-wait
condition.
The system's
resource-allocation state is defined by the number of available and allocated
resources, and the maximum possible demands of the processes. When a process
requests an available resource, the system must decide if immediate allocation
leaves the system in a safe state.
The system is in a safe
state if there exists a safe sequence of all processes:
Sequence < P1,
P2, ... Pn > is safe for the current
allocation state if, for each Pi, the resources which Pi can
still request can be satisfied by
- the currently available resources plus
- the resources held by all of the Pj's,
where j < i.
If the system is in a safe
state, there can be no deadlock. If the system is in an unsafe state, there is
the possibility of deadlock.
Example: consider a system with 12 magnetic
tapes and 3 processes (P0, P1, and P2):
available = 3
|
|||
Process
|
Maximum Needs
|
Holding
|
Needs
|
P0
|
10
|
5
|
5
|
P1
|
4
|
2
|
2
|
P2
|
9
|
2
|
7
|
Is the system in a safe
state? If so, which sequence satisfies the safety criteria?
available = 2
|
|||
Process
|
Maximum Needs
|
Holding
|
Needs
|
P0
|
10
|
5
|
5
|
P1
|
4
|
2
|
2
|
P2
|
9
|
3
|
6
|
//Bankers algorithm for deadlock avoidance.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no of resource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter the allocated resources:");
for(i=1;i<=n;i++)
for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("Enter the process making the new request:");
scanf("%d",&p);
printf("Enter the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter the processes which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i);
scanf("%d%d",&block[i],&run[i]);
}
}
block[p]=0;
run[p]=0;
for(k=1;k<=r;k++)
{
j=0;
for(i=1;i<=n;i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[k]<0)
{
u=1;break;
}
}
if(u==0)
{
for(k=1;k<=r;k++)
simalloc[k]=totalloc[k];
for(s=1;s<=n;s++)
for(i=1;i<=n;i++)
{
if(active[i]==1)
{
j=0;
for(k=1;k<=r;k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;break;
}
}
}
if(j==0)
{
active[i]=0;
for(k=1;k<=r;k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1;k<=r;k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
else
{
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
getch();
}
OUTPUT
No comments:
Post a Comment