GithubHelp home page GithubHelp logo

am0stafa / deadlock-avoidance Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 268 KB

Deadlock-Avoidance is a comprehensive toolkit focusing on deadlock prevention strategies in system design. It offers simulations for the Banker's Algorithm, deadlocks, and race conditions, alongside a Resource Allocation Graph (RAG) visualizer. A great resource for understanding and visualizing deadlock concepts in multi-threaded environments.

Python 44.39% Java 55.61%

deadlock-avoidance's Introduction

DeadLock Avoidance

Deadlock avoidance requires that the system has some additional a priori information regarding the resources that each process will request. Using this information, the system can decide whether or not to allocate a resource to a process at a particular point in time, in order to ensure that the system will remain in a safe state. There are several methods to achieve deadlock avoidance:

  1. Banker's Algorithm:

    • This algorithm is used in a system where the number and types of resources are known in advance.
    • It keeps track of all resource allocation and process states to ensure that the system will not enter an unsafe state which could lead to deadlock.
    • It's more suitable for systems with a small number of processes and resources due to its complexity.
  2. Resource Allocation Graph (RAG):

    • The Resource Allocation Graph algorithm is utilized when resources have only a single instance.
    • It uses a directed graph to keep track of all the resources and processes in the system, as well as the requests and allocations of resources.
    • A cycle in the graph is used as an indication of deadlock.

Each mechanism is suited for different scenarios depending on the nature and complexity of the system, the number and types of resources, and the requirements of the processes. For instance, Banker's Algorithm would be more suitable in a controlled environment with a known set of resources and processes, while Resource Ordering or Incremental Resource Allocation might be better suited for more dynamic or complex systems.

1) Banker's Algorithm Simulator

Banker's Algorithm explained in more details

Let's consider a simplified example where we have three processes (P0, P1, and P2) and three types of resources (A, B, and C). We'll illustrate how the Banker's Algorithm would work with these processes and resources.

Here's the initial state of the system:

Max Demand Allocation Need Available
A B C A B C A B C A B C
P0 7 5 3 0 1 0 7 4 3 3 3 2
P1 3 2 2 2 0 0 1 2 2
P2 9 0 2 3 0 2 6 0 0
  1. Max Demand: The maximum number of instances of each resource type that a process may need.
  2. Allocation: The number of instances of each resource type currently allocated to a process.
  3. Need: The remaining resource need for each process (Need = Max Demand - Allocation).
  4. Available: The number of instances of each resource type currently available in the system.

Now, suppose process P1 requests one more instance of resource B. To check whether this request can be granted, the Banker's Algorithm performs a provisional allocation and checks for safety.

Provisional Allocation:

Allocation Need Available
A B C A B C A B C
P0 0 1 0 7 4 3 3 2 2
P1 2 1 0 1 1 2
P2 3 0 2 6 0 0

Now we'll check for a safe sequence:

  • P2 can be the first process in the sequence since its current needs can be satisfied by the available resources.
  • Once P2 completes, it releases its resources, making the available resources A=6, B=2, C=4.
  • Now, P1 can proceed since its current needs can be satisfied by the available resources.
  • Once P1 completes, it releases its resources, making the available resources A=8, B=3, C=4.
  • Finally, P0 can proceed with the now available resources.

So, a safe sequence (P2, P1, P0) has been found, indicating that the system is in a safe state after the provisional allocation. Therefore, the request by process P1 can be granted.

This simplified example demonstrates how the Banker's Algorithm checks for safety before granting a resource request, ensuring that the system remains deadlock-free.

Overview

The Banker's Algorithm is a renowned resource allocation and deadlock avoidance algorithm used in operating system design to prevent deadlock by evaluating resource requests against available resources. This simulator aims to demonstrate the Banker's Algorithm with support for N processes and M resource types (with a constraint of N < 10 and M < 10).

File Structure

The input to this simulator is a text file that represents the initial system state. It is structured as:

<number_of_processes>
<number_of_resources>
| max | |alloc.| |ttl res
<max values, alloc values, total resources>
...

Example:

5  # processes
4  # resources
3,2,2,2,2,1,1,0,5,6,8,4
2,1,1,2,0,1,1,0,5,6,8,4
... 

