Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
For instructions on how to work on the assignment on Mimir, please review materials in Lab 0. Do not
forget to submit your work before the deadline. A Makefile is provided. Run make sub to create a zip file
and submit it. You can also run make clean and then submit the assignment folder.
Problem 1. Bridge (100 points)
In a wonderland are there two islands connected by a bridge. The bridge spans from west to east. People
can drive across the bridge to get to the other island. The cars follow the traffic rules listed below.
1. At most max_crossing cars can be on the bridge at any time.
2. The traffic on the bridge cycles through two phases: westbound and eastbound phases. At any time,
cars on the bridge move in the same direction.
3. At most max_crossed cars (in the phase direction) can cross the bridge in a phase if a car is waiting
in the opposite direction,
4. If cars waiting on both ends of the bridge may get on the bridge, cars traveling in the phase direction
have precedence.
5. A car does not wait unless it cannot cross the bridge because of other rules.
Rule 3 allows cars traveling in both directions to have a chance to cross the bridge and Rules 4 and 5
maximize the traffic flow on the bridge. If there are a lot of cars waiting on both ends, we will see alternating
westbound and eastbound phases, in each of which max_crossed cars cross bridge.
The appendix on the next page uses some examples to explain the rules.
Tasks
In this problem we write a multi-threaded program bridge to simulate how cars cross the bridge, following
the above rules. Each car is a thread (thread_car()). Every car crosses the bridge n_trips times and does
not change its direction. For each trip, a car drives for a while and arrives at the bridge. A message is
printed. The car may pick a random number as the time to cross (drive on) the bridge. Then function
cross_bridge_eastbound() or cross_bridge_westbound() are called to perform the rest of steps. Our
tasks are to complete the two functions.
void cross_bridge_eastbound(car_t * car);
void cross_bridge_westbound(car_t * car);
In the functions, a car waits (if necessary), gets on the bridge, drives on the bridge, and finally leaves the
bridge. The functions use macros to report the car’s status and to simulate driving. The steps are listed in
function cross_bridge_single().
A structure bridge_t is defined in the starter code. It has sufficient fields to solve the problem.
In this problem, we do not enforce the ordering of cars. For example, car 2 arrives at the bridge before
car 4, but car 4 may get on the bridge before car 2 does. Car 2 gets on the bridge later than car 4, but may
leave the bridge earlier.
The program takes many optional command line arguments. See the help message for details.
$./bridge -h
Testing
An example output of running the program with default configurations is in file example-output.txt.
A Python script check-bridge.py helps to check the output of the program. You can use a pipeline, but
it may be a good idea to save the output in a file so you can examine the output manually. The script support
a few arguments that controls how the script prints information. Try the following example commands.