This problem is based on a problem described in Concurrent Programming by Stephen J. Hartley.
Many amusement parks in the United States have bumper car rides. Each car holds one person. People who want to ride a bumper car line up and wait for a free car.
When a car is returned and the previous rider gets out, a new rider gets in the car from the head of the waiting line and rides around on the bumper ride floor, bumping into other cars. After riding for a while, the rider decides that enough is enough and returns the car to the area where new riders line up. A new rider gets in and takes a ride. The rider who just got out of the car wanders around the park, perhaps getting something to eat. After wandering for a while, the rider decides to take another bumper car ride, returns to the waiting area and gets in line.
If there are more free cars than people wishing to ride, the free cars are kept in a line. A person wishing to ride a bumper car gets in the car at the head of this line and returning cars go to the end of this line.
Develop code for a multithreaded program simulating the bumper car operations, and the riders as threads. Each of the N riders is simulated with one thread, created from a common class (if java) or function (if using posix threads).
Finally, there is a test driver (i.e. main) method/function whose main method starts everything.
The waiting area where riders get in line is implemented with a bounded buffer. Each rider who finishes wandering around the amusement park gets in line by requesting a bumper car. If there are no cars available, the rider stays in line (blocks) until one is available. The bounded buffer contains the car identifiers (as ints from 1 to number of bumper cars). Initially, the buffer is filled -- representing the bumper cars in line waiting for riders.
When a bumper car is requested and a car is available, the car identifier is given to the rider thread. If the rider had been waiting, she is no longer in line.
The rider rides for some period of time. This will be simulated with special code rideTime() code we provide on the course website. You can edit it to provide controlled values for your testing. We will use our own copy for grading.
We are also providing walkAroundTime() code on the course website to simulate the passing of time for the rider after she finishes the ride.
Here is one way to organize the program. A rider thread is instantiated multiple times. A coordinator class contains implementation of service methods invoked by the riders. The coordinator manages the bounded buffer.
Each time a rider wants to get in line, it calls int getInLine() method provided by the coordinator. getInLine() contains a critical section that determines if a car is available (i.e. is there a 'car' identifier currently in the buffer). If not, the thread is blocked. If there is a car, the car id is returned for the rider thread.
The rider then calls rideTime() method. This simulates the passing of time for the rider until the ride finishes.
When the ride is over, the rider thread invokes the coordinator's returnCar (int carId) to return the car to the queue of available cars. If a rider thread was inline waiting for a car, it must become unblocked by the returnCar method.
Running the Bumper Car Simulation
Below is some pseudo code. The pseudo-code shows that command-line arguments must provide the number of bumper cars, the number of riders, and the number of seconds for the simulation.
For example, if the c-code was executed as:
a.out 5 10 60
the program must provide 5 initial entries in the buffer with values 1,2, 3, 4, and 5 to indicate cars 1 through 5 are available, create 10 rider threads, and run the simulation for 60 seconds.
main: a coordinator with a bounded buffer containing the ids for the argv bumper cars. number of threads = argv for (int i = 0; i < number of threads ; i++) a rider thread and make it ready to run a thread (or use the main thread) that sleeps for argv seconds then exits. This thread will terminate the entire process.
The 'riding' and 'walking around' time is simulated by having the threads sleep. The code for Sleeper (Sleeper.java, sleeper.c, and sleeper.cpp) is on the course webpage. Feel free to edit your copy of the methods/functions for testing.
Below is pseudo-code for the riders:
Rider Thread: while (true) Sleeper walkAround (...) coordinator getInLine(...); Sleeper rideTime (...); cordinator returnCar (...)
Below is pseudo-code for the coordinator:
Coordinator int getInLine() if no car available, block return carId returnCar (int carId) place carId in buffer unblock a waiting thread
Please provide a rider id for each of your riders and provide output statements each time the rider changes state.
Rider 1 is walking around the park. Rider 2 is walking around the park. Rider 1 is now riding in car 1. Rider 1 returned car 1. Rider 2 is now riding in car 2. etc.
Please submit the following source files:
· Rider.java or rider.c or rider.cpp (and any associated header files)
· Coordinator.java or coordinator.c or coordinator.cpp (and any associated header files)
· You should submit any "Sleeper" files only if you changed them
Put the main() method to run your simulation in the coordinator module.
Recently Asked Questions
- how does beta-adrenergic receptor activation lead to increased myocardial contractility?
- 17.Your company's CEO does not have authority over some of the activities involved in a) a functional process b) a workgroup process c) an enterprise process
- Please refer to the attachment to answer this question. This question was created from Scavenger Hunt Biology 2 2017 (2).docx.