Program to create Deadlock Using C in Linux

Deadlock in operating system is a situation which occurs when a process or thread enters a waiting state because a resource requested is being held by another waiting process, which in turn is waiting for another resource held by another waiting process. In a deadlock state a process is unable to change its state(waiting) indefinitely because the resources requested by it are being used by another waiting process.

Setup

Deadlock Creation Using C in Linux

To simulate deadlock in the system we will create the above shown situation.

P1 and P2 will be represented by two thread one and two.
The two resources R1 and R2 will be represented by the two lock variables first_mutex and second_mutex
First thread one will acquire lock first_mutex and then thread two will acquire lock second_mutex. Hence, both the threads have acquired one resource each. Next, thread one will try to acquire lock second_mutex while the second thread, thread two will try to acquire lock first_mutex. Since the resources are already occupied by the other thread, both the threads will get into a deadlock.

Note: You must know how to create Threads to understand this program

Program to create Deadlock Using C in Linux using Mutex Locks and threads

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
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\n");
}
void *function1( ) {
pthread_mutex_lock(&first_mutex); // to acquire the resource/mutex lock
printf("Thread ONE acquired first_mutex\n");
sleep(1);
pthread_mutex_lock(&second_mutex);
printf("Thread ONE acquired second_mutex\n");
pthread_mutex_unlock(&second_mutex); // to release the resource
printf("Thread ONE released second_mutex\n");
pthread_mutex_unlock(&first_mutex);
printf("Thread ONE released first_mutex\n");

}

void *function2( ) {
pthread_mutex_lock(&second_mutex);
printf("Thread TWO acquired second_mutex\n");
sleep(1);
pthread_mutex_lock(&first_mutex);
printf("Thread TWO acquired first_mutex\n");
pthread_mutex_unlock(&first_mutex);
printf("Thread TWO released first_mutex\n");
pthread_mutex_unlock(&second_mutex);
printf("Thread TWO released second_mutex\n");

}

Practice Programs on Program to create Deadlock Using C in Linux

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.

Viva Questions on Program to create Deadlock Using C in Linux

Q1. What is deadlock?
Q2. What is the minimum number of threads/process required for deadlock to occur?
Q3. What is the significance of pthread_mutex_lock() function?
Q4. Why the sleep() function is used in the program?

Video Link

Leave a Reply