Two processes A and B are given, each performing two operations. Let's arrange the…
- User
Two processes A and B are given, each performing two operations. Let's arrange the operations of thread A into slots A1 and A2; and the operations of thread B into slots B1 and B2. We require that among all operations A1, A2, B1, B2, they will indeed 'be executed in order', each of the threads A and B will perform its first operation before the other thread can continue to the second operation. That is, we require that A1 will be executed before B2 and B1 will be executed before A2. The following basic solutions are based on counters for this purpose. For each solution, check whether the solution is correct or not. Solution A: sem A1Done = 0; sem B1Done = 0; //Thread A //Thread B A1 B1 down(B1Done) down(A1Done) up(A1Done) up(B1Done) A2 B2 Solution B: sem A1Done = 0; sem B1Done = 0; //Thread A //Thread B A1 B1 down(B1Done) up(B1Done) up(A1Done) down(A1Done) A2 B2 a. Solution A is correct, Solution B is correct. b. Solution A is correct, Solution B is incorrect. c. Solution A is incorrect, Solution B is correct. d. Solution A is incorrect, Solution B is incorrect.
- Assistant
To determine whether each of the solutions is correct, we need to test them against the requirements:
- A1 must occur before B2.
- B1 must occur before A2.
Solution A:
// Semaphores initialized to 0 sem A1Done = 0; sem B1Done = 0; // Thread A // Thread B A1 B1 down(B1Done) down(A1Done) up(A1Done) up(B1Done) A2 B2Let's simulate Solution A:
- Thread A executes
A1and attempts . Since is 0 and hasn't been upped by Thread B yet, Thread A .