Process Synchronization Two process Solution (Algo-2) Deadlock Concept, Flag concept.

Process Synchronization Two process Solution (Algo-2) Deadlock Concept , Flag concept.



play-rounded-fill play-rounded-outline play-sharp-fill play-sharp-outline
pause-sharp-outline pause-sharp-fill pause-rounded-outline pause-rounded-fill
00:00

Process Synchronization – Two Process Solution (Algo-2) | Deadlock & Flag Concept

 Introduction to Process Synchronization

Process Synchronization is a mechanism to ensure that multiple processes can execute in a way that avoids inconsistencies and conflicts when accessing shared resources. One of the key challenges in synchronization is deadlock.

 Two-Process Solution (Algorithm-2)

The Two-Process Solution for synchronization ensures mutual exclusion, progress, and bounded waiting. It typically uses flags and turn variables.

 Algorithm-2 (Flag Concept Based Approach)

This method relies on two flags to indicate whether a process wants to enter the critical section.

 Assumptions:

  • There are two processes, P1 and P2.
  • Each process sets its flag to indicate entry to the critical section.
  • The process checks the other process’s flag before entering.

 Algorithm (Using Flags)

// Shared Variables
boolean flag[2] = {false, false}; // Flags for P1 and P2

Process P1:
while (true) {
flag[0] = true; // P1 wants to enter the critical section
while (flag[1]); // Wait if P2 is in the critical section

// Critical Section (CS)

flag[0] = false; // Exit CS
// Remainder Section
}

Process P2:
while (true) {
flag[1] = true; // P2 wants to enter the critical section
while (flag[0]); // Wait if P1 is in the critical section

// Critical Section (CS)

flag[1] = false; // Exit CS
// Remainder Section
}

 Explanation:

  • Each process sets its flag (flag[i] = true) before entering the Critical Section (CS).
  • It checks whether the other process’s flag is true. If yes, it waits.
  • If the other process is not in the CS (flag[j] = false), it enters the CS.
  • After execution, the process resets its flag (flag[i] = false) and enters the Remainder Section.

 Deadlock Concept

A deadlock occurs when two or more processes wait indefinitely for resources held by each other.

 Can Deadlock Occur in This Algorithm?

Yes! This algorithm can lead to deadlock in the following situation:

  1. Both P1 and P2 set their flags at the same time (flag[0] = true, flag[1] = true).
  2. Both enter their respective while loops (while (flag[1]); and while (flag[0]);).
  3. Now, both are waiting for each other to release the flag, leading to a deadlock.

 Avoiding Deadlock: Peterson’s Solution

Peterson’s Algorithm improves the Two-Process Solution by introducing a turn variable along with flags.

boolean flag[2] = {false, false};
int turn = 0; // Indicates which process gets priority

Process P1:
while (true) {
flag[0] = true;
turn = 1;
while (flag[1] && turn == 1); // Wait if P2 has priority

// Critical Section
flag[0] = false; // Exit CS
}

Process P2:
while (true) {
flag[1] = true;
turn = 0;
while (flag[0] && turn == 0); // Wait if P1 has priority

// Critical Section
flag[1] = false; // Exit CS
}

 Why is Peterson’s Solution Deadlock-Free?

  • If both processes want to enter, the turn variable ensures only one gets priority.
  • This prevents both from waiting indefinitely on each other.

Conclusion

  • Algorithm-2 (Flag Concept) can cause deadlock if both processes set their flags simultaneously.
  • Peterson’s Algorithm eliminates deadlock using a turn variable.
  • Process Synchronization is essential in operating systems, multi-threading, and concurrent computing.

 Would you like more details on Deadlock Prevention & Avoidance?

Process Synchronization Two process Solution (Algo-2) Deadlock Concept, Flag concept.

Chapter 6: Synchronization



Leave a Reply

Your email address will not be published. Required fields are marked *

error: