Question: Deadlocks ( ) Deadlock in an operating system ( OS ) refers to a situation where a set of processes are blocked because each process

Deadlocks()
Deadlock in an operating system (OS) refers to a
situation where a set of processes are blocked
because each process is holding a resource and
waiting for another resource that is held by
another process. In this situation, none of the
processes can proceed, resulting in a standstill.
Consider two processes, P1 and P2, and two
resources, R1 and R2:
P1 holds R1 and is waiting for R2.
P2 holds R2 and is waiting for R1.
Both processes are blocked, waiting
indefinitely for each other to release the
resource they need, causing a deadlock.
12
Deadlocks()
Deadlock Conditions:
For a deadlock to occur, all of the following conditions must be true simultaneously:
1. Mutual Exclusion: A resource can be held by only one process at a time.
2. Hold and Wait: Processes holding resources can request new ones.
3. No Preemption: Resources cannot be preempted from processes holding them.
4. Circular Wait: A circular chain of processes exists, where each process is waiting for a resource held by the
next process.
13
Program to create Deadlock Using C in Linux using Mutex Locks and threads
#include
#include
#include
void *function1();
void *function2();
pthread_mutex_t first_mutex; //mutex lock
pthread_mutex_t second_mutex;
int main(){
pthread_mutex_init(&first_mutex,NULL); //initialize
the lock
pthread_mutex_init(&second_mutex,NULL);
pthread_t one, two;
pthread_create(&one, NULL, function1, NULL); //
create thread
pthread_create(&two, NULL, function2, NULL);
pthread_join(one, NULL);
pthread_join(two, NULL);
printf("Thread joined
");
}
14
void *function1(){
pthread_mutex_lock(&first_mutex); // to acquire the
resource/mutex lock
printf("Thread ONE acquired first_mutex
");
sleep(1);
pthread_mutex_lock(&second_mutex);
printf("Thread ONE acquired second_mutex
");
pthread_mutex_unlock(&second_mutex); // to release the
resource
printf("Thread ONE released second_mutex
");
pthread_mutex_unlock(&first_mutex);
printf("Thread ONE released first_mutex
");
}
void *function2(){
pthread_mutex_lock(&second_mutex);
printf("Thread TWO acquired second_mutex
");
sleep(1);
pthread_mutex_lock(&first_mutex);
printf("Thread TWO acquired first_mutex
");
pthread_mutex_unlock(&first_mutex);
printf("Thread TWO released first_mutex
");
pthread_mutex_unlock(&second_mutex);
printf("Thread TWO released second_mutex
");
}
Lab Exercise
Q1. Write a program to simulate deadlock between three threads.
Q2. Write a program to create 4 threads (thread1, thread2, thread3 and
thread4). Create a deadlock situation between thread2 and thread4.
Follow the concepts given and give me the codes for the above questions.
and also provide the output and explanation.

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!