Question: Write a C program to simulate thread scheduling that ensures deadlock avoidance using a simplified resource allocation system. The goal is to avoid deadlock by

Write a C program to simulate thread scheduling that ensures deadlock avoidance using a simplified resource allocation system. The goal is to avoid deadlock by controlling the order in which threads acquire locks, based on a predefined global knowledge of resources.
Design:
You should create 3 threads (T1, T2, T3), and each thread needs to acquire 2 locks (Lock A and Lock B) in a specific order.
The threads should not be able to acquire locks in a fashion such that the program deadlocks.
The program should ensure deadlock-free execution by scheduling the acquisition of locks based on the given deadlock avoidance algorithm.
Implement deadlock avoidance using Resource Instance Ordering as follows:
Enforce a strict lock acquisition order (e.g., Lock A must always be acquired before Lock B).
1. Input:
None
2. Output:
Print messages showing the order of lock acquisition and thread waiting (e.g.,"T1 acquired Lock A","T2 waiting for Lock A").
The program should terminate after each thread has acquired each lock 3 times.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!