10.07.2015 Views

Nachos Assignment #1: Build a thread system Tom Anderson ...

Nachos Assignment #1: Build a thread system Tom Anderson ...

Nachos Assignment #1: Build a thread system Tom Anderson ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

7. You have been hired by Caltrans to synchronize trac over a narrow lightdutybridge on a public highway. Trac may only cross the bridge in onedirection at a time, and if there are ever more than 3 vehicles on the bridgeat one time, it will collapse under their weight. In this <strong>system</strong>, each caris represented by one <strong>thread</strong>, which executes the procedure OneVehiclewhen it arrives at the bridge:OneVehicle(int direc){ArriveBridge(direc)CrossBridge(direc)ExitBridge(direc)}In the code above, direc is either 0 or 1 it gives the direction in which thevehicle will cross the bridge.(a) Write the procedures ArriveBridge and ExitBridge (the CrossBridgeprocedure should just print out a debug message), using locks andcondition variables. ArriveBridge must not return until it safe forthe car to cross the bridge in the given direction (it must guaranteethat there will be no head-on collisions or bridge collapses). Exit-Bridge is called to indicate that the caller has nished crossing thebridge ExitBridge should take steps to let additional cars cross thebridge. This is a lightly-travelled rural bridge, so you do not need toguarantee fairness or freedom from starvation.(b) In your solution, if a car arrives while trac is currently moving in itsdirection of travel across the bridge, but there is another car alreadywaiting to cross in the opposite direction, will the new arrival crossbefore the car waiting on the other side, after the car on the otherside, or is it impossible to say? Explain briey.8. Implement (non-preemptive) priority scheduling. Modify the <strong>thread</strong> schedulerto always return the highest priority <strong>thread</strong>. (You will need to createa new constructor for Thread to take another parameter { the prioritylevel of the <strong>thread</strong> leave the old constructor alone since we'll need it forbackward compatibility.) You may assume that there are a xed, smallnumber of priority levels { for this assignment, you'll only need two levels.Can changing the relative priorities of the producers and consumer <strong>thread</strong>shave any aect on the output? For instance, what happens with twoproducers and one consumer, when one of the producers is higher prioritythan the other? What if the two producers are at the same priority, butthe consumer is at high priority?5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!