1.Let A and B be two semaphores initialized to 1, whether this following code ha
ID: 3723186 • Letter: 1
Question
1.Let A and B be two semaphores initialized to 1, whether this following code has problem, if so, which problem and explain your answer
P0 P1
wait(A); wait(B);
wait(B); wait(A);
signal(A); signal(B);
signal(B); signal(A);
2.Philosophers spend their lives thinking and eating . These Philosophers don’t interact with their neighbors, occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl and they need both to eat, then release both when done
In the case of 5 philosophers, Semaphore chopstick [5] initialized to 1
Does the following codes have problem? If so, which kind(s) problem(s)? How to deal with the problem(s)?
do {
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
Explanation / Answer
Solution:
The first question is done as per Chegg guidelines, please repost others.
So we have this code
P0 P1
wait(A); wait(B);
wait(B); wait(A);
signal(A); signal(B);
signal(B); signal(A);
A and B are set to 1.
In the given code a deadlock is possible to occur, let me tell you how
A = 1, B = 1.
Now P1 executes wait(A); and preempts. Now A = 0
now P2 executes wait(B) and now B = 0
Now there is no way any of the processes can proceed further.
This is the problem with the above code.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.