In Lock B, suppose ticket = 5 and turn = 3. What is the lock state?
Blog
Consider a system with three threads and three locks (A, B,…
Consider a system with three threads and three locks (A, B, C) (assume any locks held are eventually freed in each thread, after these operations): Thread 1: lock(A); lock(B); lock(C) Thread 2: lock(B); lock(C); unlock(B); lock(A) Thread 3: lock(C); lock(A) Which pair(s) of threads can deadlock with each other?
Consider a bounded buffer with one producer and two consumer…
Consider a bounded buffer with one producer and two consumers using a single condition variable and while loops: void *producer(void *arg) { mutex_lock(&m); while (count == MAX) cond_wait(&cv, &m); buffer_add(item); count++; cond_signal(&cv); mutex_unlock(&m); } void *consumer(void *arg) { mutex_lock(&m); while (count == 0) cond_wait(&cv, &m); item = buffer_get(); count–; cond_signal(&cv); mutex_unlock(&m); } What can go wrong with this code?
A programmer runs their multithreaded program 1000 times and…
A programmer runs their multithreaded program 1000 times and never observes a bug. What can they conclude?
Deadlock and Race Detection: True/False
Deadlock and Race Detection: True/False
Suppose all three threads are modified to use mutex_trylock…
Suppose all three threads are modified to use mutex_trylock in a loop: each thread attempts to acquire both locks, and if the second trylock fails, it releases the first lock and retries both from the beginning (with no delay). What problem can arise?
Consider a concurrent linked list with hand-over-hand (lock…
Consider a concurrent linked list with hand-over-hand (lock coupling) locking: each node has its own lock, and a thread traversing the list locks the next node before unlocking the current one. Compared to using a single coarse-grained lock for the entire list, what is the main advantage of this approach?
In a system where every thread holds at most one lock at a t…
In a system where every thread holds at most one lock at a time, deadlock is impossible.
Consider the following program: int x = 0; void *worker(void…
Consider the following program: int x = 0; void *worker(void *arg) { x = x + 1; return NULL; } int main() { pthread_t t1, t2; pthread_create(&t1, NULL, worker, NULL); pthread_create(&t2, NULL, worker, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); printf(“%d\n”, x); return 0; } Which of the following best describes the behavior of this program?
Does Lock A provide mutual exclusion?
Does Lock A provide mutual exclusion?