# Concurrent and Parallel Algorithms

Question 2

There is a public swimming pool with a limit of 4 swimmers in the pool at one time. (It is a very small pool!) The code for each swimmer is given below. It uses a semaphore poolSpace, to represent whether there is space left in the pool for more swimmers. S1, S2 and S3 are labels identifying the line of code. Swimmer code: S1: wait(poolSpace); S2: swim(); S3: signal(poolSpace);

(a) There are six people, A, B, C, D, E and F who want to swim in the pool. Each person is an instance of the Swimmer class. Demonstrate how the semaphore prevents more than four people being in the pool at the same time by giving an example execution trace. Show the value of poolSpace after each wait or signal step. Ensure that you state the initial value of poolSpace. If a wait operation executes, indicate whether the process succeeds or is placed in the queue. If a signal operation executes, indicate whether the value is changed or a sleeping process is woken up. Each line of your trace should have the following format: Statement executed including which Swimmer process (e.g. A.S1); poolSpace value (e.g. poolSpace=2); whether the wait succeeded or was placed in the queue/ whether the signal changed the semaphore value or a sleeping process woke up. (b) The swimmers have become more competitive. They want to keep track of the fastest time to swim a length of the pool. They decide to use the code below, which uses a shared variable fastest to record the fastest time (i.e. smallest value) so far.

Assume that when the method swim runs, it updates the Swimmer’s local int variable myTime to the time just taken for this current swim. The details of how it does this is irrelevant. Global shared variable: int fastest; Swimmer code: wait(poolSpace); swim(); if (fastest > myTime){ fastest = myTime; } signal(poolSpace); The code given has a problem. The swimmers have found that sometimes the time stored in fastest is not actually the quickest time so far. Explain, in words, how this situation could occur. Show a trace that demonstrates the problem occurring. Note that you are not being asked to solve the problem. You can select any arbitrary times for myTime for each swimmer as long as it demonstrates how the problem occurs.

#Concurrent #Parallel #Algorithms

Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!