Q.1. Consider the following Snapshot of a System.
Allocation Max Available A B C D A B C D A B C D P0 0 0 1 2 0 0 1 2 1 5 2 0 P1 1 0 0 0 1 7 5 0 P2 1 3 5 4 2 3 5 6 P3 0 6 3 2 0 6 5 2 P4 0 0 1 4 0 6 5 6
Use Banker’s algorithm.
a) What is the content of matrix need ?
b) Is the system in a safe state ?
Implement Banker’s algorithm using a C program.
Solution :
/*-------------Banker's algorithm---------------------------------*/
#include
#include
#include
#define MAX_PROC 10
#define MAX_RESO 10
#define TRUE 1
#define FALSE 0 void main()
{
int n; //Let n be the number of processes in the system int m; //Let m be the number of resource types int available[MAX_RESO]; //Number of available resources //of each type int max[MAX_PROC][MAX_RESO];//Maximum demand of each process int allocation[MAX_PROC][MAX_RESO]; //No. of resources of each type //currently allocated to each process int need[MAX_PROC][MAX_RESO]; //Remaining resource need of each process
void assign_available(int available[], int m); void assign_max(int max[][MAX_RESO], int n, int m); void assign_alloc(int allocation[][MAX_RESO], int n, int m); void cal_need(int need[][MAX_RESO], int max[][MAX_RESO], int alloc[][MAX_RESO], int n, int m); int safety(int available[], int max[][MAX_RESO], int alloc[][MAX_RESO], int need[][MAX_RESO], int n, int m); clrscr(); printf("\nEnter no. of processes : "); scanf("%d", &n); if(n > MAX_PROC) { printf("\nNumber of processes cannot be greater than %d",MAX_PROC); exit(0); }
printf("Enter no. of resource types: "); scanf("%d", &m); if(m > MAX_RESO) { printf("\nNumber of resources cannot be greater than %d",MAX_RESO); exit(0); }