C program of Banker’s Algorithm to avoid Deadlock

# C program of Banker’s Algorithm to avoid Deadlock

1188
13
SHARE

C program of banker’s algorithm to avoid deadlock : Banker’s algorithm is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources, and then makes a “s-state” check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue.

How Banker’s Algorithm works ?

The Banker’s algorithm is run by the operating system whenever a process requests resources. The algorithm avoids deadlock by denying or postponing the request if it determines that accepting the request could put the system in an unsafe state (one where deadlock could occur). When a new process enters a system, it must declare the maximum number of instances of each resource type that may not exceed the total number of resources in the system. Also, when a process gets all its requested resources it must return them in a finite amount of time.

Let us see how to implement Banker’s algorithm in C which takes number of processes and resources as input shows state as output.

### C program of Banker’s Algorithm to avoid deadlock :

```#include<stdio.h>
#include<conio.h>
int main()
{
int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
int first,p,r,i,j,prc,count,t;
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
printf("----------------------------------------------------\n");
printf("----------------------------------------------------\n\n");
printf("\t BANKERS ALGORITHM IN C \n\n");
printf("Enter the no of processes : ");
scanf("%d",&p);
printf("\n\nEnter the no of resources : ");
scanf("%d",&r);
printf("\n\nEnter the claim for each process : ");
for(i=1;i<=p;i++)
{
printf("\nFor process %d : ",i);
for(j=1;j<=r;j++)
{
scanf("%d",&clm[i][j]);
}
}
printf("\n\nEnter the allocation for each process : ");
for(i=1;i<=p;i++)
{
printf("\nFor process %d : ",i);
for(j=1;j<=r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n\nEnter total no of each resource : ");
for(j=1;j<=r;j++)
scanf("%d",&rsrc[j]);
for(j=1;j<=r;j++)
{
int total=0;
avail[j]=0;
for(i=1;i<=p;i++)
{total+=alloc[i][j];}
avail[j]=rsrc[j]-total;
}
do
{
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
req[i][j]=clm[i][j]-alloc[i][j];
}
}
printf("\n\nAvailable resources are : ");
for(j=1;j<=r;j++)
{ printf(" ",avail[j]); }
printf("\nClaim matrix:\tAllocation matrix:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
printf("%d\t",clm[i][j]);
}
printf("\t\t\t");
for(j=1;j<=r;j++)
{
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
prc=0;
for(i=1;i<=p;i++)
{
if(comp[i]==0)//if not completed
{
prc=i;
for(j=1;j<=r;j++)
{
if(avail[j]=0)
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\nProcess ",prc,"runs to completion!");
count++;
for(j=1;j<=r;j++)
{
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
}
}
while(count!=p&&prc!=0);
if(count==p)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}```

We hope you all have enjoyed the above of Banker’s Algorithm to avoid deadlock. If you have any issues with above code or logic, ask us in form of comments.

Uniqueness is the key ! And Quality is the process that we follow!

SHARE
Previous articleC program to print reverse pyramid of alphabets
Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games. Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com

1. I get pleasure from, result in I discovered exactly what I was looking for. You have ended my four day lengthy hunt! God Bless you man. Have a nice day. Bye

• “Panem et cie”ensrsc. Pane poco, ma circenses parecchio per il popolino bue che DEVE essere distratto dalla realtÃ  quotidiana. ‘no schifo senza fine.Paolo, perchÃ¨ non ti “offri” come ospite con qualche vero esperto?

2. nice and thanks. very nice article and idea|Thank you for sharing your article I would always follow|Thank you all for your information and comments Regards ….|a very successful site. Also very revealing article. Thanks to the contributors.|I greatly appreciate all the info I’ve read here. I will spread the word about your blog to other people. Cheers.|nice, thank you so much ;)|really this is a nice web thank you thanks admin good post super messege|Ooohh, great information you write it very clean. I’m very lucky to get this details from you. ;-)}

3. I have been examinating out some of your stories and i can state pretty clever stuff. I will definitely bookmark your site.

4. Wow, superb blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is fantastic, as well as the content!. Thanks For Your article about C program of Banker's Algorithm to avoid Deadlock | C codechamp .

5. I think this is one of the most important information for me. And i am glad reading your article. But should remark on some general things, The website style is wonderful, the articles is really nice : D. Good job, cheers

6. Hi,
This code was useful, but i could not understand much. Can you explain me the use of each array. I want some explanation. Kindly help me….

7. Outstanding post, I conceive people should acquire a lot from this web blog its rattling user genial. So much superb info on here abdgeedecdbf