There is a deadlock in this situation, how do i resolve this?

Status
Not open for further replies.

yeonxhan_88

New Member
Joined
Jun 21, 2023
Messages
2
Reaction score
0
Credits
33
#include <stdio.h>
#include <pthread.h>

pthread_mutex_t lock1, lock2;

void* thread1(void* arg) {
// Acquire lock1
pthread_mutex_lock(&lock1);

// Simulate some work
printf("Thread 1 is trying to acquire the lock..\nTHERE IS A DEADLOCK SITUATION!!!\n");
sleep(1);

// Acquire lock2
pthread_mutex_lock(&lock2);

// Critical section
printf("Thread 1 is in the critical section.\n");

// Release lock2
pthread_mutex_unlock(&lock2);

// Release lock1
pthread_mutex_unlock(&lock1);

pthread_exit(NULL);
}

void* thread2(void* arg) {
// Acquire lock2
pthread_mutex_lock(&lock2);

// Simulate some work
printf("Thread 2 is trying to acquire the lock\n");
sleep(1);

// Acquire lock1
pthread_mutex_lock(&lock1);

// Critical section
printf("Thread 2 is in the critical section.\n");

// Release lock1
pthread_mutex_unlock(&lock1);

// Release lock2
pthread_mutex_unlock(&lock2);

pthread_exit(NULL);
}

int main() {
pthread_t tid1, tid2;

// Initialize locks
pthread_mutex_init(&lock1, NULL);
pthread_mutex_init(&lock2, NULL);

// Create thread 1
pthread_create(&tid1, NULL, thread1, NULL);

// Create thread 2
pthread_create(&tid2, NULL, thread2, NULL);

// Wait for threads to finish
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);

// Destroy locks
pthread_mutex_destroy(&lock1);
pthread_mutex_destroy(&lock2);

return 0;
}
 


The two threads acquire the locks in a different order.
Thread1 acquires lock1 before it tries to acquire lock2.
Thread2 acquires lock2 before it tries to acquire lock1.

If thread1 acquires lock1 and thread2 acquires lock2 while thread1 sleeps and has not yet acquired lock2, then each thread will be stuck, waiting on its second lock. That is the deadlock.

Can you figure out the solution on your own with this new information? Is this a homework assignment for school or a class? Did you ask your teacher for help?
 
I just realized that this is the same OP who demanded a homework solution in a previous thread:
https://www.linux.org/threads/producer-consumer-buffer.45610/
Tsk, tsk, tsk. Naughty, naughty.....

So long as folks come on fora, asking for help with homework assignments.....and well-meaning members continue to give advice & try to help them.....then these folks will keep on coming back for more.

Better by far to either

  • Just gently point them in the right direction without actually "helping" them, or
  • Simply don't answer

Not being funny, but I have seen TOO much of this in recent years.....treating fora as basically online help-desks, without attempting to put any effort of their own in at all. I also completely fail to see the sense in obtaining qualifications to secure a responsible position with whoever, if you don't understand the material that got you that qualification in the first place.

Are we gullible?


Mike. :confused:
 
Last edited:
Locking this thread - thanks Members.

Wizard
 
Status
Not open for further replies.


Follow Linux.org


Top