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A9IGZhbHNlOwogICAgICAgIC8vIEZpbmQgYSBwcm9jZXNzIGkgdGhhdCBpcyBub3QgZmluaXNoZWQgQU5EIChuZWVkICZsdDs9IHdvcmspCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgJmx0OyBQOyBpKyspIHsKICAgICAgICAgICAgaWYgKGZpbmlzaFtpXSA9PSBmYWxzZSkgewogICAgICAgICAgICAgICAgaW50IGo7CiAgICAgICAgICAgICAgICBmb3IgKGogPSAwOyBqICZsdDsgUjsgaisrKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKG5lZWRbaV1bal0gJmd0OyB3b3JrW2pdKSB7IC8vIFByb2Nlc3MgaSdzIG5lZWQgaXMgbm90IG1ldAogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIGlmIChqID09IFIpIHsgLy8gUHJvY2VzcyBpJ3MgbmVlZCBpcyBtZXQgKGogd2VudCB0aHJvdWdoIGFsbCByZXNvdXJjZXMpCiAgICAgICAgICAgICAgICAgICAgLy8gR3JhbnQgcmVzb3VyY2VzIGFuZCBzaW11bGF0ZSBwcm9jZXNzIGZpbmlzaAogICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGsgPSAwOyBrICZsdDsgUjsgaysrKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHdvcmtba10gKz0gYWxsb2NhdGlvbltpXVtrXTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgc2FmZV9zZXF1ZW5jZVtjb3VudCsrXSA9IGk7CiAgICAgICAgICAgICAgICAgICAgZmluaXNoW2ldID0gdHJ1ZTsKICAgICAgICAgICAgICAgICAgICBmb3VuZCA9IHRydWU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgCiAgICAgICAgaWYgKGZvdW5kID09IGZhbHNlKSB7IC8vIE5vIHByb2Nlc3MgY291bGQgcnVuIGluIHRoaXMgaXRlcmF0aW9uCiAgICAgICAgICAgIHJldHVybiBmYWxzZTsgLy8gU3lzdGVtIGlzIGluIGFuIFVOU0FGRSBzdGF0ZQogICAgICAgIH0KICAgIH0KCiAgICAvLyBTeXN0ZW0gaXMgaW4gYSBTQUZFIHN0YXRlCiAgICBwcmludGYoJnF1b3Q7U3lzdGVtIGlzIGluIGEgU0FGRSBzdGF0ZS4gU2FmZSBzZXF1ZW5jZTogJnF1b3Q7KTsKICAgIGZvciAoaW50IGkgPSAwOyBpICZsdDsgUDsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCZxdW90O1AlZCAmcXVvdDssIHNhZmVfc2VxdWVuY2VbaV0pOwogICAgfQogICAgcHJpbnRmKCZxdW90O1xuJnF1b3Q7KTsKICAgIHJldHVybiB0cnVlOwp9CgovLyBFeGFtcGxlIHVzYWdlIHdvdWxkIGRlZmluZSBQIChwcm9jZXNzZXMpLCBSIChyZXNvdXJjZXMpLCBhbmQgdGhlIG1hdHJpY2VzLgovLyAK
// Simplified C Program to Simulate Safety Check (Deadlock Avoidance)
#include <stdio.h>
#include <stdbool.h>
// Function to simulate the Safety Algorithm
bool isSafe(int available[], int max[][3], int allocation[][3], int P, int R) {
int work[R]; // A copy of available resources
for (int i = 0; i < R; i++) {
work[i] = available[i];
}
bool finish[P]; // Tracks if a process can finish
for (int i = 0; i < P; i++) {
finish[i] = false;
}
int need[P][R];
// Calculate Need matrix
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
int safe_sequence[P];
int count = 0;
// The main safety check loop
while (count < P) {
bool found = false;
// Find a process i that is not finished AND (need <= work)
for (int i = 0; i < P; i++) {
if (finish[i] == false) {
int j;
for (j = 0; j < R; j++) {
if (need[i][j] > work[j]) { // Process i's need is not met
break;
}
}
if (j == R) { // Process i's need is met (j went through all resources)
// Grant resources and simulate process finish
for (int k = 0; k < R; k++) {
work[k] += allocation[i][k];
}
safe_sequence[count++] = i;
finish[i] = true;
found = true;
}
}
}
if (found == false) { // No process could run in this iteration
return false; // System is in an UNSAFE state
}
}
// System is in a SAFE state
printf("System is in a SAFE state. Safe sequence: ");
for (int i = 0; i < P; i++) {
printf("P%d ", safe_sequence[i]);
}
printf("\n");
return true;
}
// Example usage would define P (processes), R (resources), and the matrices.
//