Monday , December 18 2017
Home / C / C program for bankers algorithm

C program for bankers algorithm

This is a C program for Banker’s algorithm for finding out the safe sequence. Bankers algorithm is used to schedule processes according to the resources they need. It is very helpful in Deadlock Handling. Bankers algorithm produce a safe sequence as a output if all the resources can be executed and return error if no safe sequence of the processes available.
 

#include<stdio.h>
#include<conio.h>
void main()
{
int k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz, count=0;
clrscr();
printf("\n Enter the number of resources : ");
scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for(i=0;i<rz;i++)
{  avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n     ");

for(i=0;i<rz;i++)
printf(" %c",(i+97));
printf("\n");
for(i=0;i <pno;i++)
{           P[i]=i;
printf("P[%d]  ",P[i]);
for(j=0;j<rz;j++)
{
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}

printf("\nEnter the MAX matrix \n     ");
for(i=0;i<rz;i++)
{          printf(" %c",(i+97));
avail[i]=ins[i]-avail[i];
}
printf("\n");
for(i=0;i <pno;i++)
{
printf("P[%d]  ",i);
 for(j=0;j<rz;j++)
 scanf("%d", &MAX[i][j]);
}

printf("\n");
A: d=-1;
for(i=0;i <pno;i++)
{ count=0; t=P[i];
 for(j=0;j<rz;j++)
 {
 need[t][j] = MAX[t][j]-allocated[t][j];
 if(need[t][j]<=avail[j])
 count++;
 }
if(count==rz)
{
output[k++]=P[i];
 for(j=0;j<rz;j++)
avail[j]+=allocated[t][j];
}
else
 P[++d]=P[i];
}

if(d!=-1)
{ pno=d+1;
goto A;
}
printf("\t <");
for(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
getch();
}

OUTPUT:

 
Tags: c program for bankers algorithm, c program to find safe state using bankers algorithm, finding out the safe state in c.
 
 
 
Also See:   C program for Sum of two number using While LOOP

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

8 comments

  1. Great, thank you for this !

  2. Is anyone know the programcode for given question? if yes then please mail it to me before 2 pm of 14 nov.it’s very urgent for me.
    E mail-aeragonexcellence@gmail.com

    Q1) A computer has three commonly used resources designated A, B and C. Up to three processes designated X, Y and Z run on the computer and each makes periodic use of two of the three resources.

    Process X acquires C, then B, uses both and then releases both.

    Process Y acquires A, then C, uses both and then releases both.

    Process Z acquires B, then A, uses both and then releases both.

  3. not working after the max matrix

  4. can u please provide explanation for the each line of the code.
    its a bit confusing.

Leave a Reply

Your email address will not be published. Required fields are marked *