From 974aba4b13824420a5b8e26df491b8a3c473ea34 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 19:51:46 +0530 Subject: [PATCH 01/12] Banker's Algorithm.c --- Banker's Algorithm.c | 100 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Banker's Algorithm.c diff --git a/Banker's Algorithm.c b/Banker's Algorithm.c new file mode 100644 index 00000000..aed7da6c --- /dev/null +++ b/Banker's Algorithm.c @@ -0,0 +1,100 @@ +#include +#include + +#define MAX_PROCESSES 10 +#define MAX_RESOURCES 10 + +// Function to check if the system is in a safe state +bool isSafe(int processes, int resources, int max[MAX_PROCESSES][MAX_RESOURCES], + int allocation[MAX_PROCESSES][MAX_RESOURCES], int available[MAX_RESOURCES]) { + + int work[MAX_RESOURCES]; + bool finish[MAX_PROCESSES] = {false}; + int safeSequence[MAX_PROCESSES]; + int count = 0; + + // Initialize work to available resources + for (int i = 0; i < resources; i++) { + work[i] = available[i]; + } + + // Find a safe sequence + while (count < processes) { + bool found = false; + for (int p = 0; p < processes; p++) { + if (!finish[p]) { + bool canAllocate = true; + for (int r = 0; r < resources; r++) { + if (max[p][r] - allocation[p][r] > work[r]) { + canAllocate = false; + break; + } + } + + // If a process can be allocated + if (canAllocate) { + for (int r = 0; r < resources; r++) { + work[r] += allocation[p][r]; + } + safeSequence[count++] = p; + finish[p] = true; + found = true; + } + } + } + + // If no process can be allocated, then system is not in a safe state + if (!found) { + printf("System is not in a safe state.\n"); + return false; + } + } + + // Print safe sequence + printf("System is in a safe state.\nSafe sequence: "); + for (int i = 0; i < processes; i++) { + printf("%d ", safeSequence[i]); + } + printf("\n"); + return true; +} + +int main() { + int processes, resources; + int max[MAX_PROCESSES][MAX_RESOURCES]; + int allocation[MAX_PROCESSES][MAX_RESOURCES]; + int available[MAX_RESOURCES]; + + // Input the number of processes and resources + printf("Enter number of processes: "); + scanf("%d", &processes); + printf("Enter number of resources: "); + scanf("%d", &resources); + + // Input the Max matrix + printf("Enter the Max matrix:\n"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + scanf("%d", &max[i][j]); + } + } + + // Input the Allocation matrix + printf("Enter the Allocation matrix:\n"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + scanf("%d", &allocation[i][j]); + } + } + + // Input the Available resources + printf("Enter the Available resources:\n"); + for (int i = 0; i < resources; i++) { + scanf("%d", &available[i]); + } + + // Check if the system is in a safe state + isSafe(processes, resources, max, allocation, available); + + return 0; +} From 7f437b63056edd40b108515cb8218ce896327e03 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 19:54:23 +0530 Subject: [PATCH 02/12] Rename Banker's Algorithm.c to Scheduling Algorithm/Banker's Algorithm.c --- Banker's Algorithm.c => Scheduling Algorithm/Banker's Algorithm.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Banker's Algorithm.c => Scheduling Algorithm/Banker's Algorithm.c (100%) diff --git a/Banker's Algorithm.c b/Scheduling Algorithm/Banker's Algorithm.c similarity index 100% rename from Banker's Algorithm.c rename to Scheduling Algorithm/Banker's Algorithm.c From 5c99d98624dde6a16309c20ff43fa334e23c39ad Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:01:46 +0530 Subject: [PATCH 03/12] README.md --- Scheduling Algorithm/README.md | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Scheduling Algorithm/README.md diff --git a/Scheduling Algorithm/README.md b/Scheduling Algorithm/README.md new file mode 100644 index 00000000..cfce7ea9 --- /dev/null +++ b/Scheduling Algorithm/README.md @@ -0,0 +1,65 @@ +# Banker's Algorithm in C + +This project implements the **Banker's Algorithm** in C, a classic deadlock avoidance algorithm used in operating systems. The algorithm checks if resource allocation requests can be safely granted without causing deadlock by ensuring that the system remains in a "safe state" after each allocation. + +## Table of Contents +- [Introduction](#introduction) +- [Algorithm Overview](#algorithm-overview) +- [Features](#features) +- [Example](#example) +- [Limitations](#limitations) + +## Introduction + +The Banker's Algorithm, designed by Edsger Dijkstra, is a deadlock avoidance algorithm that allocates resources to processes only if the system can remain in a safe state. A "safe state" is one where a sequence of processes can complete without running into resource contention issues that would lead to deadlock. + +## Algorithm Overview + +The Banker's Algorithm operates based on three main matrices: +- **Max Matrix**: Indicates the maximum resources each process may require. +- **Allocation Matrix**: Shows the resources currently allocated to each process. +- **Available Vector**: Tracks the currently available resources in the system. + +When a process requests resources, the algorithm calculates if fulfilling the request will keep the system in a safe state. If so, the resources are allocated; otherwise, the request is denied. + +## Features + +- Deadlock avoidance for resource allocation. +- Identification of a safe sequence for process execution. +- Simple implementation using arrays and loops, compatible with standard C libraries. + +## Example + +### Example 1 + +#### Sample Input + +Enter number of processes: 3 +Enter number of resources: 3 + +Enter the Max matrix: +7 5 3 +3 2 2 +9 0 2 + +Enter the Allocation matrix: +0 1 0 +2 0 0 +3 0 2 + +Enter the Available resources: +3 3 2 + +#### Sample Input + +System is in a safe state. +Safe sequence: 1 0 2 + +## Limitations + +- **Fixed Resource and Process Count**: This implementation sets maximum limits for the number of processes and resources. Modifying these limits requires changing the code and recompiling. +- **Static Resource Allocation**: The algorithm only supports a static allocation based on the initial request. Dynamic resource changes or multiple resource requests are not handled. +- **Non-Preemptive**: Once allocated, resources cannot be preempted or re-assigned from one process to another, limiting flexibility. +- **Single Resource Type Per Process**: The current implementation assumes that each process only requests one type of resource at a time. For complex requests, code adjustments are needed. +- **Sequential Execution**: The algorithm performs sequential checks to find a safe sequence, which can be inefficient for systems with a large number of processes and resources. + From 628ab05cae344d21714a6c010f3edc89e3d1245b Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:12:27 +0530 Subject: [PATCH 04/12] Delete Scheduling Algorithm directory --- Scheduling Algorithm/Banker's Algorithm.c | 100 ---------------------- Scheduling Algorithm/README.md | 65 -------------- 2 files changed, 165 deletions(-) delete mode 100644 Scheduling Algorithm/Banker's Algorithm.c delete mode 100644 Scheduling Algorithm/README.md diff --git a/Scheduling Algorithm/Banker's Algorithm.c b/Scheduling Algorithm/Banker's Algorithm.c deleted file mode 100644 index aed7da6c..00000000 --- a/Scheduling Algorithm/Banker's Algorithm.c +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include - -#define MAX_PROCESSES 10 -#define MAX_RESOURCES 10 - -// Function to check if the system is in a safe state -bool isSafe(int processes, int resources, int max[MAX_PROCESSES][MAX_RESOURCES], - int allocation[MAX_PROCESSES][MAX_RESOURCES], int available[MAX_RESOURCES]) { - - int work[MAX_RESOURCES]; - bool finish[MAX_PROCESSES] = {false}; - int safeSequence[MAX_PROCESSES]; - int count = 0; - - // Initialize work to available resources - for (int i = 0; i < resources; i++) { - work[i] = available[i]; - } - - // Find a safe sequence - while (count < processes) { - bool found = false; - for (int p = 0; p < processes; p++) { - if (!finish[p]) { - bool canAllocate = true; - for (int r = 0; r < resources; r++) { - if (max[p][r] - allocation[p][r] > work[r]) { - canAllocate = false; - break; - } - } - - // If a process can be allocated - if (canAllocate) { - for (int r = 0; r < resources; r++) { - work[r] += allocation[p][r]; - } - safeSequence[count++] = p; - finish[p] = true; - found = true; - } - } - } - - // If no process can be allocated, then system is not in a safe state - if (!found) { - printf("System is not in a safe state.\n"); - return false; - } - } - - // Print safe sequence - printf("System is in a safe state.\nSafe sequence: "); - for (int i = 0; i < processes; i++) { - printf("%d ", safeSequence[i]); - } - printf("\n"); - return true; -} - -int main() { - int processes, resources; - int max[MAX_PROCESSES][MAX_RESOURCES]; - int allocation[MAX_PROCESSES][MAX_RESOURCES]; - int available[MAX_RESOURCES]; - - // Input the number of processes and resources - printf("Enter number of processes: "); - scanf("%d", &processes); - printf("Enter number of resources: "); - scanf("%d", &resources); - - // Input the Max matrix - printf("Enter the Max matrix:\n"); - for (int i = 0; i < processes; i++) { - for (int j = 0; j < resources; j++) { - scanf("%d", &max[i][j]); - } - } - - // Input the Allocation matrix - printf("Enter the Allocation matrix:\n"); - for (int i = 0; i < processes; i++) { - for (int j = 0; j < resources; j++) { - scanf("%d", &allocation[i][j]); - } - } - - // Input the Available resources - printf("Enter the Available resources:\n"); - for (int i = 0; i < resources; i++) { - scanf("%d", &available[i]); - } - - // Check if the system is in a safe state - isSafe(processes, resources, max, allocation, available); - - return 0; -} diff --git a/Scheduling Algorithm/README.md b/Scheduling Algorithm/README.md deleted file mode 100644 index cfce7ea9..00000000 --- a/Scheduling Algorithm/README.md +++ /dev/null @@ -1,65 +0,0 @@ -# Banker's Algorithm in C - -This project implements the **Banker's Algorithm** in C, a classic deadlock avoidance algorithm used in operating systems. The algorithm checks if resource allocation requests can be safely granted without causing deadlock by ensuring that the system remains in a "safe state" after each allocation. - -## Table of Contents -- [Introduction](#introduction) -- [Algorithm Overview](#algorithm-overview) -- [Features](#features) -- [Example](#example) -- [Limitations](#limitations) - -## Introduction - -The Banker's Algorithm, designed by Edsger Dijkstra, is a deadlock avoidance algorithm that allocates resources to processes only if the system can remain in a safe state. A "safe state" is one where a sequence of processes can complete without running into resource contention issues that would lead to deadlock. - -## Algorithm Overview - -The Banker's Algorithm operates based on three main matrices: -- **Max Matrix**: Indicates the maximum resources each process may require. -- **Allocation Matrix**: Shows the resources currently allocated to each process. -- **Available Vector**: Tracks the currently available resources in the system. - -When a process requests resources, the algorithm calculates if fulfilling the request will keep the system in a safe state. If so, the resources are allocated; otherwise, the request is denied. - -## Features - -- Deadlock avoidance for resource allocation. -- Identification of a safe sequence for process execution. -- Simple implementation using arrays and loops, compatible with standard C libraries. - -## Example - -### Example 1 - -#### Sample Input - -Enter number of processes: 3 -Enter number of resources: 3 - -Enter the Max matrix: -7 5 3 -3 2 2 -9 0 2 - -Enter the Allocation matrix: -0 1 0 -2 0 0 -3 0 2 - -Enter the Available resources: -3 3 2 - -#### Sample Input - -System is in a safe state. -Safe sequence: 1 0 2 - -## Limitations - -- **Fixed Resource and Process Count**: This implementation sets maximum limits for the number of processes and resources. Modifying these limits requires changing the code and recompiling. -- **Static Resource Allocation**: The algorithm only supports a static allocation based on the initial request. Dynamic resource changes or multiple resource requests are not handled. -- **Non-Preemptive**: Once allocated, resources cannot be preempted or re-assigned from one process to another, limiting flexibility. -- **Single Resource Type Per Process**: The current implementation assumes that each process only requests one type of resource at a time. For complex requests, code adjustments are needed. -- **Sequential Execution**: The algorithm performs sequential checks to find a safe sequence, which can be inefficient for systems with a large number of processes and resources. - From bf1e1ff77c8093f60a3ea5a6b88b69a40908cdc1 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:13:35 +0530 Subject: [PATCH 05/12] BANKER'S ALGORITHM.c --- Scheduling Algorithm/BANKER'S ALGORITHM.c | 100 ++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Scheduling Algorithm/BANKER'S ALGORITHM.c diff --git a/Scheduling Algorithm/BANKER'S ALGORITHM.c b/Scheduling Algorithm/BANKER'S ALGORITHM.c new file mode 100644 index 00000000..aed7da6c --- /dev/null +++ b/Scheduling Algorithm/BANKER'S ALGORITHM.c @@ -0,0 +1,100 @@ +#include +#include + +#define MAX_PROCESSES 10 +#define MAX_RESOURCES 10 + +// Function to check if the system is in a safe state +bool isSafe(int processes, int resources, int max[MAX_PROCESSES][MAX_RESOURCES], + int allocation[MAX_PROCESSES][MAX_RESOURCES], int available[MAX_RESOURCES]) { + + int work[MAX_RESOURCES]; + bool finish[MAX_PROCESSES] = {false}; + int safeSequence[MAX_PROCESSES]; + int count = 0; + + // Initialize work to available resources + for (int i = 0; i < resources; i++) { + work[i] = available[i]; + } + + // Find a safe sequence + while (count < processes) { + bool found = false; + for (int p = 0; p < processes; p++) { + if (!finish[p]) { + bool canAllocate = true; + for (int r = 0; r < resources; r++) { + if (max[p][r] - allocation[p][r] > work[r]) { + canAllocate = false; + break; + } + } + + // If a process can be allocated + if (canAllocate) { + for (int r = 0; r < resources; r++) { + work[r] += allocation[p][r]; + } + safeSequence[count++] = p; + finish[p] = true; + found = true; + } + } + } + + // If no process can be allocated, then system is not in a safe state + if (!found) { + printf("System is not in a safe state.\n"); + return false; + } + } + + // Print safe sequence + printf("System is in a safe state.\nSafe sequence: "); + for (int i = 0; i < processes; i++) { + printf("%d ", safeSequence[i]); + } + printf("\n"); + return true; +} + +int main() { + int processes, resources; + int max[MAX_PROCESSES][MAX_RESOURCES]; + int allocation[MAX_PROCESSES][MAX_RESOURCES]; + int available[MAX_RESOURCES]; + + // Input the number of processes and resources + printf("Enter number of processes: "); + scanf("%d", &processes); + printf("Enter number of resources: "); + scanf("%d", &resources); + + // Input the Max matrix + printf("Enter the Max matrix:\n"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + scanf("%d", &max[i][j]); + } + } + + // Input the Allocation matrix + printf("Enter the Allocation matrix:\n"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + scanf("%d", &allocation[i][j]); + } + } + + // Input the Available resources + printf("Enter the Available resources:\n"); + for (int i = 0; i < resources; i++) { + scanf("%d", &available[i]); + } + + // Check if the system is in a safe state + isSafe(processes, resources, max, allocation, available); + + return 0; +} From 6d95548646ddbf061ae2d7c7298b0918cdc93d31 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:14:40 +0530 Subject: [PATCH 06/12] Delete Scheduling Algorithm directory --- Scheduling Algorithm/BANKER'S ALGORITHM.c | 100 ---------------------- 1 file changed, 100 deletions(-) delete mode 100644 Scheduling Algorithm/BANKER'S ALGORITHM.c diff --git a/Scheduling Algorithm/BANKER'S ALGORITHM.c b/Scheduling Algorithm/BANKER'S ALGORITHM.c deleted file mode 100644 index aed7da6c..00000000 --- a/Scheduling Algorithm/BANKER'S ALGORITHM.c +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include - -#define MAX_PROCESSES 10 -#define MAX_RESOURCES 10 - -// Function to check if the system is in a safe state -bool isSafe(int processes, int resources, int max[MAX_PROCESSES][MAX_RESOURCES], - int allocation[MAX_PROCESSES][MAX_RESOURCES], int available[MAX_RESOURCES]) { - - int work[MAX_RESOURCES]; - bool finish[MAX_PROCESSES] = {false}; - int safeSequence[MAX_PROCESSES]; - int count = 0; - - // Initialize work to available resources - for (int i = 0; i < resources; i++) { - work[i] = available[i]; - } - - // Find a safe sequence - while (count < processes) { - bool found = false; - for (int p = 0; p < processes; p++) { - if (!finish[p]) { - bool canAllocate = true; - for (int r = 0; r < resources; r++) { - if (max[p][r] - allocation[p][r] > work[r]) { - canAllocate = false; - break; - } - } - - // If a process can be allocated - if (canAllocate) { - for (int r = 0; r < resources; r++) { - work[r] += allocation[p][r]; - } - safeSequence[count++] = p; - finish[p] = true; - found = true; - } - } - } - - // If no process can be allocated, then system is not in a safe state - if (!found) { - printf("System is not in a safe state.\n"); - return false; - } - } - - // Print safe sequence - printf("System is in a safe state.\nSafe sequence: "); - for (int i = 0; i < processes; i++) { - printf("%d ", safeSequence[i]); - } - printf("\n"); - return true; -} - -int main() { - int processes, resources; - int max[MAX_PROCESSES][MAX_RESOURCES]; - int allocation[MAX_PROCESSES][MAX_RESOURCES]; - int available[MAX_RESOURCES]; - - // Input the number of processes and resources - printf("Enter number of processes: "); - scanf("%d", &processes); - printf("Enter number of resources: "); - scanf("%d", &resources); - - // Input the Max matrix - printf("Enter the Max matrix:\n"); - for (int i = 0; i < processes; i++) { - for (int j = 0; j < resources; j++) { - scanf("%d", &max[i][j]); - } - } - - // Input the Allocation matrix - printf("Enter the Allocation matrix:\n"); - for (int i = 0; i < processes; i++) { - for (int j = 0; j < resources; j++) { - scanf("%d", &allocation[i][j]); - } - } - - // Input the Available resources - printf("Enter the Available resources:\n"); - for (int i = 0; i < resources; i++) { - scanf("%d", &available[i]); - } - - // Check if the system is in a safe state - isSafe(processes, resources, max, allocation, available); - - return 0; -} From 8b9550078bb88920e55dcc0a9cbfb497901927a8 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:16:25 +0530 Subject: [PATCH 07/12] Banker's Algorithm.c --- .../Banker's Algorithm.c | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Deadlock avoidance algorithm/Banker's Algorithm.c diff --git a/Deadlock avoidance algorithm/Banker's Algorithm.c b/Deadlock avoidance algorithm/Banker's Algorithm.c new file mode 100644 index 00000000..aed7da6c --- /dev/null +++ b/Deadlock avoidance algorithm/Banker's Algorithm.c @@ -0,0 +1,100 @@ +#include +#include + +#define MAX_PROCESSES 10 +#define MAX_RESOURCES 10 + +// Function to check if the system is in a safe state +bool isSafe(int processes, int resources, int max[MAX_PROCESSES][MAX_RESOURCES], + int allocation[MAX_PROCESSES][MAX_RESOURCES], int available[MAX_RESOURCES]) { + + int work[MAX_RESOURCES]; + bool finish[MAX_PROCESSES] = {false}; + int safeSequence[MAX_PROCESSES]; + int count = 0; + + // Initialize work to available resources + for (int i = 0; i < resources; i++) { + work[i] = available[i]; + } + + // Find a safe sequence + while (count < processes) { + bool found = false; + for (int p = 0; p < processes; p++) { + if (!finish[p]) { + bool canAllocate = true; + for (int r = 0; r < resources; r++) { + if (max[p][r] - allocation[p][r] > work[r]) { + canAllocate = false; + break; + } + } + + // If a process can be allocated + if (canAllocate) { + for (int r = 0; r < resources; r++) { + work[r] += allocation[p][r]; + } + safeSequence[count++] = p; + finish[p] = true; + found = true; + } + } + } + + // If no process can be allocated, then system is not in a safe state + if (!found) { + printf("System is not in a safe state.\n"); + return false; + } + } + + // Print safe sequence + printf("System is in a safe state.\nSafe sequence: "); + for (int i = 0; i < processes; i++) { + printf("%d ", safeSequence[i]); + } + printf("\n"); + return true; +} + +int main() { + int processes, resources; + int max[MAX_PROCESSES][MAX_RESOURCES]; + int allocation[MAX_PROCESSES][MAX_RESOURCES]; + int available[MAX_RESOURCES]; + + // Input the number of processes and resources + printf("Enter number of processes: "); + scanf("%d", &processes); + printf("Enter number of resources: "); + scanf("%d", &resources); + + // Input the Max matrix + printf("Enter the Max matrix:\n"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + scanf("%d", &max[i][j]); + } + } + + // Input the Allocation matrix + printf("Enter the Allocation matrix:\n"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + scanf("%d", &allocation[i][j]); + } + } + + // Input the Available resources + printf("Enter the Available resources:\n"); + for (int i = 0; i < resources; i++) { + scanf("%d", &available[i]); + } + + // Check if the system is in a safe state + isSafe(processes, resources, max, allocation, available); + + return 0; +} From 22a294d060eabd5d7fb702c67f1b66b301066511 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:24:42 +0530 Subject: [PATCH 08/12] README.md --- Deadlock avoidance algorithm/README.md | 64 ++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Deadlock avoidance algorithm/README.md diff --git a/Deadlock avoidance algorithm/README.md b/Deadlock avoidance algorithm/README.md new file mode 100644 index 00000000..9b9ee927 --- /dev/null +++ b/Deadlock avoidance algorithm/README.md @@ -0,0 +1,64 @@ +# Banker's Algorithm in C + +This project implements the **Banker's Algorithm** in C, a classic deadlock avoidance algorithm used in operating systems. The algorithm checks if resource allocation requests can be safely granted without causing deadlock by ensuring that the system remains in a "safe state" after each allocation. + +## Table of Contents +- [Introduction](#introduction) +- [Algorithm Overview](#algorithm-overview) +- [Features](#features) +- [Example](#example) +- [Limitations](#limitations) + +## Introduction + +The Banker's Algorithm, designed by Edsger Dijkstra, is a deadlock avoidance algorithm that allocates resources to processes only if the system can remain in a safe state. A "safe state" is one where a sequence of processes can complete without running into resource contention issues that would lead to deadlock. + +## Algorithm Overview + +The Banker's Algorithm operates based on three main matrices: +- **Max Matrix**: Indicates the maximum resources each process may require. +- **Allocation Matrix**: Shows the resources currently allocated to each process. +- **Available Vector**: Tracks the currently available resources in the system. + +When a process requests resources, the algorithm calculates if fulfilling the request will keep the system in a safe state. If so, the resources are allocated; otherwise, the request is denied. + +## Features + +- Deadlock avoidance for resource allocation. +- Identification of a safe sequence for process execution. +- Simple implementation using arrays and loops, compatible with standard C libraries. + +## Example + +### Example 1 + +#### Sample Input + +Enter number of processes: 3 +Enter number of resources: 3 + +Enter the Max matrix: +7 5 3 +3 2 2 +9 0 2 + +Enter the Allocation matrix: +0 1 0 +2 0 0 +3 0 2 + +Enter the Available resources: +3 3 2 + +#### Sample Input + +System is in a safe state. +Safe sequence: 1 0 2 + +## Limitations + +- **Fixed Resource and Process Count**: This implementation sets maximum limits for the number of processes and resources. Modifying these limits requires changing the code and recompiling. +- **Static Resource Allocation**: The algorithm only supports a static allocation based on the initial request. Dynamic resource changes or multiple resource requests are not handled. +- **Non-Preemptive**: Once allocated, resources cannot be preempted or re-assigned from one process to another, limiting flexibility. +- **Single Resource Type Per Process**: The current implementation assumes that each process only requests one type of resource at a time. For complex requests, code adjustments are needed. +- **Sequential Execution**: The algorithm performs sequential checks to find a safe sequence, which can be inefficient for systems with a large number of processes and resources. From 6cb1f7e93f5c2e249721ea240a059b272973a1d0 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:35:47 +0530 Subject: [PATCH 09/12] Delete Deadlock avoidance algorithm/README.md --- Deadlock avoidance algorithm/README.md | 64 -------------------------- 1 file changed, 64 deletions(-) delete mode 100644 Deadlock avoidance algorithm/README.md diff --git a/Deadlock avoidance algorithm/README.md b/Deadlock avoidance algorithm/README.md deleted file mode 100644 index 9b9ee927..00000000 --- a/Deadlock avoidance algorithm/README.md +++ /dev/null @@ -1,64 +0,0 @@ -# Banker's Algorithm in C - -This project implements the **Banker's Algorithm** in C, a classic deadlock avoidance algorithm used in operating systems. The algorithm checks if resource allocation requests can be safely granted without causing deadlock by ensuring that the system remains in a "safe state" after each allocation. - -## Table of Contents -- [Introduction](#introduction) -- [Algorithm Overview](#algorithm-overview) -- [Features](#features) -- [Example](#example) -- [Limitations](#limitations) - -## Introduction - -The Banker's Algorithm, designed by Edsger Dijkstra, is a deadlock avoidance algorithm that allocates resources to processes only if the system can remain in a safe state. A "safe state" is one where a sequence of processes can complete without running into resource contention issues that would lead to deadlock. - -## Algorithm Overview - -The Banker's Algorithm operates based on three main matrices: -- **Max Matrix**: Indicates the maximum resources each process may require. -- **Allocation Matrix**: Shows the resources currently allocated to each process. -- **Available Vector**: Tracks the currently available resources in the system. - -When a process requests resources, the algorithm calculates if fulfilling the request will keep the system in a safe state. If so, the resources are allocated; otherwise, the request is denied. - -## Features - -- Deadlock avoidance for resource allocation. -- Identification of a safe sequence for process execution. -- Simple implementation using arrays and loops, compatible with standard C libraries. - -## Example - -### Example 1 - -#### Sample Input - -Enter number of processes: 3 -Enter number of resources: 3 - -Enter the Max matrix: -7 5 3 -3 2 2 -9 0 2 - -Enter the Allocation matrix: -0 1 0 -2 0 0 -3 0 2 - -Enter the Available resources: -3 3 2 - -#### Sample Input - -System is in a safe state. -Safe sequence: 1 0 2 - -## Limitations - -- **Fixed Resource and Process Count**: This implementation sets maximum limits for the number of processes and resources. Modifying these limits requires changing the code and recompiling. -- **Static Resource Allocation**: The algorithm only supports a static allocation based on the initial request. Dynamic resource changes or multiple resource requests are not handled. -- **Non-Preemptive**: Once allocated, resources cannot be preempted or re-assigned from one process to another, limiting flexibility. -- **Single Resource Type Per Process**: The current implementation assumes that each process only requests one type of resource at a time. For complex requests, code adjustments are needed. -- **Sequential Execution**: The algorithm performs sequential checks to find a safe sequence, which can be inefficient for systems with a large number of processes and resources. From e0a31049ea84c35ebb4e36eb37339dbbbf83cb5c Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:36:09 +0530 Subject: [PATCH 10/12] Readme.md --- Deadlock avoidance algorithm/Readme.md | 64 ++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Deadlock avoidance algorithm/Readme.md diff --git a/Deadlock avoidance algorithm/Readme.md b/Deadlock avoidance algorithm/Readme.md new file mode 100644 index 00000000..9b9ee927 --- /dev/null +++ b/Deadlock avoidance algorithm/Readme.md @@ -0,0 +1,64 @@ +# Banker's Algorithm in C + +This project implements the **Banker's Algorithm** in C, a classic deadlock avoidance algorithm used in operating systems. The algorithm checks if resource allocation requests can be safely granted without causing deadlock by ensuring that the system remains in a "safe state" after each allocation. + +## Table of Contents +- [Introduction](#introduction) +- [Algorithm Overview](#algorithm-overview) +- [Features](#features) +- [Example](#example) +- [Limitations](#limitations) + +## Introduction + +The Banker's Algorithm, designed by Edsger Dijkstra, is a deadlock avoidance algorithm that allocates resources to processes only if the system can remain in a safe state. A "safe state" is one where a sequence of processes can complete without running into resource contention issues that would lead to deadlock. + +## Algorithm Overview + +The Banker's Algorithm operates based on three main matrices: +- **Max Matrix**: Indicates the maximum resources each process may require. +- **Allocation Matrix**: Shows the resources currently allocated to each process. +- **Available Vector**: Tracks the currently available resources in the system. + +When a process requests resources, the algorithm calculates if fulfilling the request will keep the system in a safe state. If so, the resources are allocated; otherwise, the request is denied. + +## Features + +- Deadlock avoidance for resource allocation. +- Identification of a safe sequence for process execution. +- Simple implementation using arrays and loops, compatible with standard C libraries. + +## Example + +### Example 1 + +#### Sample Input + +Enter number of processes: 3 +Enter number of resources: 3 + +Enter the Max matrix: +7 5 3 +3 2 2 +9 0 2 + +Enter the Allocation matrix: +0 1 0 +2 0 0 +3 0 2 + +Enter the Available resources: +3 3 2 + +#### Sample Input + +System is in a safe state. +Safe sequence: 1 0 2 + +## Limitations + +- **Fixed Resource and Process Count**: This implementation sets maximum limits for the number of processes and resources. Modifying these limits requires changing the code and recompiling. +- **Static Resource Allocation**: The algorithm only supports a static allocation based on the initial request. Dynamic resource changes or multiple resource requests are not handled. +- **Non-Preemptive**: Once allocated, resources cannot be preempted or re-assigned from one process to another, limiting flexibility. +- **Single Resource Type Per Process**: The current implementation assumes that each process only requests one type of resource at a time. For complex requests, code adjustments are needed. +- **Sequential Execution**: The algorithm performs sequential checks to find a safe sequence, which can be inefficient for systems with a large number of processes and resources. From 5cbe9aa7f8ba6cbf76d97b6c64b38fe156deb1e5 Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:38:04 +0530 Subject: [PATCH 11/12] Delete Deadlock avoidance algorithm/Banker's Algorithm.c --- .../Banker's Algorithm.c | 100 ------------------ 1 file changed, 100 deletions(-) delete mode 100644 Deadlock avoidance algorithm/Banker's Algorithm.c diff --git a/Deadlock avoidance algorithm/Banker's Algorithm.c b/Deadlock avoidance algorithm/Banker's Algorithm.c deleted file mode 100644 index aed7da6c..00000000 --- a/Deadlock avoidance algorithm/Banker's Algorithm.c +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include - -#define MAX_PROCESSES 10 -#define MAX_RESOURCES 10 - -// Function to check if the system is in a safe state -bool isSafe(int processes, int resources, int max[MAX_PROCESSES][MAX_RESOURCES], - int allocation[MAX_PROCESSES][MAX_RESOURCES], int available[MAX_RESOURCES]) { - - int work[MAX_RESOURCES]; - bool finish[MAX_PROCESSES] = {false}; - int safeSequence[MAX_PROCESSES]; - int count = 0; - - // Initialize work to available resources - for (int i = 0; i < resources; i++) { - work[i] = available[i]; - } - - // Find a safe sequence - while (count < processes) { - bool found = false; - for (int p = 0; p < processes; p++) { - if (!finish[p]) { - bool canAllocate = true; - for (int r = 0; r < resources; r++) { - if (max[p][r] - allocation[p][r] > work[r]) { - canAllocate = false; - break; - } - } - - // If a process can be allocated - if (canAllocate) { - for (int r = 0; r < resources; r++) { - work[r] += allocation[p][r]; - } - safeSequence[count++] = p; - finish[p] = true; - found = true; - } - } - } - - // If no process can be allocated, then system is not in a safe state - if (!found) { - printf("System is not in a safe state.\n"); - return false; - } - } - - // Print safe sequence - printf("System is in a safe state.\nSafe sequence: "); - for (int i = 0; i < processes; i++) { - printf("%d ", safeSequence[i]); - } - printf("\n"); - return true; -} - -int main() { - int processes, resources; - int max[MAX_PROCESSES][MAX_RESOURCES]; - int allocation[MAX_PROCESSES][MAX_RESOURCES]; - int available[MAX_RESOURCES]; - - // Input the number of processes and resources - printf("Enter number of processes: "); - scanf("%d", &processes); - printf("Enter number of resources: "); - scanf("%d", &resources); - - // Input the Max matrix - printf("Enter the Max matrix:\n"); - for (int i = 0; i < processes; i++) { - for (int j = 0; j < resources; j++) { - scanf("%d", &max[i][j]); - } - } - - // Input the Allocation matrix - printf("Enter the Allocation matrix:\n"); - for (int i = 0; i < processes; i++) { - for (int j = 0; j < resources; j++) { - scanf("%d", &allocation[i][j]); - } - } - - // Input the Available resources - printf("Enter the Available resources:\n"); - for (int i = 0; i < resources; i++) { - scanf("%d", &available[i]); - } - - // Check if the system is in a safe state - isSafe(processes, resources, max, allocation, available); - - return 0; -} From 8d47fcfbee2da9c65bfab18a40d3d763c0478dde Mon Sep 17 00:00:00 2001 From: Ashmita Barthwal <149078715+AshmitaBarthwal@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:38:47 +0530 Subject: [PATCH 12/12] Banker's Algorithm.c --- .../Banker's Algorithm.c | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Deadlock avoidance algorithm/Banker's Algorithm.c diff --git a/Deadlock avoidance algorithm/Banker's Algorithm.c b/Deadlock avoidance algorithm/Banker's Algorithm.c new file mode 100644 index 00000000..aed7da6c --- /dev/null +++ b/Deadlock avoidance algorithm/Banker's Algorithm.c @@ -0,0 +1,100 @@ +#include +#include + +#define MAX_PROCESSES 10 +#define MAX_RESOURCES 10 + +// Function to check if the system is in a safe state +bool isSafe(int processes, int resources, int max[MAX_PROCESSES][MAX_RESOURCES], + int allocation[MAX_PROCESSES][MAX_RESOURCES], int available[MAX_RESOURCES]) { + + int work[MAX_RESOURCES]; + bool finish[MAX_PROCESSES] = {false}; + int safeSequence[MAX_PROCESSES]; + int count = 0; + + // Initialize work to available resources + for (int i = 0; i < resources; i++) { + work[i] = available[i]; + } + + // Find a safe sequence + while (count < processes) { + bool found = false; + for (int p = 0; p < processes; p++) { + if (!finish[p]) { + bool canAllocate = true; + for (int r = 0; r < resources; r++) { + if (max[p][r] - allocation[p][r] > work[r]) { + canAllocate = false; + break; + } + } + + // If a process can be allocated + if (canAllocate) { + for (int r = 0; r < resources; r++) { + work[r] += allocation[p][r]; + } + safeSequence[count++] = p; + finish[p] = true; + found = true; + } + } + } + + // If no process can be allocated, then system is not in a safe state + if (!found) { + printf("System is not in a safe state.\n"); + return false; + } + } + + // Print safe sequence + printf("System is in a safe state.\nSafe sequence: "); + for (int i = 0; i < processes; i++) { + printf("%d ", safeSequence[i]); + } + printf("\n"); + return true; +} + +int main() { + int processes, resources; + int max[MAX_PROCESSES][MAX_RESOURCES]; + int allocation[MAX_PROCESSES][MAX_RESOURCES]; + int available[MAX_RESOURCES]; + + // Input the number of processes and resources + printf("Enter number of processes: "); + scanf("%d", &processes); + printf("Enter number of resources: "); + scanf("%d", &resources); + + // Input the Max matrix + printf("Enter the Max matrix:\n"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + scanf("%d", &max[i][j]); + } + } + + // Input the Allocation matrix + printf("Enter the Allocation matrix:\n"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + scanf("%d", &allocation[i][j]); + } + } + + // Input the Available resources + printf("Enter the Available resources:\n"); + for (int i = 0; i < resources; i++) { + scanf("%d", &available[i]); + } + + // Check if the system is in a safe state + isSafe(processes, resources, max, allocation, available); + + return 0; +}