Answered step by step
Verified Expert Solution
Question
1 Approved Answer
13. How could you change the program in Figure 3 to avoid a deadlock? [2 points] 0) Semaphore sem1, sem2, sem3; 1) 2) int
13. How could you change the program in Figure 3 to avoid a deadlock? [2 points] 0) Semaphore sem1, sem2, sem3; 1) 2) int main() { initializeSempahore (sem1, 1); // Used as a binary semaphore initializeSempahore (sem2, 1); // Used as a binary semaphore initializeSempahore (sem3, 1); // Used as a binary semaphore 4) 5) 7) 9) 10) 11) 12) } // Create and start threads thread t1 (threadRunnerA); thread t2 (threadRunnerB); // ... Other code return 0; 13) 14) void threadRunnerA() { // Function executing as a thread 15) 16) 17) 18) 19) 120) 21) 22) 23) 24) 25) } 26) wait (sem1); wait (sem2); // Do some work signal (sem1); wait (sem3); // Do some more work... signal (sem2); signal (sem3); 27) void threadRunnerB () { // Function executing as a thread 28) wait (sem1); 29) wait (sem3); 30) 31) 32) signal (sem1); 33) wait (sem2); 34) 35) 36) 37) 38)} Figure 3: Program listing 3 // Do some work. III // Do some more work... signal (sem2); signal (sem3);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
To avoid a deadlock in the program shown we must ensure that semaphores are always acquired in the s...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started