#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
// Function to check if all processes can finish
bool canFinish(int process, int finish[], int need[MAX_PROCESSES][MAX_RESOURCES], int work[], int m) {
for (int i = 0; i < m; i++) {
if (need[process][i] > work[i])
return false;
}
return true;
}
// Deadlock Detection Function
bool detectDeadlock(int processes[], int available[], int max[MAX_PROCESSES][MAX_RESOURCES], int allocation[MAX_PROCESSES][MAX_RESOURCES], int need[MAX_PROCESSES][MAX_RESOURCES], int n, int m) {
int work[MAX_RESOURCES];
int finish[MAX_PROCESSES] = {0};
// Initialize work as available
for (int i = 0; i < m; i++) {
work[i] = available[i];
}
bool deadlock = false;
for (int i = 0; i < n; i++) {
if (!finish[i] && canFinish(i, finish, need, work, m)) {
for (int j = 0; j < m; j++) {
work[j] += allocation[i][j];
}
finish[i] = 1;
i = -1; // Restart the process to check for others
}
}
for (int i = 0; i < n; i++) {
if (!finish[i]) {
deadlock = true;
printf("Process %d is in deadlock\n", i
); }
}
return deadlock;
}
int main() {
int n = MAX_PROCESSES; // Number of processes
int m = MAX_RESOURCES; // Number of resources
int processes[MAX_PROCESSES] = {0, 1, 2, 3, 4};
// Available instances of resources
int available[MAX_RESOURCES] = {3, 3, 2};
// Maximum R that can be allocated to processes
int max[MAX_PROCESSES][MAX_RESOURCES] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};
// Resources allocated to processes
int allocation[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};
// Need of each process
int need[MAX_PROCESSES][MAX_RESOURCES];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
if (detectDeadlock(processes, available, max, allocation, need, n, m)) {
printf("System is in Deadlock.\n"); } else {
printf("No Deadlock Detected.\n"); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlv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FmaW5pc2hbaV0gJiYgY2FuRmluaXNoKGksIGZpbmlzaCwgbmVlZCwgd29yaywgbSkpIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspIHsKICAgICAgICAgICAgICAgIHdvcmtbal0gKz0gYWxsb2NhdGlvbltpXVtqXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBmaW5pc2hbaV0gPSAxOwogICAgICAgICAgICBpID0gLTE7IC8vIFJlc3RhcnQgdGhlIHByb2Nlc3MgdG8gY2hlY2sgZm9yIG90aGVycwogICAgICAgIH0KICAgIH0KCiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGlmICghZmluaXNoW2ldKSB7CiAgICAgICAgICAgIGRlYWRsb2NrID0gdHJ1ZTsKICAgICAgICAgICAgcHJpbnRmKCJQcm9jZXNzICVkIGlzIGluIGRlYWRsb2NrXG4iLCBpKTsKICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIGRlYWRsb2NrOwp9CgppbnQgbWFpbigpIHsKICAgIGludCBuID0gTUFYX1BST0NFU1NFUzsgIC8vIE51bWJlciBvZiBwcm9jZXNzZXMKICAgIGludCBtID0gTUFYX1JFU09VUkNFUzsgIC8vIE51bWJlciBvZiByZXNvdXJjZXMKCiAgICBpbnQgcHJvY2Vzc2VzW01BWF9QUk9DRVNTRVNdID0gezAsIDEsIDIsIDMsIDR9OwoKICAgIC8vIEF2YWlsYWJsZSBpbnN0YW5jZXMgb2YgcmVzb3VyY2VzCiAgICBpbnQgYXZhaWxhYmxlW01BWF9SRVNPVVJDRVNdID0gezMsIDMsIDJ9OwoKICAgIC8vIE1heGltdW0gUiB0aGF0IGNhbiBiZSBhbGxvY2F0ZWQgdG8gcHJvY2Vzc2VzCiAgICBpbnQgbWF4W01BWF9QUk9DRVNTRVNdW01BWF9SRVNPVVJDRVNdID0gewogICAgICAgIHs3LCA1LCAzfSwKICAgICAgICB7MywgMiwgMn0sCiAgICAgICAgezksIDAsIDJ9LAogICAgICAgIHsyLCAyLCAyfSwKICAgICAgICB7NCwgMywgM30KICAgIH07CgogICAgLy8gUmVzb3VyY2VzIGFsbG9jYXRlZCB0byBwcm9jZXNzZXMKICAgIGludCBhbGxvY2F0aW9uW01BWF9QUk9DRVNTRVNdW01BWF9SRVNPVVJDRVNdID0gewogICAgICAgIHswLCAxLCAwfSwKICAgICAgICB7MiwgMCwgMH0sCiAgICAgICAgezMsIDAsIDJ9LAogICAgICAgIHsyLCAxLCAxfSwKICAgICAgICB7MCwgMCwgMn0KICAgIH07CgogICAgLy8gTmVlZCBvZiBlYWNoIHByb2Nlc3MKICAgIGludCBuZWVkW01BWF9QUk9DRVNTRVNdW01BWF9SRVNPVVJDRVNdOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG07IGorKykgewogICAgICAgICAgICBuZWVkW2ldW2pdID0gbWF4W2ldW2pdIC0gYWxsb2NhdGlvbltpXVtqXTsKICAgICAgICB9CiAgICB9CgogICAgaWYgKGRldGVjdERlYWRsb2NrKHByb2Nlc3NlcywgYXZhaWxhYmxlLCBtYXgsIGFsbG9jYXRpb24sIG5lZWQsIG4sIG0pKSB7CiAgICAgICAgcHJpbnRmKCJTeXN0ZW0gaXMgaW4gRGVhZGxvY2suXG4iKTsKICAgIH0gZWxzZSB7CiAgICAgICAgcHJpbnRmKCJObyBEZWFkbG9jayBEZXRlY3RlZC5cbiIpOwogICAgfQoKICAgIHJldHVybiAwOwp9Cg==