The code below could encounter what issue? Select the BEST answer. A. Hold and W
ID: 3705717 • Letter: T
Question
The code below could encounter what issue? Select the BEST answer.
A. Hold and Wait
B. Mutual Exclusion
C. Absolute Ordering
D. Deadlock
E. Livelock
----------------------------------------------------------------------------------------------------------------
void thread1(void* arg) {
struct queue* q1 = arg[0];
struct queue* q2 = arg[1];
while (true) {
int check = pthread_mutex_trylock(q1->mutex);
if (check == 0) {
check = pthread_mutex_trylock(q2->mutex);
if (check == 0) {
enqueue(q1, dequeue(q2));
} else {
pthread_mutex_unlock(q1->mutex);
}
}
}
}
void thread2(void* arg) {
struct queue* q1 = arg[0];
struct queue* q2 = arg[1];
while (true) {
int check = pthread_mutex_trylock(q2->mutex);
if (check == 0) {
check = pthread_mutex_trylock(q1->mutex);
if (check == 0) {
enqueue(q2, dequeue(q1));
} else {
pthread_mutex_unlock(q2->mutex);
}
}
}
}
Explanation / Answer
Solution:
The bounded wait is the problem here,
which will lead to livelock, means starvation.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)