|
UNIVERSITY OF WISCONSIN-MADISON
Computer Sciences Department | ||
|
CS 537
Fall 2012 | Bart Miller | |
| Quiz #3
Wednesday, October 10 |
You are in charge of filling busses with passengers at a bus terminal. Each bus has space for ten (10) passengers. Four (4) of those spaces can hold only wheelchair passengers, and the other six (6) can hold only non-wheelchair passengers.
Busses and both types of passengers arrive at random. As busses arrive, your are to fill them up with passengers. Once a bus is full (containing 6 non-wheelchair, and 4 wheelchair passengers), it is allowed to leave the terminal, along with its passengers.
You are responsible only for loading the passengers on the bus and having the bus depart. You do not need to worry about what happens to the busses or passengers after they leave. Each bus process has available to it the functions ArriveAtTerminal(), OpenDoors(), CloseDoors(), and DepartTerminal().
Each passenger process has available to it the functions ArriveAtTerminal() and GetOnBus(). Executing GetOnBus() loads the passenger on the bus, and the function returns when the passenger is properly seated. After a passenger process is safely on the bus, or a bus process has left the terminal, that process may terminate (exit).
Make sure that ONLY ONE PASSENGER AT A TIME is executing GetOnBus().
Using semaphores as your synchronization mechanism, fill in the below program for coordinating the loading of the buses. Use whatever language style is most familiar to you.
Assume that each bus is a process, and that the passenger is a process. You are to define the global variables (including semaphores) that are to be used, and how they are initialized. You are to then write the code that the two different types of passengers use, and the code for the busses.
// Declare semaphores and variables (if any) here
sem bus_lock (1);
sem let_wheel_in (0);
sem let_walk_in (0);
sem handshake (0);
|
void bus ()
{
int i;
ArriveAtTerminal();
OpenDoors();
bus_lock.P();
for (i = 0; i < 4; i++)
{
let_wheel_in.V);
handshake.P();
}
for (i = 0; i< 6; i++)
{
let_walk_in.V();
handshake.P();
}
bus_lock.V();
CloseDoors();
DepartTerminal();
}
|
void wheel_chair()
{
ArriveAtTerminal();
let_wheel_in.P();
GetOnBus();
handshake.V();
}
|
void non_wheel_chair()
{
ArriveAtTerminal();
let_walk_in.P();
GetOnBus();
handshake.V();
}
|