联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2019-11-21 11:53

CSE 3100 Systems Programming Fall 2019

Homework #8 Due: 11/26/2019

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.

1

$python3 ./check-bridge.py example-output.txt

$python3 ./check-bridge.py example-output.txt -v

$python3 ./check-bridge.py example-output.txt -V

The script does not catch all bugs. And the program may only produce incorrect output under certain

scheduling order of threads. Do more testing even if the program seems working. For example, we can

increase the number of trips and/or use a for loop in bash to run the program with same arguments multiple

times.

$ ./bridge -t200 | ./check-bridge.py

$ for ((i=0;i<10;i++)) do ./bridge -se0 | ./check-bridge.py || break; done

Appendix

Below is an example that helps us to understand the rules. We use a status line, shown below, to describe

the current state. > indicates eastbound phase and < indicates westbound phase.

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 on

the 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]<[9]<[]

10. After car 9 leaves, car 5 can get on the bridge. Phase is now eastbound.

ncrossed=1 []>[5]>[]

Enjoy!

2


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp