Question: The following program does not always lead to deadlock: / * thread _ one runs in this function void * do _ work _ one

The following program does not always lead to deadlock:
/* thread_one runs in this function
void *do_work_one(void *param){
pthread_mutex_lock(&first_mutex);
pthread_mutex_lock(&second_mutex);
/* do some work */
pthread_mutex_unlock(&second_mutex);
pthread_mutex_unlock(&first_mutex);
}
/* thread_two runs in this function */
void *do_work_two(void *param){
pthread_mutex_lock(&second_mutex);
pthread_mutex_lock(&first_mutex);
/* do some work */
pthread_mutex_lock(&first_mutex);
pthread_mutex_lock(&second_mutex);
pthread_exit();
}
In which scenario would deadlock occur?
The following program does not always lead to deadlock:
/* thread_one runs in this function
void *do_work_one(void *param){
pthread_mutex_lock(&first_mutex);
pthread_mutex_lock(&second_mutex);
/* do some work */
pthread_mutex_unlock(&second_mutex);
pthread_mutex_unlock(&first_mutex);
}
/* thread_two runs in this function */
void *do_work_two(void *param){
pthread_mutex_lock(&second_mutex);
pthread_mutex_lock(&first_mutex);
/* do some work */
pthread_mutex_lock(&first_mutex);
pthread_mutex_lock(&second_mutex);
pthread_exit();
}
In which scenario would deadlock occur?
If thread_one acquires both locks before thread_two.
If thread_two acquire both locks before thread_one.
If one thread is able to acquire only one lock before the other thread acquires the second lock.
All of the above

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 Databases Questions!