Lab15 1
Lab15 1
h>
#include<stdlib.h>
#include<stdbool.h>
int main(){
int r, p;
int maxneed[5][5];
int allocation[5][5];
int available[5];
int need[5][5];
int finish[5];
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
//Available Matrix
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");
}
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
// 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
// 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);
}
}
}
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;
}
}
}
}