How to write a C Program to Banker Algorithm Example in C Programming Language ?
The Banker's algorithm, sometimes referred to as the avoidance 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 an "s-state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue.
The algorithm was developed in the design process for the THE operating system and originally described (in Dutch) in EWD108 When a new process enters a system, it must declare the maximum number of instances of each resource type that it may ever claim; clearly, that number 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.
Solution:
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- int n,m,i,j;
- int all[10][10],max [10][10],need[10][10];
- int avail[10],work[10],req[10];
- struct process
- {
- char name[10];
- int flag;
- }p[10];
- void input()
- {
- printf("enter the total number of process \n");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- printf("name:");
- scanf("%s",p[i].name);
- }
- printf(" enter the number of resouces \n");
- scanf("%d",&m);
- printf(" enter the allocation matrix \n");
- for(i=0;i<n;++i)
- {
- for(j=0;j<m;++j)
- scanf("%d",&all[i][j]);
- }
- printf(" enter max matrix \n");
- for(i=0;i<n;++i)
- {
- for(j=0;j<m;++j)
- scanf("%d",&max[i][j]);
- }
- printf(" need matrix \n");
- for(i=0;i<n;++i)
- {
- for(j=0;j<m;++j)
- {
- need[i][j]=max[i][j]-all[i][j];
- printf(" %d ",need[i][j]);
- printf("\n");
- }
- }
- printf("\n");
- printf("enter avaliable resources \n");
- for(i=0;i<m;i++)
- scanf("%d",&avail[i]);
- }
- void safeseq()
- {
- int sseq[10],ss=0,chk=0,chki=0,count=0;
- for(j=0;j<m;++j)
- work[j]=avail[j];
- for(i=0;i<n;++i)
- p[i].flag=0;
- while(count!=5)
- {
- for(i=0;i<n;++i)
- {
- chk=0;
- for(j=0;j<m;++j)
- {
- if(p[i].flag==0)
- {
- if(need[i][j]<=work[j]);
- chk++;
- }
- if(chk==m)
- {
- for(j=0;j<m;j++)
- {
- work[j]=work[j]+all[i][j];
- printf("%d",work[j]);
- p[i].flag=1;
- }
- sseq[ss]=1;
- ss++;
- count++;
- }
- }
- }
- }
- for(i=0;i<n;++i)
- {
- if(p[i].flag==1)
- chki++;
- }
- if(chki>=n)
- {
- printf("system is in safe state \n");
- for(i=0;i<n;++i)
- printf(" %d ",sseq[i]);
- }
- else
- printf("not safe \n");
- }
- void request()
- {
- int pro;
- printf(" enter the process number of request \n");
- scanf("%d",&pro);
- printf(" enetr the request array \n");
- for(i=0;i<m;++i);
- scanf("%d",&req[i]);
- for(j=0;j<m;j++)
- {
- if(req[j]<=need[pro][j])
- {
- if(req[j]<=avail[j])
- {
- avail[j]=avail[j]-req[j];
- all[pro][j]=all[pro][j]+req[j];
- need[pro][j]=need[pro][j]-req[j];
- printf(" avail: %d",avail[j]);
- }
- printf("\t need : %d",need[pro][j]);
- }
- else
- {
- printf("process has some kirik \n");
- exit(0);
- }
- }
- }
- void print()
- {
- printf("max matrix \n");
- for(i=0;i<n;++i)
- {
- for(j=0;j<m;++j)
- printf(" %d ",max[i][j]);
- printf("\n");
- }
- printf("allocation matrix \n");
- for(i=0;i<n;++i)
- {
- for(j=0;j<m;++j)
- printf(" %d ",all[i][j]);
- printf("\n");
- }
- printf("need matrix \n");
- for(i=0;i<n;++i)
- {
- for(j=0;j<m;++j)
- printf(" %d ",need[i][j]);
- printf("\n");
- }
- printf(" \n availiable ");
- for(i=0;i<m;++i)
- printf(" %d ",avail[i]);
- }
- int main()
- {
- int ch;
- do{
- printf("\n menu");
- printf(" 1.input ");
- printf("\n 2.safe seq ");
- printf(" \n 3. request ");
- printf("\n 4.print");
- printf("\n 5.exit");
- printf("\n enter choice ");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1: input();
- break;
- case 2: safeseq();
- break;
- case 3: request();
- break;
- case 4: print();
- break;
- case 5:break;
- }
- }while(ch!=5);
- return 0;
- }
Learn More :
Example
- C Program String - Alphabet Count Example
- C Program Array Example: Average
- C Program Array Example: Reverse
- C Program Switch - Case Example
- C Program if - else if - else Example
- C Program Friend & Operator: Point2D Example
- C Program Friend & Operator: Vector Example
- C Program Recursion Example
- C Program Structure Example-2
- C Program Structure Example
- C Program Pointer Example
- C Program Function Example
- C Program String Example
- C Program Character Example
- C Program sizeof & memcpy Example
- C Program Array Example
- C Program Side Length Example
- C Program Pipes()
- C Program 0-1000 to Ordinary
- C Program Roboturtle
- C Program to Destroys a Mailbox
- C Program to Destroys a semaphore
- C Code for Testing the pH Meter V1.0
- APA 102c test program
- How To Retarget the C Library printf function to the UART.
Algorithm
- C program generates a random number and uses an algorithm to generate 9 other numbers.
- C Program Fraction To Decimal
- C Program for distance vector algorithm to find suitable path for transmission
- C Program to find MST(Minimum Spanning Tree) using Prim's Algorithm
- C Program Dijkstra's Shortest Path Algorithm
- C Program to Implement Dijkstra's Algorithm