Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I cause a deadlock in the following program: #include<sys/types.h> #inclu

ID: 3692713 • Letter: H

Question

How do I cause a deadlock in the following program:

#include<sys/types.h>

#include<sys/wait.h>

#include<sys/stat.h>

#include<unistd.h>

#include<iostream>

#include<string.h>

#include<pthread.h>

#include<mutex>

using namespace std;

void *philosopher(void *param);

const int NUM = 5;

std::mutex chopstick[NUM];

int main(){
pthread_t tid[NUM];
  
pthread_attr_t attr;
  
pthread_attr_init(&attr);
  
for(int i=0; i < NUM; i++){
  
int *num = new int;
  
*num = i;
  
pthread_create(&tid[i], &attr, philosopher, (void *)num);
  
}
  
while(1);
  
//pthread_join(tid1, NULL);
  
return 0;
  
}

void *philosopher(void *param){
  
int i = *((int *)param);
  
cout << "Registred philosopher: " << i << endl;
  
while(1){
  
cout << "Philosopher " << i << " is thinking ";
  
sleep(rand()%3);
  
cout << "Philosopher " << i << " is hungry ";
  
cout << "Philosopher " << i << " is waiting on another chopstick ";
  
sleep(rand()%7);
  
chopstick[i].unlock();
  
chopstick[(i+1)%NUM].unlock();
  
}
}
  

Explanation / Answer

You can use double Mutex to create a deadlock. I suggest the threading with odd and even mutex which will make you to create a dead lock. Please see the example i have used and make the changes as per your requirement. This should work.

The code sample is rather a concocted one. The "std::cout" resource is protected with two mutex locks with different order: either mutex1->mutex2 or mutex2->mutex1. With this setup, sometimes it works fine but sometimes we have a deadlock. In our case, the deadlock happens when two threads are waiting for a mutex owned by the other.

One of the most common ways of avoiding a deadlock is to always lock the two mutexes in the same order. If we always lock mutex Abefore mutex B, then we'll never have a deadlock. Sometimes this is straightforward, because the mutexes are serving different purposes, but other times it's not so simple, such as when the mutexes are each protecting a separate instance of the same class.

As an example, let's think about an operation that exchanges data between two instances of the same class. In order to ensure that the data is exchanged correctly, without being affected by concurrent modifications, the mutexes on both instances must be locked. However, if a fixed order is chosen such as, the mutex for the instance supplied as the first parameter, then the mutex for the instance supplied as the second parameter. This can backfire, however, all it takes is for two threads to try to exchange data between the same two instances with the parameters swapped, and we have deadlock.

In the above code :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote