讲解:CSE 3100、Systems Programming、Python、PythonPython|

CSE 3100 Systems Programming Fall 2019Homework #8 Due: 11/26/2019For instructions on how to work on the assignment on Mimir, please review materials in Lab 0. Do notforget to submit your work before the deadline. A Makefile is provided. Run make sub to create a zip fileand 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. Peoplecan 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 waitingin the opposite direction,4. If cars waiting on both ends of the bridge may get on the bridge, cars traveling in the phase directionhave 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 5maximize the traffic flow on the bridge. If there are a lot of cars waiting on both ends, we will see alternatingwestbound 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.TasksIn this problem we write a multi-threaded program bridge to simulate how cars cross the bridge, followingthe above rules. Each car is a thread (thread_car()). Every car crosses the bridge n_trips times and doesnot change its direction. For each trip, a car drives for a while and arrives at the bridge. A message isprinted. The car may pick a random number as the time to cross (drive on) the bridge. Then functioncross_bridge_eastbound() or cross_bridge_westbound() are called to perform the rest of steps. Ourtasks 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 thebridge. The functions use macros to report the car’s status and to simulate driving. The steps are listed infunction 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 exampCSE 3100代做、代写Systems Programmile, car 2 arrives at the bridge beforecar 4, but car 4 may get on the bridge before car 2 does. Car 2 gets on the bridge later than car 4, but mayleave the bridge earlier.The program takes many optional command line arguments. See the help message for details.$./bridge -hTestingAn 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, butit may be a good idea to save the output in a file so you can examine the output manually. The script supporta few arguments that controls how the script prints information. Try the following example commands.1$python3 ./check-bridge.py example-output.txt$python3 ./check-bridge.py example-output.txt -v$python3 ./check-bridge.py example-output.txt -VThe script does not catch all bugs. And the program may only produce incorrect output under certainscheduling order of threads. Do more testing even if the program seems working. For example, we canincrease the number of trips and/or use a for loop in bash to run the program with same arguments multipletimes.$ ./bridge -t200 | ./check-bridge.py$ for ((i=0;iAppendixBelow is an example that helps us to understand the rules. We use a status line, shown below, to describethe current state. > indicates eastbound phase and ncrossed=N [waiting eastbound cars]>[cars on bridge]>[waiting westbound cars]The example assumes max_crossing is 2, max_crossed is 4, and the initial state is:ncrossed=0 []>[]>[]1. Three eastbound cars (cars 1, 2, and 3) arrive at the bridge, two of them, say, cars 1 and 3, can get onthe bridge. Car 2 has to wait because of Rule 1.ncrossed=2 [2]>[1,3]>[]2. Car 1 leaves the bridge.ncrossed=2 [2]>[3]>[]3. Car 4 traveling eastbound arrives. Luckily, it gets on the bridge without waiting.ncrossed=3 [2]>[3,4]>[]4. Car 9 traveling westbound arrives. It has to wait because Rule 2.ncrossed=3 [2]>[3,4]>[9]5. Cars 3 and 4 leave the bridge.ncrossed=3 [2]>[]>[9]6. Car 2 gets on the bridge (Rule 4).ncrossed=4 []>[2]>[9]7. Car 5 traveling eastbound arrives. It has to wait because of Rule 3.ncrossed=4 [5]>[2]>[9]8. Car 2 leaves the bridge.ncrossed=4 [5]>[]>[9]9. Car 9 gets on the bridge. Phase is now westbound.ncrossed=1 [5]10. After car 9 leaves, car 5 can get on the bridge. Phase is now eastbound.ncrossed=1 []>[5]>[]Enjoy!2转自:http://www.6daixie.com/contents/3/4438.html

你可能感兴趣的:(讲解:CSE 3100、Systems Programming、Python、PythonPython|)