0% found this document useful (0 votes)
12 views3 pages

DC Exp 8

The document discusses deadlock management in distributed systems, explaining its definition, examples, and techniques such as prevention, avoidance, and detection. It includes a program code that simulates deadlock detection and resolution using a wait-for graph and threading. The conclusion emphasizes the importance of effective deadlock management for system reliability and minimizing downtime.

Uploaded by

abhijaysingh66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

DC Exp 8

The document discusses deadlock management in distributed systems, explaining its definition, examples, and techniques such as prevention, avoidance, and detection. It includes a program code that simulates deadlock detection and resolution using a wait-for graph and threading. The conclusion emphasizes the importance of effective deadlock management for system reliability and minimizing downtime.

Uploaded by

abhijaysingh66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment No-8

Aim: Implement a Deadlock Management in Distributed Systems


Theory :
In distributed systems, deadlock occurs when a group of processes is waiting for resources held by
each other in such a way that no process can proceed, resulting in a system-wide halt. Deadlock
management in distributed systems involves detection, prevention, and recovery mechanisms.
Here's a detailed explanation:

1. Deadlock in Distributed Systems


 Definition: A deadlock occurs when multiple processes are blocked forever because each
process is waiting for a resource that another process holds.
 Example:
 Process A holds Resource X and requests Resource Y.
 Process B holds Resource Y and requests Resource X.
 Both processes cannot proceed, leading to a deadlock.

2. Deadlock Management Techniques


a) Deadlock Prevention
 Prevent deadlocks by avoiding at least one of the four necessary conditions:
1. Mutual Exclusion: Resources are shared instead of being exclusively assigned.
2. Hold and Wait: A process must request all its resources simultaneously.
3. No Preemption: Allow preemption of resources.
4. Circular Wait: Impose a total ordering on resource requests to avoid cycles.
b) Deadlock Avoidance
 Use algorithms (e.g., Banker’s Algorithm) to dynamically check resource allocation and
ensure that no unsafe state is reached.
c) Deadlock Detection and Recovery
 Detection: Use a Wait-For Graph (WFG) to identify cycles.
 WFG is a directed graph where:
 Nodes represent processes.
 Edges indicate which process is waiting for resources held by another.
 A cycle in the graph indicates a deadlock.
 Recovery: Resolve deadlocks by:
 Terminating one or more processes involved in the cycle.
 Releasing the resources held by terminated processes.

Program Code :
import threading
import time
import random
from collections import defaultdict
# Number of processes and resources
N=5
MAX_ITERATIONS = 3 # Each process will try to access resources up to this limit

# Shared resources (simulating distributed environment)


resources = [0] * N # 0 indicates free, non-zero indicates allocated
resource_lock = threading.Lock()

# Wait-for graph
wait_for_graph = defaultdict(set)
graph_lock = threading.Lock()

# Function to detect cycles in the wait-for graph (deadlock detection)


def detect_deadlock():
def has_cycle(node, visited, stack):
visited.add(node)
stack.add(node)

for neighbor in wait_for_graph[node]:


if neighbor not in visited:
if has_cycle(neighbor, visited, stack):
return True
elif neighbor in stack:
return True

stack.remove(node)
return False

visited = set()
for process in range(N):
if process not in visited:
if has_cycle(process, visited, set()):
return True
return False

# Function to handle resource requests and releases


def process_function(process_id):
for _ in range(MAX_ITERATIONS): # Each process attempts a limited number of resource
accesses
resource_id = random.randint(0, N - 1) # Randomly choose a resource to request

# Request resource
with resource_lock:
if resources[resource_id] == 0:
# Allocate resource if available
resources[resource_id] = process_id
print(f"Process {process_id} allocated resource {resource_id}")
time.sleep(random.uniform(0.5, 1)) # Simulate work
# Release resource
resources[resource_id] = 0
print(f"Process {process_id} released resource {resource_id}")
else:
# Add process to wait-for graph
with graph_lock:
wait_for_graph[process_id].add(resources[resource_id])

# Check for deadlock


with graph_lock:
if detect_deadlock():
print(f"Deadlock detected! Process {process_id} resolved the deadlock.")
# Resolve deadlock (terminate one process in the cycle)
with resource_lock:
resources[resource_id] = 0 # Forcefully release the resource
break # Terminate the process
# Simulate some delay before next request
time.sleep(random.uniform(0.1, 0.5))
# Create and start threads for each process
threads = []
for i in range(N):
t = threading.Thread(target=process_function, args=(i,))
threads.append(t)
t.start()

# Wait for all threads to finish


for t in threads:
t.join()
print("Deadlock management simulation complete.")
Output :

Conclusion : Effective deadlock management ensures smooth operation in distributed systems by


detecting and resolving resource conflicts dynamically. It enhances system reliability and minimizes
downtime, making it crucial for real-world applications.

You might also like