Operating System
Synchronization and Deadlock

The Critical Section Problem

The critical section denotes a specific part of code or a block where a process or thread interacts with a shared resource like a variable, file, or database. To preserve data integrity and prevent conflicts, only one process or thread is permitted to enter the critical section at any given time. This precaution ensures that multiple processes do not interfere with each other,simultaneously modifying shared resources.

  • Critical Section:
    This is the part of the code where shared variables are accessed and/or updated.

  • Remainder Section:
    The rest of the program excluding the Critical Section.


Process Synchronization (Solution to Critical Section Problem)

Process synchronization is like a traffic signal to control the flow of vehicles at an intersection. If multiple processes or threads are concurrently handling different tasks, process synchronization ensures their effective cooperation and communication to prevent conflicts and maintain a proper execution order. This mechanism addresses potential issues like race conditions, data inconsistencies, or deadlocks that may arise when multiple processes or threads access shared resources simultaneously.

The key requirements of synchronization mechanisms include:

  • Mutual Exclusion:
    The synchronization mechanism must enforce mutual exclusion, allowing only one process or thread to access a shared resource or enter a critical section at any given time. This prevents conflicts and ensures consistency during concurrent access.

  • Progress:
    The synchronization mechanism should allow processes or threads to make progress by ensuring that at least one process/thread can enter the critical section when it desires to do so. It avoids situations where all processes/threads are blocked indefinitely, leading to a deadlock.

  • Bounded Waiting:
    The synchronization mechanism needs to guarantee that a process or thread waiting to enter a critical section will eventually be granted access. This prevents starvation, ensuring that a process or thread doesn't wait indefinitely to access a shared resource.


Ways to Ensure Proper Synchronization

  • Locks/Mutexes:
    These are like special keys that only one process or thread can have at a time. It ensures that only one of them can use a shared resource or critical section.It's like having a key to a room that only one person can use at a time, making sure they have it to themselves.

  • Semaphores:
    Think of semaphores like traffic signals for processes or threads. They control access to shared resources. They can be implemented as binary semaphores (mutexes) or counting semaphores. Counting semaphores allow a specified number of processes or threads to access a shared resource simultaneously. Semaphores provide mechanisms for mutual exclusion, signaling, and coordination.

  • Read-Write Locks:
    Imagine a situation where some people want to read a book (multiple readers), and some want to write in it (writers). Read-write locks help by letting many people read at the same time, but only one person can write, ensuring things stay organized and don't get messed up. It's like allowing a group to read a book together while making sure only one person writes in it at a time.


Deadlocks

A deadlock is a situation where a set of processes is blocked because each process is holding a resource and waiting for another resource acquired by some other process. Deadlocks can occur if the following four conditions hold simultaneously (Necessary Conditions):

  • Mutual Exclusion:
    One or more resources are non-sharable, meaning only one process can use them at a time.

  • Hold and Wait:
    A process is holding at least one resource and waiting for additional resources.

  • No Preemption:
    A resource cannot be taken from a process unless the process willingly releases it.

  • Circular Wait:
    A set of processes is waiting for each other in a circular form.


Methods for Handling Deadlocks

There are three ways to handle deadlocks:

  • Deadlock Prevention or Avoidance:
    The idea is to prevent the system from entering a deadlock state in the first place.

  • Deadlock Detection and Recovery:
    Allow the deadlock to occur, then use preemption to handle it once it has occurred.

  • Ignore the Problem Altogether:
    If deadlocks are very rare, let them happen and reboot the system. This approach is taken by both Windows and UNIX.

  • Banker's Algorithm:
    The Banker's algorithm is used to avoid deadlock and is one of the deadlock-avoidance methods.It is named after the banking system, where a bank never allocates available cash in a way that it can no longer satisfy the requirements of all its customers.