2.
Distributed Approach ( Probe Based )
Algorithm Steps
Procedure Send_Probe(Initiator, Current_Process, Num_Processes)
1. If (Current_Process == Initiator):
a. Return True (Cycle Detected → Deadlock Found)
2. If (Current_Process is already visited):
a. Return False (No cycle detected)
3. Mark Current_Process as visited.
4. For each process P in the system:
a. If (WaitForGraph[Current_Process][P] == 1) (i.e., a dependency exists):
i. Recursively call Send_Probe(Initiator, P, Num_Processes).
ii. If recursion returns True, propagate True (Deadlock detected).
5. If no cycle is found, return False.
End Procedure
Procedure Detect_Deadlock(Num_Processes)
1. For each process P in the system:
a. Reset the `visited[]` array for a new check.
b. If (Probe has not been sent from P):
i. Call Send_Probe(P, P, Num_Processes).
ii. If it returns True:
- Return True (Deadlock detected).
iii. Mark probe as sent for P.
2. Return False (No deadlock found).
End Procedure
C Program
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 10
int waitForGraph[MAX_PROCESSES][MAX_PROCESSES];
bool visited[MAX_PROCESSES];
bool probeSent[MAX_PROCESSES];
// Function to send a probe
bool sendProbe(int initiator, int current, int numProcesses) {
if (current == initiator) {
return true; // Cycle detected
if (visited[current]) {
return false; // Already processed
visited[current] = true;
for (int i = 0; i < numProcesses; i++) {
if (waitForGraph[current][i] == 1) {
if (sendProbe(initiator, i, numProcesses)) {
return true;
}
}
return false;
// Function to detect deadlock using the probe-based approach
bool detectDeadlock(int numProcesses) {
for (int i = 0; i < numProcesses; i++) {
// Reset visited array for each process
for (int j = 0; j < numProcesses; j++) {
visited[j] = false;
if (!probeSent[i]) {
if (sendProbe(i, i, numProcesses)) {
return true; // Deadlock detected
probeSent[i] = true; // Mark the probe as sent
return false;
int main() {
int numProcesses;
printf("Enter the number of processes: ");
scanf("%d", &numProcesses);
printf("Enter the Wait-For Graph as an adjacency matrix:\n");
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numProcesses; j++) {
scanf("%d", &waitForGraph[i][j]);
if (detectDeadlock(numProcesses)) {
printf("Deadlock detected!\n");
} else {
printf("No deadlock detected.\n");
return 0;