3. (a) Two sculptors are building two independent pieces. Due to budget cuts, they were only given a chisel and a hammer. They must now share these tools. Naturally, a sculptor needs both tools to build, and they must wait for the tools to become available. Furthermore, a tool can only be held by one sculptor at a time. In the light of the necessary and sufficient (or Coffman) conditions, discuss how the above scenario may lead to a deadlock. [4 marks]
(b) Below is an implementation for the Tools class in this context that may end up in deadlock. Now
answer the following questions:
i. Fix the code within the acquire method in such a way that a deadlock will not occur. [3 marks]
ii. Describe which one of the Co man conditions your solution broke. [2 marks]
1 import java . util . concurrent . Semaphore ;
2 public class Tools {
3 private Semaphore chisel = new Semaphore (1) ;
4 private Semaphore hammer = new Semaphore (1) ;
5 public void acquire (){
6 double r = Math . random ();
7 try {
8 if (r >=0.5) {
9 chisel . acquire ();
10 hammer . acquire ();
11 } else {
12 hammer . acquire ();
13 chisel . acquire ();
14 }
15 } catch ( Exception e){
16 System . out. println (" Something went wrong .");
17 }
18 }
19 public void release (){
20 chisel . release ();
21 hammer . release ();
22 }
23 }