Where:

  • number_of_processes specifies the number of processes.
  • number_of_resources specifies the number of resource types.
  • Each subsequent line represents the data for a process. The data includes:
    • The maximum demand of resources for the process.
    • The currently allocated resources to the process.
    • The total resources available in the system.

How the Code Works

  1. SetupFile Method:

    • Reads the input file to initialize various arrays and matrices such as max, allocate, need, allocatedResources, and ttlResources.
    • Calculates the initial available resources by subtracting the allocated resources from the total resources.
  2. CheckSafeSeq Method:

    • Checks for the existence of a safe sequence by verifying if need[i] <= initial[i] for any process.
  3. PrintMatrix Method:

    • Displays the system matrices (max, allocate, need) and the available resources.
  4. Check Method:

    • Checks if the needs of a particular process can be satisfied by the currently available resources. It also keeps track of multiple solutions.
  5. Algorithm Method:

    • Executes the Banker's Algorithm:
      • It releases resources held by a process if the process's needs can be satisfied.
      • Updates the available resources.
      • Tracks the processes in the safe sequence.
  6. Main Method:

    • Sets up the file, checks if a safe sequence exists, and prints the system matrix. The Banker's Algorithm is then executed to find potential safe sequences.

Execution Steps

  1. Run the program.
  2. When prompted, input the filename (without .txt extension) containing the system state.
  3. The program will read the file, display the system matrices, and find potential safe sequences.
  4. If a safe sequence exists, it will be displayed. If not, the program will output "There is no safe Sequence!"

Testing

Three sample text files have been provided for testing purposes. These can be used to check various scenarios of the Banker's Algorithm. Make sure to specify the correct path when prompted for the filename.

Conclusion

This Banker's Algorithm simulator effectively visualizes the algorithm's process of determining safe sequences, giving the user insights into how resource allocation and deadlock avoidance operate in an OS environment.


2) Deadlock Simulator in Python

This simulator demonstrates a typical deadlock scenario in a multithreaded environment. A deadlock arises when two or more threads are permanently blocked, each waiting indefinitely for the other to release a lock.

In this simulation, two threads, thread_1 and thread_2, are created. The thread_1 attempts to acquire lock1 and then lock2, while thread_2 attempts to acquire lock2 and then lock1. This leads to a situation where each thread is waiting for the other to release a lock, resulting in a deadlock.

How to Run

  1. Make sure you have Python installed on your system.
  2. Get into the directory by cd Deadlock-Avoidance.
  3. Run the code by simulation.py.

Expected Output

You might observe the following output, although the exact sequence might vary:

Thread 1 acquired lock1
Thread 2 acquired lock2

After this, both threads will be in a deadlock situation, waiting for each other to release their respective locks.

Caution

This is a simple simulator to demonstrate a deadlock. It's important to understand the implications and the challenges of multithreaded environments in real-world applications. Deadlocks can lead to unresponsive systems and are often hard to diagnose and resolve.


3) Race Condition Simulator

This Python-based simulator is designed to demonstrate race conditions, a type of concurrency bug that can occur when multiple threads attempt to access and modify shared resources without proper synchronization. In the provided simulation1.py, an array of numbers is divided into ten parts, and each part is summed up by a separate thread. Due to a lack of synchronization mechanisms, there's a chance that race conditions will occur, leading to an incorrect total sum.

How it Works

  • Array of Numbers: The program initializes a large array of numbers named numbers.

  • Global Total Sum: There's a shared global variable total_sum which holds the sum of the numbers and will be updated by all threads.

  • sum_numbers Function: Each thread will execute this function, which is responsible for summing a slice of the array. Due to the deliberate introduction of a small delay using time.sleep(), race conditions are more likely to occur.

  • Thread Creation and Execution: The array is divided into ten slices, and ten threads are created to process these slices. Once started, each thread will attempt to update the total_sum without synchronization.

  • Checking for Race Condition: After all threads have completed their execution, the program will compute the actual sum of the numbers in the array and compare it to the total_sum. If they don't match, a race condition has been detected.

Note

This simulator is a simplified representation of how race conditions can occur in multi-threaded environments. It's essential to recognize that in real-world scenarios, race conditions might be less predictable and could result in various issues, including data corruption and application crashes.


4) Resource Allocation Graph (RAG)

