1. Homepage
  2. Exam
  3. [2021] Swansea - CS210 Concurrency - Final Exam - Q3 Deadlock

[2021] Swansea - CS210 Concurrency - Final Exam - Q3 Deadlock

This question has been solved
Engage in a Conversation

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] CourseNana.COM

  CourseNana.COM

(b) Below is an implementation for the Tools class in this context that may end up in deadlock. Now CourseNana.COM

answer the following questions: CourseNana.COM

  CourseNana.COM

i. Fix the code within the acquire method in such a way that a deadlock will not occur. [3 marks] CourseNana.COM

ii. Describe which one of the Co man conditions your solution broke. [2 marks] CourseNana.COM

  CourseNana.COM

1 import java . util . concurrent . Semaphore ; CourseNana.COM

2 public class Tools { CourseNana.COM

3 private Semaphore chisel = new Semaphore (1) ; CourseNana.COM

4 private Semaphore hammer = new Semaphore (1) ; CourseNana.COM

5 public void acquire (){ CourseNana.COM

6 double r = Math . random (); CourseNana.COM

7 try { CourseNana.COM

8 if (r >=0.5) { CourseNana.COM

9 chisel . acquire (); CourseNana.COM

10 hammer . acquire (); CourseNana.COM

11 } else { CourseNana.COM

12 hammer . acquire (); CourseNana.COM

13 chisel . acquire (); CourseNana.COM

14 } CourseNana.COM

15 } catch ( Exception e){ CourseNana.COM

16 System . out. println (" Something went wrong ."); CourseNana.COM

17 } CourseNana.COM

18 } CourseNana.COM

19 public void release (){ CourseNana.COM

20 chisel . release (); CourseNana.COM

21 hammer . release (); CourseNana.COM

22 } CourseNana.COM

23 } CourseNana.COM

CourseNana.COM

Get the Solution to This Question

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Swansea University代写,CS210代写,Concurrency代写,Swansea University代编,CS210代编,Concurrency代编,Swansea University代考,CS210代考,Concurrency代考,Swansea Universityhelp,CS210help,Concurrencyhelp,Swansea University作业代写,CS210作业代写,Concurrency作业代写,Swansea University编程代写,CS210编程代写,Concurrency编程代写,Swansea Universityprogramming help,CS210programming help,Concurrencyprogramming help,Swansea Universityassignment help,CS210assignment help,Concurrencyassignment help,Swansea Universitysolution,CS210solution,Concurrencysolution,