|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "def is_safe_state(available, max_claim, allocation):\n", |
| 10 | + " # Get the number of resources and processes\n", |
| 11 | + " num_resources = len(available)\n", |
| 12 | + " num_processes = len(max_claim)\n", |
| 13 | + "\n", |
| 14 | + " # Create a copy of available resources (work) and initialize finish array\n", |
| 15 | + " work = available.copy()\n", |
| 16 | + " finish = [False] * num_processes\n", |
| 17 | + "\n", |
| 18 | + " while True:\n", |
| 19 | + " found = False\n", |
| 20 | + " # Iterate through each process\n", |
| 21 | + " for i in range(num_processes):\n", |
| 22 | + " # Check if the process is not finished and if it can be executed\n", |
| 23 | + " if not finish[i] and all(work[j] >= max_claim[i][j] - allocation[i][j] for j in range(num_resources)):\n", |
| 24 | + " found = True\n", |
| 25 | + " # Mark the process as finished\n", |
| 26 | + " finish[i] = True\n", |
| 27 | + " # Add the allocated resources to the work vector\n", |
| 28 | + " work = [work[j] + allocation[i][j] for j in range(num_resources)]\n", |
| 29 | + " break\n", |
| 30 | + "\n", |
| 31 | + " # If no process can be executed, break out of the loop\n", |
| 32 | + " if not found:\n", |
| 33 | + " break\n", |
| 34 | + "\n", |
| 35 | + " # Check if all processes have finished (i.e., system is in a safe state)\n", |
| 36 | + " return all(finish)\n", |
| 37 | + "\n", |
| 38 | + "def deadlock_detection(available, max_claim, allocation):\n", |
| 39 | + " # Check if the current state is safe or in a deadlock\n", |
| 40 | + " if is_safe_state(available, max_claim, allocation):\n", |
| 41 | + " print(\"The system is in a safe state. No deadlock detected.\")\n", |
| 42 | + " else:\n", |
| 43 | + " print(\"Deadlock detected. The system is in an unsafe state.\")\n", |
| 44 | + "\n", |
| 45 | + "if __name__ == \"__main__\":\n", |
| 46 | + " # case 1\n", |
| 47 | + " # Define the available resources, maximum claim of each process, and current allocation\n", |
| 48 | + " available_resources = [3, 3, 2]\n", |
| 49 | + " max_claim_per_process = [\n", |
| 50 | + " [7, 5, 3],\n", |
| 51 | + " [3, 2, 2],\n", |
| 52 | + " [9, 0, 2],\n", |
| 53 | + " [2, 2, 2],\n", |
| 54 | + " [4, 3, 3]\n", |
| 55 | + " ]\n", |
| 56 | + " current_allocation = [\n", |
| 57 | + " [0, 1, 0],\n", |
| 58 | + " [2, 0, 0],\n", |
| 59 | + " [3, 0, 2],\n", |
| 60 | + " [2, 1, 1],\n", |
| 61 | + " [0, 0, 2]\n", |
| 62 | + " ]\n", |
| 63 | + "\n", |
| 64 | + " # Perform deadlock detection\n", |
| 65 | + " deadlock_detection(available_resources, max_claim_per_process, current_allocation)\n" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": null, |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "# case 2\n", |
| 75 | + "if __name__ == \"__main__\":\n", |
| 76 | + " # Define the deadlock scenario\n", |
| 77 | + " available_resources = [2, 2, 2]\n", |
| 78 | + " max_claim_per_process = [\n", |
| 79 | + " [3, 2, 2],\n", |
| 80 | + " [1, 2, 4],\n", |
| 81 | + " [4, 4, 3],\n", |
| 82 | + " [2, 3, 2]\n", |
| 83 | + " ]\n", |
| 84 | + " current_allocation = [\n", |
| 85 | + " [0, 1, 0],\n", |
| 86 | + " [0, 0, 1],\n", |
| 87 | + " [2, 1, 1],\n", |
| 88 | + " [1, 0, 0]\n", |
| 89 | + " ]\n", |
| 90 | + "\n", |
| 91 | + " # Perform deadlock detection\n", |
| 92 | + " deadlock_detection(available_resources, max_claim_per_process, current_allocation)\n" |
| 93 | + ] |
| 94 | + } |
| 95 | + ], |
| 96 | + "metadata": { |
| 97 | + "kernelspec": { |
| 98 | + "display_name": "Python 3", |
| 99 | + "language": "python", |
| 100 | + "name": "python3" |
| 101 | + }, |
| 102 | + "language_info": { |
| 103 | + "codemirror_mode": { |
| 104 | + "name": "ipython", |
| 105 | + "version": 3 |
| 106 | + }, |
| 107 | + "file_extension": ".py", |
| 108 | + "mimetype": "text/x-python", |
| 109 | + "name": "python", |
| 110 | + "nbconvert_exporter": "python", |
| 111 | + "pygments_lexer": "ipython3", |
| 112 | + "version": "3.11.0" |
| 113 | + }, |
| 114 | + "orig_nbformat": 4 |
| 115 | + }, |
| 116 | + "nbformat": 4, |
| 117 | + "nbformat_minor": 2 |
| 118 | +} |
0 commit comments