Consider the pseudocode shown below. After initializing the semaphores, s1 and s
ID: 3548712 • Letter: C
Question
Consider the pseudocode shown below. After initializing the semaphores, s1 and s2, three threads are created runA(), runB(), and runC(), respectively.
Is it possible for the threads to deadlock? If so, explain briefly how deadlock may occur. If not, describe which condition required for deadlck is not satisified.
semaphore s1 = 1; semaphore s2 = 0; void *runA() { output 'W' ; sem_post(&s2;) ; sem_wait(&s1;) ; output 'D' ; sem_post(&s1;) ; output 'T' ; return; } void *runB() { sem_wait(&s2;) ; sem_wait(&s1;) ; output 'I' ; sem_post(&s2;) ; output 'C' ; output 'S' ; sem_post(&s1;) ; return } void *runC() { sem_wait(&s1;) ; output 'L' ; sem_wait(&s2;) ; output 'A' ; sem_post(&s1;) ; sem__post (&s2;) ; return; }Explanation / Answer
All threads start simultaneously.
step 1: tread 1: prints 'W'; Thread 2: waits for s2; Thread 3: sets s1 = 0
step 2: Thread1:sets s2 = 1; Thread 2: sets s2 = 0 ; Thread 3: prints 'L'
step 3: Thread 1: waits for s1; Thread 2: waits for s1 ; Thread 3: waits for s2 = 0.
This is a DEADLOCK.
Thread 2 and Thread 3 can deadlock when thread 2 and thread 3 starts simultaneously after complation of Thread1
step 1: thread 2 sets s2 to 0 and thread 3 sets s1 to 0.
step 2: thread 2 waits for s1 and thread 3 prints 'L'
step 3: thread 2 is still waiting for s1 and thread 3 is now waiting for s2. This is a deadlock
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.