#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;
}
}
}
}