Lab 8 Os
Lab 8 Os
22104072
LAB 8
#include <stdio.h>
#include <stdbool.h>
#define NUM_PROCESSES
3
#define NUM_RESOURCES 4
int available[NUM_RESOURCES];
int allocation[NUM_PROCESSES][NUM_RESOURCES];
int max_demand[NUM_PROCESSES][NUM_RESOURCES];
int need[NUM_PROCESSES][NUM_RESOURCES];
bool isSafe() {
int work[NUM_RESOURCES];
if (canExecute) {
for (int j = 0; j < NUM_RESOURCES; ++j)
{ work[j] += allocation[i][j];
}
finish[i] = true;
found = true;
count++;
}
}
}
if (!found) {
break;
}
}
if (count == NUM_PROCESSES)
{ printf("Safe sequence: ");
for (int i = 0; i < NUM_PROCESSES; ++i)
{ printf("%d ", i);
}
printf("\n");
return true;
}
else {
int main() {
// Initialize available resources
available[0] = 1; available[1] = 1; available[2] = 1; available[3] = 1;
// Case 1: Safe Sequence
allocation[0][0] = 0; allocation[0][1] = 1; allocation[0][2] = 0; allocation[0][3] = 0;
printf("Case 1:\n");
isSafe();
// Case 2: No Safe Sequence
allocation[0][0] = 0; allocation[0][1] = 1; allocation[0][2] = 0; allocation[0][3] = 0;
allocation[1][0] = 2; allocation[1][1] = 0; allocation[1][2] = 2; allocation[1][3] = 1;