C program of Banker’s Algorithm to avoid Deadlock

Mr Coder August 29, 2012 9

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("-------------Made by C codechamp--------------------\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 C program 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!

 

9 Comments »

  1. how to buy cheap Hermes Birkin 35 Bags September 1, 2012 at 4:15 am - Reply

    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

  2. online tv station software September 4, 2012 at 8:56 pm - Reply

    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. link building September 30, 2012 at 12:55 pm - Reply

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

  4. longchamp pas cher October 13, 2012 at 1:29 pm - Reply

    Only a smiling visitor here to share the love (:, btw outstanding design. “Make the most of your regrets… . To regret deeply is to live afresh.” by Henry David Thoreau.

  5. baju bayi November 22, 2012 at 4:55 am - Reply

    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 .

  6. Perlengkapan bayi December 10, 2012 at 9:10 pm - Reply

    Thanks like your C program of Banker's Algorithm to avoid Deadlock | C codechamp

  7. baju bayi December 14, 2012 at 4:50 am - Reply

    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

  8. tamanna January 31, 2013 at 7:37 pm - Reply

    plz show me the any demo to show the output…i can’t understand…..plz plz plz…

  9. karthika June 27, 2013 at 6:07 pm - Reply

    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….

Leave A Response »