Implementation of resource allocation graph(RAG)
Here you will learn the program code of implementation of resource allocation graph (RAG) in operating system lab using c programming.
Implementation of resource allocation graph in c program
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <stdio.h> #include <stdbool.h> #define MAX_PROCESSES 10 #define MAX_RESOURCES 10 bool resource_allocation_graph[MAX_PROCESSES][MAX_RESOURCES]; int num_processes, num_resources; // Function for checking cycles using Depth First Search (DFS) bool is_cycle_util(int process, bool visited[], bool stack[]) {     if (!visited[process]) {         visited[process] = true;         stack[process] = true;         for (int i = 0; i < num_resources; ++i) {             if (resource_allocation_graph[process][i]) {                 if (!visited[i] && is_cycle_util(i, visited, stack)) {                     return true;                 } else if (stack[i]) {                     return true;                 }             }         }     }     stack[process] = false;     return false; } // Function for detecting cycle in the graph bool has_cycle() {     bool visited[MAX_PROCESSES] = {false};     bool stack[MAX_PROCESSES] = {false};     for (int i = 0; i < num_processes; ++i) {         if (is_cycle_util(i, visited, stack)) {             return true;         }     }     return false; } int main() {     // Enter processes and resources in number     printf("Enter the number of processes: ");     scanf("%d", &num_processes);     printf("Enter the number of resources: ");     scanf("%d", &num_resources);     // Input resource allocation graph     printf("Enter the resource allocation graph (1 if process i is allocated resource j, 0 otherwise):\n");     for (int i = 0; i < num_processes; ++i) {         for (int j = 0; j < num_resources; ++j) {             scanf("%d", &resource_allocation_graph[i][j]);         }     }     // Checking for cycle     if (has_cycle()) {         printf("Deadlock detected! Found cycle in the resource allocation graph.\n");     } else {         printf("No deadlock detected. Resource allocation graph is deadlock free.\n");     }     return 0; } | 
Output 1

Output 2

Read Also
