Write in each answer sheet your name, signature, student id, course name and page number.
- [11 p] Critical section.
- Give a code level example on a situation, where critical section is composed of two distinct code segments. Explain.
- Assume, that (i) critical section takes 10-30 machine instructions, (ii) critical section execution takes less than 0.001% of program execution time, (iii) multiple threads are executed concurrently in 8-core system, and (iv) threads have access to shared memory. How would you protect the critical section in this case? Explain.
- Assume now, that requirement (iv) in previous case (b) is modified so that the threads do not use shared memory even though they execute in the same system. How would you protect the critical section in this case? Explain.
- [11 p] Deadlocks. What is the deadlock problem? Which four conditions must be in effect for the deadlock to occur? Explain the main principles of Dijktra's deadlock detection algorithm. What should one do if a deadlock is detected? How can one prevent deadlocks in advance?
- [11 p] Describe Producer Consumer problem. There are many producers and consumers. Buffer has limited size. Give the solution to this problem using semaphores. Explain, why your solution is correct.
- [11 p] Bees and bear. Friendly bees (N bees) are feeding a trapped bear by collecting honey for it. The life of the trapped bear is just eating and sleeping.
The bees collect honey and carry it to a pot, filling the pot one portion each bee at a time. When the pot is full (H portions of honey), the last bee to deposit honey wakes up the bear to eat.
The bees pause filling the pot until the pot is empty again. Once the bear is finished eating the pot empty, it allows the bees to deposit honey again and goes to sleep. Below is a rough Ada Protected Objects solution where the bees fill up the pot one bee at a time.
Modify the example so that at most 50 bees can be filling up the pot concurrently (outside the protected object pot).
Present he code for bees (N processes), the bear and the protected object pot.