

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Professor: Carver; Class: Concurrent and Distrib Systems; Subject: Computer Science; University: George Mason University; Term: Unknown 1989;
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Process Synchronization The goal of this exercise to give you some experience writing concurrent pro- grams. You have been hired by MetroTrans to synchronize traffic over a narrow light-duty bridge on a public highway. Traffic may only cross the bridge in one direction at a time, and if there are ever more than 3 vehicles on the bridge at one time, it will collapse under their weight. In this system, each vehicle is represented by one thread, which executes the procedure OneVehicle when it arrives at the bridge:
OneVehicle(int vehicle_id,int direc, int time_to_cross) { ArriveBridge(direc); CrossBridge(vehicle_id,direc,time_to_cross); ExitBridge(vehicle_id,direc); }
In the code above, vehicle id is an integer which uniquely identifies each vehicle (The vehicles arriving at the bridge should be assigned vehicle id s 1,2,3... and so on ). direc is either 0 or 1; it gives the direction in which the vehicle will cross the bridge. time to cross is the time it takes a vehicle to cross the bridge – assume that every vehicle takes 4 seconds to cross the bridge. Implement a “fair” traffic control policy that imposes a limit (say 5) on the number of vehicles that can cross the bridge in one direction, while vehicles travelling in the opposite direction are waiting to get on the bridge. NOTE: if there are no vehicles waiting to get on the bridge in the opposite direction, your policy should not switch directions. In other words, your policy should be smart enough to provide fairness if there is traffic in both directions but should never force a vehicle to wait to get on the bridge if there is no traffic in the opposite direction. Write the procedures ArriveBridge,CrossBridge and ExitBridge, using mutex locks and condition variables for synchronization^1. The ArriveBridge procedure must not return until it is safe for the car to cross the bridge in the given direction (it must guarantee that there will be no head-on collisions or bridge collapses). The CrossBridge procedure should just delay for time to cross seconds and print out a debug message. ExitBridge is called to indicate that the caller has finished crossing the bridge; ExitBridge should take steps to let additional cars cross the bridge. In addition, ExitBridge should update a shared variable departure index, which keeps track of the order in which the cars leave the bridge, i.e., the first car to leave the bridge has departure index 1, the second car has departure index 2, and so on. The ExitBridge procedure should also print out the departure index for that vehicle.
(^1) If you use Java for the assignment, use the equivalent thread synchronization faciltities
In this assignment, you have to run your program for the three vehicle arrival schedules given below:
(i) 5 : DELAY(10) : 5 : DELAY(10) : 5 : DELAY(10) : 5: DELAY(10) : 5 : DELAY(10) : 5 (ii) 10 : DELAY(10) : 10 : DELAY(10): 10 (iii) 30 Here the numbers indicate the number of vehicles arriving simultaneously at the bridge, while the numbers in parentheses indicate the delay before the next arrival(s). For example, under schedule (i) 5 vehicles arrive simultaneously at the bridge at the start of the experiment, five more vehicles arrive simultaneously 10 seconds after the arrival of the first five vehicles and so on. In each of the three schedules, thirty vehicles arrive at the bridge during the course of the experiment. Note that vehicles arriving simultaneously does not imply that they are all traveling in the same direction. Assume that the probability that a vehicle is traveling in direction “0” is 0.
1. You can use either Java or any multithreaded programming library for doing this assignment. Both Java and the Solaris thread library are available on machines in the IT&E lab. 2. On Solaris, information on how a thread library call is to be invoked is provided by the corresponding man page, e.g., to see how pthread create is invoked type man pthread create. Additional doc- umentation on multi-threaded programming using the Solaris thread library is also available on the Solaris documentation web site (see link on class web page). 3. If you are using the Solaris library, use the system call drand48() to determine the direction of the vehicle (according to the probability distribution specified above). The random number stream should be initialized by calling srand48() with the desired seed. If you are using Java for this assignment, use the methods in class java.util.Random to create and use the random number stream that determines the direction of a vehicle.