OS | Banker’s Algorithm without using no. of resources – implementation in C

The banker’s algorithm is a Resource Allocation and Deadlock Avoidance Algorithm that tests for safety by simulating the allocation for predetermined maximum possible amounts of all resources, then makes an “safe-state sequences” check to test for possible activities, before deciding whether allocation should be allowed to continue.

Algorithm

Following Data structures are used to implement the Banker’s Algorithm in C:

Let ‘n’ be the number of processes in the system.

Available :
It is a indicating the number of available resources of each type.
iniavi = Initial available of resource.

Max :
It is array of size ‘max[i]’ that defines the maximum demand of each process in a system.
max[ i ] = Maximum resource of Process.

Allocation :
It is array of size ‘allo[i]’ that defines the number of resources of each type currently allocated to each process.
alloc[ i ] = currently allocated resources of Process.
newAlloc[i] = Assign currently allocated resources to New Allocation.

Need :
It is array of size ‘need[i]’ that indicates the remaining resource need of each process.
need [ i ] = Need of resource.
for its execution.

Need [ i ] = Maximum [ i ] – Allocation [ i ]

Examples:

Input :
Number of process  : 5
Allocation Values  : {010, 200, 302, 211, 002}
Maximum Values     : {753, 322, 902, 222, 433}
Available Resource : 332 
Output : 
P1-> P3-> P4-> P0-> P2->

/* Bankers Algorithm implementation in C */
#include 
#include

int main()
{
int i, n, iniavi, alloc[10], max[10], avail[10], need[10], flag[10], safe[10];
clrscr();
printf(“\t\t\t\t BANKERS ALGORITHM\n”);
printf(“\t\t\t\t——————-\n\n”);
printf(“\nEnter the Number of process : “);
scanf(“%d”, &n);

/* Get Allocation & Maximum Values */
printf(“\nEnter the allocation and maximum values\n”);
for (i=0; i<n;i++)
{
printf("\nEnter Allocation Value P%d : \t", i);
scanf("%d", &alloc[i]);

printf("Enter Maximum Value of P%d: \t", i);
scanf("%d", &max[i]);
}

printf("\nEnter Initial available : \t");
scanf("%d", &iniavi);

/* Need calculation */
for(i=0; i<n; i++)
{
need[i] = max[i] – alloc[i];
}

/* Print entered Inputs */
printf("\nAvailable\tAllocation\tMaximum\t\tNeed");
printf("\n—————————————————–\n");
printf("%d", iniavi);
for(i=0; i<n; i++)
{
printf("\t\t%d\t\t%d\t\t%d\n", alloc[i], max[i], need[i]);
}
printf("\n—————————————————–\n");

/* Safe Sequence Calculation */
printf("\nSafe sequences\n\n");
i=0;
while(i<n)
{
if(need[i]<iniavi)

{

avail[i]=iniavi+alloc[i];

iniavi=avail[i];

safe[i+1]=i;

flag[i]=1;

printf("P%d->  ", i);
}
else
{
flag[i] = 0;
}
i++;
}

for(i=0; i<n; i++)
{
if(flag[i] != 1)
{
if(need[i]<iniavi)

{

avail[i]=iniavi+alloc[i];

safe[i+1]=i;

flag[i]=1;

printf("P%d->  ", i);
}
}
}
return 0;
}

Output

 

This Post Has 4 Comments

    1. БЛАГОДАРЮ ВАС

Leave a Reply

OS | Bankers Algorithms implementation in C without Resource request

Close Menu