Exercise 1 - Implement Park-a-Lot 2000
Requirements for the PLCS:
Cars enter the parking lot, stay there for a while, and then exit the parking lot again.
Then they wait a while before they re-enter the parking lot.
An arriving car must request permission to enter the parking lot from the PLCS entry
guard. When permission is granted, the car may enter the parking lot.
An exiting car must request permission to exit the parking lot from the PLCS exit guard.
When access is granted, the car may exit the parking lot.
Each car must be represented by a single thread, the same goes for the PLCS entry guard
and the PLCS exit guard. Running the program with one car would therefore result in 3
threads.
Each guard controls a door and when no car is waiting to enter, the door must be closed.
Flow chart
Implementation
We have implementet the code to work with both one car and serveral cars. It is working as expected.
Testing
When we executes the program with one car:
When we executes the programt with 5 cars:
Question: Which statement is valid and why?
1. One conditional variable used with multiple mutexes.
2. Multiple conditional variables used in a single mutex.
We use multiple conditional and one mutex. The different conditional variabels decides whether the car is going in or out the parking lot. The mutex protects the conditional variabels.
Consider and argue when and why you are to use pthread_cond_broadcast(), also explain what
it does.
pthread_cond_broadcast() is wakening up all the the threads with a curtaint conditional variabel.
pthread_cond_signal() only wake one thread up.
In our code there is almost no difference between the two ways of wakening up the threads. We have chosen to use broadcast because multiple car threads can be waken up, with only one call from the entry and exit guard.
During the process of figuring out the solution, you will discover that the cars will seemingly not
wait in line, but overtake each other on the way in or out. Why does this happen, and can you think of an approach that would fix it?
To fix it you can make a new mutex and lock this around the entry and exit in the car thread function. This way only one car at a time, will arrive and enter, before another car arrives and enters.
Implementation
We have implementet the code to work with both one car and serveral cars. It is working as expected.
Global Variabels, mutex/condition initialization and fuction prototypes
Car threads
Entry guard
Exit guard
Testing
When we executes the program with one car:
When we executes the programt with 5 cars:
Question: Which statement is valid and why?
1. One conditional variable used with multiple mutexes.
2. Multiple conditional variables used in a single mutex.
We use multiple conditional and one mutex. The different conditional variabels decides whether the car is going in or out the parking lot. The mutex protects the conditional variabels.
Consider and argue when and why you are to use pthread_cond_broadcast(), also explain what
it does.
pthread_cond_broadcast() is wakening up all the the threads with a curtaint conditional variabel.
pthread_cond_signal() only wake one thread up.
In our code there is almost no difference between the two ways of wakening up the threads. We have chosen to use broadcast because multiple car threads can be waken up, with only one call from the entry and exit guard.
During the process of figuring out the solution, you will discover that the cars will seemingly not
wait in line, but overtake each other on the way in or out. Why does this happen, and can you think of an approach that would fix it?
To fix it you can make a new mutex and lock this around the entry and exit in the car thread function. This way only one car at a time, will arrive and enter, before another car arrives and enters.
Exercise 2 - Extending PCLS, now with a limit on the number of cars
Implementation
The changes is one new conditional variabel and two new global variabels:
And the changes in the code, is made in the car thread.
If the parkinglot is full (parked_cars>=max_cars) --> the car thread waits on the conditional variabel 3.
When a car is exiting the parkinglot (--parked_cars) --> the conditional variabel 3 is broadcasted.
parked_car is the indicator of how many cars the parkinglot holds.
car_waiting shows how many cars - which has gotten permission to enter - is waiting.
This way we dont have to change the entry og exit guard.
It is importaint that the car_waiting is conted up AFTER the permission is granded - because the entry guard has to close the door, even if there is more cars waiting outside the door (if they can't enter).
The parkinglot can only hold maximum 2 cars at a time!
When there is 2 cars in the parkinglot, a car has to exit, before a new car can enter.
Ingen kommentarer:
Send en kommentar