Program for Bankers algorithm for deadlock avoidance in c | Wave the world

Program for Bankers algorithm for deadlock avoidance in c

/*
    Bankers Deadlock Avoidance Algorithm
    Created By: Pirate
*/

#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Baner's Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t   ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
printf("\n");
while(flag)
{
flag=0;
  for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
    c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
      }
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}
        }
}
}
  }
  }
  for(i=0;i<n;i++)
  {
if(finish[i]==1)
{
c1++;
}
else
{
printf("P%d->",i);
}
}
if(c1==n)
{
printf("\n The system is in safe state");
}
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}




Output














12 comments:

  1. Banker's Algorithm is divided into two sub parts viz. Safety Test Algorithm and Resource Request Handling System. Banker's Algorithm in C Programming is one of the best algorithms for solving deadlock avoidance problems.

    ReplyDelete
  2. Does not work properly

    ReplyDelete
    Replies
    1. what problem are you facing?

      Delete
    2. this code isn't running but it's just compiling

      Delete
    3. what error are you getting when you try to run the code?

      Delete
  3. +1 for the code

    ReplyDelete
    Replies
    1. i think safe sequence should be printed if the the system is in safe state

      Delete
    2. for that you can modify the code.

      Delete
  4. Safe sequence is wrong completely

    .

    ReplyDelete
  5. #All thanks to Mr Anderson for helping with my profits and making my fifth withdrawal possible. I'm here to share an amazing life changing opportunity with you. its called Bitcoin / Forex trading options. it is a highly lucrative business which can earn you as much as $2,570 in a week from an initial investment of just $200. I am living proof of this great business opportunity. If anyone is interested in trading on bitcoin or any cryptocurrency and want a successful trade without losing notify Mr Anderson now.Whatsapp: (+447883246472 )
    Email: tdameritrade077@gmail.com

    ReplyDelete
  6. SCAMS COME IN MANY FORMS, BUT ALL ARE DESIGNED TO GET A HOLD OF YOUR HARD-EARNED MONEY.
    The first mistake I made was thinking I could multiply my income by investing in cryptocurrency, I fell for a popular investment scam and was swindled of $60,000 in bitcoins. After a few weeks, I came across a pop-up while browsing the internet about a recovery team called Adrian Lamo Wizard that could help me recover my bitcoins in a matter of hours, I was doubtful about it at first but I finally contacted them and in a space of 48 hours, Adrian Lamo Wizard was able to recover all of my money lost in bitcoins. They are the best bitcoins recovery team out there and if you face similar issues, you should contact them immediately.
    Contact information: adrianlamowizard22@gmail.com WhatsApp: +31617016197 Telegram: +31617016197

    ReplyDelete

 

Pro

About