Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Deadlock Using C in Linux

Uploaded by

Uuii
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Deadlock Using C in Linux

Uploaded by

Uuii
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

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");

You might also like