0% found this document useful (0 votes)
65 views4 pages

Lab15 1

This C program implements the banker's algorithm for deadlock avoidance in operating systems. It takes input for the number of processes and resources, the maximum resource requirements for each process, current allocation of resources to processes, and available resources. It initializes matrices to track need, allocation, and available resources. The main function calls functions to display the current state, execute a process if it is safe to do so, and check if all processes have finished. The execute function allocates resources to a process if its needs are less than or equal to available resources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views4 pages

Lab15 1

This C program implements the banker's algorithm for deadlock avoidance in operating systems. It takes input for the number of processes and resources, the maximum resource requirements for each process, current allocation of resources to processes, and available resources. It initializes matrices to track need, allocation, and available resources. The main function calls functions to display the current state, execute a process if it is safe to do so, and check if all processes have finished. The execute function allocates resources to a process if its needs are less than or equal to available resources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>
#include<stdlib.h>
#include<stdbool.h>

void display(int r, int p, int finish[5], int allocation[5][5], int available[5],


int need[5][5]);
void execute(int r, int p, int finish[5], int allocation[5][5], int available[5],
int need[5][5]);
bool fin(int finish[5], int p);

int main(){
int r, p;
int maxneed[5][5];
int allocation[5][5];
int available[5];
int need[5][5];
int finish[5];

printf("Enter the no of processes : ");


scanf("%d", &p);

printf("\n\nEnter the no of resources : ");


scanf("%d", &r);

// max matrix values

printf("\n\nEnter the Max Matrix for each process : ");


for(int i=0;i<p; i++){
printf("\nFor process %d : ", i + 1);
for(int j=0; j<r; j++){
scanf("%d", &maxneed[i][j]);
}
}

// max allocation matrix

int temp;
printf("\n\nEnter the allocation for each process : ");
for(int i=0;i<p; i++){
printf("\nFor process %d : ",i + 1);
for(int j=0; j<r; j++){
scanf("%d", &temp);
if(temp<=maxneed[i][j]){
allocation[i][j] = temp;
}
else {
printf("Error! Allocated resources cannot exceed previously
defined maximum need");
}
}
}

// max instances

printf("\n\nEnter the Max Available Resources : ");


for(int i=0;i<r; i++){
scanf("%d", &available[i]);
}
//Need Matrix

for(int i=0;i<p; i++){


for(int j=0; j<r; j++){
need[i][j]= maxneed[i][j] - allocation[i][j];
}
}

//Available Matrix

for(int i=0;i<r; i++){


for(int j=0; j<p; j++){
available[i] -= allocation[j][i];
}
}

printf("\nProcesses: %d",p);
printf("\nResources: %d",r);
printf("\n\nMax Matrix\n");
for(int i=0;i<p; i++){
for(int j=0; j<r; j++){
printf("%d\t", maxneed[i][j]);
}
printf("\n");
}

for(int i=0; i<p; i++){


finish[i] = 0;
}

display(r, p, finish, allocation, available, need);


printf("\nSystem is in safe state\n");

while(!fin(finish, p))
execute(r, p, finish, allocation, available, need);

if(fin(finish, p))
{
printf("System completed execution\nSystem is in safe state\n");
}
}
// displaying

void display(int r, int p, int finish[5], int allocation[5][5], int available[5],


int need[5][5]){

// need matrix

printf("\n\nNeed Matrix\n");
for(int i=0;i<p; i++){
for(int j=0; j<r; j++){
printf("%d\t", need[i][j]);
}
printf("\n");
}

// available resources
printf("\n\n*****Available Vector*****\n");
for(int i=0;i<r; i++){
printf("%d\t", available[i]);
}

// allocation matrix

printf("\n\n*****Allocation Matrix (Current Status)*****\n");


for(int i=0;i<p; i++){
for(int j=0; j<r; j++){
printf("%d\t", allocation[i][j]);
}
printf("\n");
}

// state processes

printf("\n\nState of Processes\n");
for(int i=0;i<p; i++){
if(finish[i]==1){
printf("Process %d: Finished\n", i+1);
}
else if(finish[i]==0){
printf("Process %d: Not Finished\n", i+1);
}
}
}

bool fin(int finish[5], int p){


int sum=0;
for(int i=0; i<p; i++){
sum += finish[i];
}
if(sum==p){
return true;
}
else{
return false;
}
}

void execute(int r, int p, int finish[5], int allocation[5][5], int available[5],


int need[5][5]){
int check=0;
for(int i=0;i<p; i++){
if(finish[i]==0){
for(int j=0; j<r; j++){
if(need[i][j]<=available[j]){
check++;
}
}

if(check==r){
printf("\nExecuting Process %d\n", i+1);
for(int j=0; j<r; j++){
available[j] += allocation[i][j];
need[i][j] = 0;
allocation[i][j]=0;
}
finish[i]=1;
check=0;
display(r, p, finish, allocation, available, need);
return;
}
}
}
}

You might also like