Ad

Tuesday, September 15, 2015

Bankers algorithm program

Banker's Algorithm
Goal:-  C program For Banker's Algorithm.
Method:-  Simple Rules Of Banker Algorithm.
Explanation:-  Banker's Algorithm is a deadlock avoidance algorithm that checks for safe or unsafe state of a System after allocating resources to a process.
When a new process enters into system ,it must declare maximum no. of instances of each resource  that it may need.After requesting operating system run banker's algorithm to check whether after allocating requested resources,system goes into deadlock state or not. If yes then it will deny the request of resources made by process else it allocate resources to that process.
No. of requested resources (instances of each resource) may not exceed no. of available resources in operating system and when a process completes it must release all the requested and already allocated resources.
For implementing Banker's algorithm we should have pre-knowledge about three things:-
  • How many instance of each resource a process could request. (Max)
  • How many instance of each resource is already allocated to that process(Allocated)
  • How many instance of each resource is already available(Available).
We can calculate need of each process from above information:-
               Need=Max - Allocated.
If need<=available then request will be accepted otherwise it is denied and it will check for next process in waiting queue.(See Life Cycle Of a Process)
Safe or Unsafe State:- A system is in Safe state if its all process finish its execution or if any process is unable to aquire its all requested resources then system will be in Unsafe state. 






Example:-  To understand banker's algorithm,we consider a system with four processes P1 through P4 and three resource type A,B and C.Resource type has 10 instances,B has 5 instances and C has total 7 instances.
Current Scenario:-

Table-1
Need[P2] < Available..So P2 we allocate resources to P2. After Completion of P2 ,Scenario...

Table-2
Now Need[P4] < Available so P4 will complete its execution .After that--
Table-3
So P1 will execute and after that P3 will take resources.
  Safe Sequence is :-   P2 , P4, P1,P3 (we can obtain one more sequence if we allocate resources to P3 before P1 .)
Program:-
#include<stdio.h>
#include<conio.h>
   int process,resource,i,j;
  int avail[10],max[10][10],allot[10][10],need[10][10],completed[10];
void get_details()
   {
   int instanc;
   //count,k variables are taken for counting purpose
   printf("\n\t Enter No. of Process:-\n");
  printf("\t\t");
 scanf("%d",&process);                           //Entering No. of Processes
 printf("\n\tEnter No. of Resources:-\n");
   printf("\t\t");
   scanf("%d",&resource);                       //No. of Resources

   printf("\n\tEnter No. of Available Instances\n");
   for(i=0;i<resource;i++)
    {
     printf("\t\t");
     scanf("%d",&instanc);
     avail[i]=instanc;                        // Storing Available instances
    }

 printf("\n\tEnter Maximum No. of instances of resources that a Process need:\n");
 for(i=0;i<process;i++)
     {
      printf("\n\t For P[%d]",i);
for(j=0;j<resource;j++)
{
   printf("\t");
    scanf("%d",&instanc);
   max[i][j]=instanc;
}
     }
    printf("\n\t Enter no. of instances already allocated to process of a resource:\n");
     for(i=0;i<process;i++)
     {
printf("\n\t For P[%d]\t",i);
for(j=0;j<resource;j++)
{
   printf("\t\t");
   scanf("%d",&instanc);
   allot[i][j]=instanc;
   need[i][j]=max[i][j]-allot[i][j];      //calculating Need of each process
}
   }


   }
int is_safe()
   {
   int count1=0,count2=0,k=0;
   int avail2[10];
   for(i=0;i<process;i++)
      { completed[i]=0;
       } //Setting Flag for uncompleted Process
       for(i=0;i<resource;i++)
      { avail2[i]=avail[i];
       }
    printf("\n\t Safe Sequence is:- \t");
    while(count1!=process)
    {
    count2=count1;
    for(i=0;i<process;i++)
     {
       for(j=0;j<resource;j++)
       {
   if(need[i][j]<=avail2[j])
     {
k++;
     }//if
}//for
if(k==resource && completed[i]==0 )
{
 printf("P[%d]\t",i);
  completed[i]=1;
  for(j=0;j<resource;j++)
    {
      avail2[j]=avail2[j]+allot[i][j];
    }//for
    count1++;
}//if
k=0;
       }//for
if(count1==count2)
{
printf("\t\t Stop ..After this.....Deadlock \n");
return 0;
//break;
       }//if
     }//while
 return 1;
}
//if return value=1 then less than or equal
int lre(int first[],int second[])
    {
    int i=0;
    for(i=0;i<resource;i++)
{
if(first[i]>second[i]){return 0;}
}
    return 1;
    }
void modify(int first[],int second[],char op)
   {
   int i=0;
   for(i=0;i<resource;i++)
      {
      if(op=='+')
first[i]=first[i]+second[i];
      else
first[i]=first[i]-second[i];
      }
   }
void print(int a[10][10])
   {
 for(i=0;i<process;i++)
     {
     for(j=0;j<resource;j++)
 {
 printf(" %d ",a[i][j]);
 }
      printf("\n");
      }

   }
void bankers()
  {
   int req[10],i=0,pid;
   printf("\nenter requesting process number:");
   scanf("%d",&pid);
   printf("\nenter request vector:");
   for(i=0;i<resource;i++)
      {
      scanf("%d",&req[i]);
      }
    if(lre(req,need[pid]))
       {
       if(lre(req,avail))
 {
 modify(avail,req,'-');
 modify(allot[pid],req,'+');
 modify(need[pid],req,'-');
 printf("\nallocation:\n");
 print(allot);
 printf("\nneed:\n");
 print(need);
 if(is_safe())
   {
   printf("Requested Resources are allocated for P[%d]",pid);
   }
 else
   {
 modify(avail,req,'+');
 modify(allot[pid],req,'-');
 modify(need[pid],req,'+');
 printf("DEADLOCK COMES->Requested Resources are NOT allocated for P[%d]",pid);
   }
 }
       else
{
printf("\nP[%d] should wait -> no available resources",pid);
}
       }
    else
{
printf("\nsorry!!!error!!!process has requested more than its max claim");
}
  }

void main()
 {
   clrscr();
   get_details();
   if(is_safe())
   {
   bankers();
   }
   getch();
   }




No comments:

Post a Comment