You are on page 1of 2

Week 3

7 Exercises

1. Consider the Bakery Algorithm for 2 processes shown below

integer np ß 0, integer nq ß 0
p q
loop_forever loop_forever
p1: non-critical section p1: non-critical section
p2: np ß nq + 1 p2: nq ß np + 1
p3: await nq = 0 or np <= nq p3: await np = 0 or nq < np
p4: critical section p4: critical section
p5: np ß 0 p5: nq ß 0

i. Show that Mutual Exclusion holds for this algorithm;


ii. Show that the algorithm is free from deadlock;
iii. Show that the algorithm is free from starvation.

2. Why does it no longer matter that assignment is atomic in the test and set and swap
approaches?

3. Show how each of the following can be used to solve the critical section problem:
i. Test and Set;
ii. Swap.

4. What are the possible outputs of the following program:

Semaphore S ß 1, boolean b ß false


p q
p1: wait(S) p2: wait(S)
p2: B ß true p1: while not B
p3: signal(S) q3: print(“*”)
q4: signal(S)

5. Consider the following scenario. A swing bridge can be used by cars to cross the river. It
can also be raised to allow tall ships to pass underneath it. The bridge must not be raised
when cars are on it; and it must not be lowered whilst ships are passing underneath.
i. Write pseudocode to show how semaphores can be used to solve this problem.
ii. You will now implement this in Java. First create a Java class SwingBridge:
a) Add the following to the class Vehicle (given at the bottom of these exercises):
 A Semaphore that monitors whether the bridge is clear and allows one
vehicle to use (or pass under) the bridge at once.
 A boolean bridgeOpen, representing the state of the bridge, which is
initialised to false (meaning the bridge is down and can be used by road
users).
b) Create a class Car which extends Vehicle:
 It should have a Constructor that sets needsToBeOpen to false;
 It should be able to run as a thread;
 Create the method crossBridge() which:
◦ Closes the bridge if it is open;
◦ Prints a message that v.getName() is crossing the bridge;
◦ Sleeps for 100ms;
 Implement the run method for this thread so that it:
◦ Loops 5 times:
◦ Waits for the bridge to be clear;
◦ Executes crossBridge()
◦ Signals that the bridge is clear.
c) Create a class Ship which extends vehicle:
 It should have a Constructor that sets needsToBeOpen to true;
 It should be able to run as a thread;
 Create a method crossBridge() which:
◦ Opens the bridge if it is closed;
◦ Prints a message that v.getName()is sailing underneath the bridge;
◦ Sleeps for 150ms;
 Implement the run method for this thread so that it:
◦ Loops 3 times:
◦ Waits for the bridge to be clear;
◦ Executes crossBridge()
◦ Signals that the bridge is clear.
d) Write a main method that creates 3 ships, named ship1, ship2 and ship3. Creates
5 Cars named car1, car2,...car5. Runs all of these as threads.
 Test your code to make sure only one vehicle can use/pass underneath the
bridge at once.

6. Modify your code above so that 3 cars can use the bridge at once, but only one ship can go
under the bridge at once.
 Hint 1: you need only modify the car class;
 Hint 2: you will need a class variable to count the number of cars on the bridge and you
will need to use a semaphore to protect access/modification to it.

7. Merge sort splits an array into two parts, sorts each of these parts and then merges the
resulting sorted arrays. Implement merge sort in Java using threads and Semaphores.

Code for class Vehicle:

public abstract class Vehicle {

protected boolean needsBridgeOpen;


protected String name;

public Vehicle(String nameIn, boolean landBasedIn){


name = nameIn;
needsBridgeOpen = !landBasedIn;
}
}

You might also like