This project aims to create a Resource Allocation Graph (RAG) to keep track of resources and processes in a system, and to visualize this graph along with detecting cycles which could indicate potential deadlocks.

Overview

Resource Allocation Graph (RAG) is a data structure that represents processes, resources, and the relationships between them to prevent and avoid deadlock situations. This implementation allows you to:

  • Define processes with unique names and priorities.
  • Define resources with unique names and available instances.
  • Allocate specific instances of resources to processes.
  • Release instances of resources from processes.
  • Visualize the Resource Allocation Graph.

Features

  1. Easy-to-Use Command-Line Interface: Define processes, resources, allocations, and releases via command-line arguments.
  2. Visualization Support: Plot the RAG with process and resource nodes using networkx and matplotlib.
  3. Deadlock Detection: Upon visualizing the graph, the system will detect and notify if a cycle (potential deadlock) exists.

Usage

The script can be executed from the command line using the following format:

python rag.py --processes [ProcessName:Priority ...] --resources [ResourceName:Instances ...] --allocate [ProcessName:ResourceName:Instances ...] --release [ProcessName:ResourceName:Instances ...]

How the Code Works

  1. Data Classes:

    • Process Class:
      • Holds information about a process including its name, priority, allocated resources, and requested resources.
    • Resource Class:
      • Holds information about a resource including its name, the number of available instances, and allocations to processes.
  2. Resource Allocation Graph (RAG) Class:

    • ResourceAllocationGraph Class:
      • Manages the collection of Process and Resource objects.
      • Provides methods to add processes and resources, allocate and release resources, and visualize the graph.
  3. Resource and Process Management:

    • add_process Method:
      • Adds a new process to the RAG with a specified name and priority.
    • add_resource Method:
      • Adds a new resource to the RAG with a specified name and number of instances.
    • allocate Method:
      • Allocates a specified number of instances of a resource to a process.
    • release Method:
      • Releases a specified number of instances of a resource from a process back to the resource pool.
  4. Graph Visualization:

    • visualize Method:
      • Uses NetworkX and Matplotlib to create a visual representation of the RAG.
      • Processes are represented as red nodes, resources as blue nodes, and allocations as edges with labels indicating the number of allocated instances.
      • Checks for cycles in the graph to detect potential deadlocks and prints the result to the console.
  5. User Input Handling:

    • The argparse library is used to define command-line arguments for specifying processes, resources, allocation requests, and release requests.
    • Users can input data through command-line arguments when launching the script.
  6. Main Function:

    • Parses command-line arguments using argparse.
    • Initializes a ResourceAllocationGraph object.
    • Processes user input to add processes and resources, and handle allocation and release requests.
    • Calls the visualize method to display the graph and indicate whether a cycle is detected.
  7. Cycle Detection:

    • Within the visualize method, the nx.is_directed_acyclic_graph() function from NetworkX is used to check for cycles in the graph, which would indicate potential deadlocks.
  8. Command-Line Interface:

    • Users can interact with the script through command-line arguments to specify processes, resources, allocation requests, and release requests.
  9. Error Handling:

    • Comprehensive error handling is provided to manage scenarios like invalid input, insufficient resource instances, non-existing processes or resources, etc.
  10. Execution:

    • When the script is run, it processes the command-line arguments, manages the resources and processes as per user input, visualizes the RAG, and checks for cycles to detect potential deadlocks.

Parameters

  • --processes: List of processes and their priorities. Format: ProcessName:Priority.
  • --resources: List of resources and their available instances. Format: ResourceName:Instances.
  • --allocate: Allocation requests specifying which process requests which resource and how many instances. Format: ProcessName:ResourceName:Instances.
  • --release: Resource release requests specifying which process releases which resource and how many instances. Format: ProcessName:ResourceName:Instances.

Example

python rag.py --processes P1:1 P2:2 --resources R1:3 R2:4 --allocate P1:R1:2 P2:R2:3 --release P1:R1:1

Implementation Details

  • Process: Represents a process with a name, priority, allocated resources, and requested resources.
  • Resource: Represents a resource with a name, total instances, and allocated instances to processes.
  • ResourceAllocationGraph: Main class to manage processes, resources, allocations, and visualizations.

deadlock-avoidance's People

Contributors

am0stafa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.