Page 1 of 1

13. How could you change the program in Figure 3 to avoid a deadlock? [2 points] 0) Semaphore semi, sem2, sem3; 1) 2 int

Posted: Fri May 20, 2022 10:29 am
by answerhappygod
13 How Could You Change The Program In Figure 3 To Avoid A Deadlock 2 Points 0 Semaphore Semi Sem2 Sem3 1 2 Int 1
13 How Could You Change The Program In Figure 3 To Avoid A Deadlock 2 Points 0 Semaphore Semi Sem2 Sem3 1 2 Int 1 (75.45 KiB) Viewed 28 times
13. How could you change the program in Figure 3 to avoid a deadlock? [2 points] 0) Semaphore semi, sem2, sem3; 1) 2 int main() { initializeSempahore( sem1, 1); // Used as a binary semaphore 14) initializeSempahore (sem2, 1); // Used as a binary semaphore 5) initializeSempahore (sem3, 1); // Used as a binary semaphore I7) // Create and start threads 18 thread t1(threadRunnerA); 9) thread t2(threadRunnerB); 10) // ... Other code 11) return 0; 12) } 13 14) void threadRunnerA() { // Function executing as a thread (15) wait(sem1); 16) wait (sem2); 17) 18) // Do some work ... 19) signal (sem 1); 120) wait (sem3); 21) // Do some more work ... 22) 23) signal(sem2); (24) signal( sem3); 25) } 126) 27) void threadRunnerB() { // Function executing as a thread 28) wait(sem1); 29) wait( sem3); 30) 31) // Do some work 32) signal(sem1); 133) wait (sem2); 34) 35) // Do some more work ... signal(sem2); 37) signal(sem3); 38) } Figure 3: Program listing 3 36)