about the solution for the problem of bridge.
about the solution for the problem of bridge:
we define a new class "Bridge" to simulate the operation of a bridge.and here is the interface of this class(Vehicle.h):
class Bridge {
public:
Bridge();
~Bridge();
void ArriveBridge(int direc); //if car coming in direc, calling this function in the thread where the vehicle executing.
void CrossBridge(int direc); //if the bridge is capable, then this function will be executed.
void ExitBridge(int direc); //if the car on bridge has executed Pass() successfully, then call this function.
private: List *queue[2]; //denote the waiting cars in two directions.
int age[2]; //denote the number have passed in both two directions.
int curDriection; //denote the current direction of the cars on the bridge(aims to preserve cars from crash).
Semaphore *sem; //as a variable of mutex to synchronize the operation on the bridge. Semaphore *avi; //as a variable of a counter to represent the limitation of the bridge's capacity.
};
this class comprise two lists represent the waiting cars from two directions. if a thread calls the ArriveBridge(), then the caller will be inserted into these two lists by the key of directions.
after calling ArriveBridge, we need a thread to take the charge of the monitor, this thread will call the CrossBridge() in a fixed time and never exit untill all processes exit. this function will make a decision that which car in these two lists can corss the bridge.and here is the algorithm in details: first of all, the thread must get the acception from the monitor via calling avi->P(); the next, make a decision via the following steps: 1)if one of two lists is empty, then make the direction to be the contrary one, else goto the step 2. 2)if the number of passed car from one direction is greater more than a constant, then, we will make the direction to be the less one, else goto step 3. 3)making the direction to be the current direction, aims to make the corss convenient. after that, checking if the direciton made above equals the current direction on the bridge, if yes, then the car can cross the bridge. if not, then this car must wait untill seizing the rest acceptions via calling avi->P() twice, that means there must be no car on the bridge. ok, then release this car from the fit list.revalue the variable of the current direction. that's all the details above.
and the last, if the subthread who represents the car finished, then he must call ExitBridge(), and this will give the acception back to the monitor, and wake up some threads waiting for the acceptions.
here, we have another question: why fair? that is easy to see, in the CrossBridge(), we make this available via controlling the number of passed cars from two directions,that is,making the distance to be constant.
we define a new class "Bridge" to simulate the operation of a bridge.and here is the interface of this class(Vehicle.h):
class Bridge {
public:
Bridge();
~Bridge();
void ArriveBridge(int direc); //if car coming in direc, calling this function in the thread where the vehicle executing.
void CrossBridge(int direc); //if the bridge is capable, then this function will be executed.
void ExitBridge(int direc); //if the car on bridge has executed Pass() successfully, then call this function.
private: List *queue[2]; //denote the waiting cars in two directions.
int age[2]; //denote the number have passed in both two directions.
int curDriection; //denote the current direction of the cars on the bridge(aims to preserve cars from crash).
Semaphore *sem; //as a variable of mutex to synchronize the operation on the bridge. Semaphore *avi; //as a variable of a counter to represent the limitation of the bridge's capacity.
};
this class comprise two lists represent the waiting cars from two directions. if a thread calls the ArriveBridge(), then the caller will be inserted into these two lists by the key of directions.
after calling ArriveBridge, we need a thread to take the charge of the monitor, this thread will call the CrossBridge() in a fixed time and never exit untill all processes exit. this function will make a decision that which car in these two lists can corss the bridge.and here is the algorithm in details: first of all, the thread must get the acception from the monitor via calling avi->P(); the next, make a decision via the following steps: 1)if one of two lists is empty, then make the direction to be the contrary one, else goto the step 2. 2)if the number of passed car from one direction is greater more than a constant, then, we will make the direction to be the less one, else goto step 3. 3)making the direction to be the current direction, aims to make the corss convenient. after that, checking if the direciton made above equals the current direction on the bridge, if yes, then the car can cross the bridge. if not, then this car must wait untill seizing the rest acceptions via calling avi->P() twice, that means there must be no car on the bridge. ok, then release this car from the fit list.revalue the variable of the current direction. that's all the details above.
and the last, if the subthread who represents the car finished, then he must call ExitBridge(), and this will give the acception back to the monitor, and wake up some threads waiting for the acceptions.
here, we have another question: why fair? that is easy to see, in the CrossBridge(), we make this available via controlling the number of passed cars from two directions,that is,making the distance to be constant.
0 Comments:
Post a Comment
<< Home