I3ISU-Ex6: Thread Synchronization II

Introduction
In this exercise a real world example is used to         help visualize multithreaded programmed solutions: PLCS: A Parking Lot Control System which monitors a parking lot and grants access for cars to enter and exit the parking lot. The PLCS has the following requirements: Then they wait a while before they re-enter the parking lot.
 * Cars enter the parking lot, stay there for a while, and then exit the parking lot again.
 * 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.

Subexercise 1: Implement Park-a-lot 2000
First Step and The Grandiose Test have been made in 1 go instead of making a First Step for one car and then expanding it to additional cars.

Answers to questions and other comments
Question: Which statement is valid and why?


 * 1) One conditional variable used with multiple mutexes.
 * 2) Multiple conditionals used in a single mutex.

Answer:

The second statement is valid. Mostly because we're using stament 2 and cant really come up with any setup where statement 1 would work(or be useful).

Pseudo Code
The pseudo code is presented below.

Car thread psuedo code

Entrance door thread psuedo code

Implemented Code
Car thread code

Entrance door thread code

Exit door thread code

Testing
The code run with 10 cars could look like this: Choose the number of cars to create (1-100): 10 Car 1 drives to entrance and waits. Car 2 drives to entrance and waits. Car 0 drives to entrance and waits. Car 3 drives to entrance and waits. Car 9 drives to entrance and waits. Entrance door is opened. Car 1 drives into the garage and parks. Car 2 drives into the garage and parks. Car 6 drives to entrance and waits. Car 6 drives into the garage and parks. Car 3 drives into the garage and parks. Car 5 drives to entrance and waits. Car 5 drives into the garage and parks. Car 7 drives to entrance and waits. Car 7 drives into the garage and parks. Car 0 drives into the garage and parks. Car 9 drives into the garage and parks. Car 8 drives to entrance and waits. Car 8 drives into the garage and parks. Entrance door is closed. Car 4 drives to entrance and waits. Entrance door is opened. Car 4 drives into the garage and parks. Entrance door is closed. Car 0 drives to exit and waits. Exit door is opened. Car 0 drives out of the garage and disappears Exit door is closed. Car 1 drives to exit and waits. Exit door is opened. Car 1 drives out of the garage and disappears Exit door is closed. Car 2 drives to exit and waits. Exit door is opened. Car 2 drives out of the garage and disappears Exit door is closed. Car 0 drives to entrance and waits. Entrance door is opened. Car 0 drives into the garage and parks. Entrance door is closed.

Subexercise 2: Extending PCLS, now with a limit on the number of cars
We now add an additional requirement to the PLCS:
 * The entry guard must ensure that entry is not granted to a car if the parking lot is full. In that case cars wanting to enter must wait.

Answers to questions and comment
In this part there is essentially just added a few lines of code, to the previous part. There is added an extra check in the door opener thread, if the parking garage is full the door doesnt open, but waits a signal from a car leaving the garage. There is added that any car that leaves the garage sends that signal, and increases a counter when a car drives inside, and decreases it when it drives outside. We have changed the conditional with the information that the door is now open, from a broadcast to a signal, which means only one thread wakes up everytime it goes through the loop, so the entry door thread can evaluate everytime if the parkinglot is full and the door should be closed.

Pseudo Code
Car thread psuedo code

Entrance door thread psuedo code

Implemented Code
Car thread code

Entry door thread code

Exit door thread code

Testing
Choose the number of cars to create (1-100): 10 Choose the number of parking spaces to create (1-100): 3 Car 6 drives to entrance and waits. Car 7 drives to entrance and waits. Car 5 drives to entrance and waits. Car 4 drives to entrance and waits. Car 3 drives to entrance and waits. Car 2 drives to entrance and waits. Car 8 drives to entrance and waits. Entrance door is opened. Car 9 drives to entrance and waits. Car 9 drives into the garage and parks. There is 1 cars parked inside the garage. Car 6 drives into the garage and parks. There is 2 cars parked inside the garage. Car 7 drives into the garage and parks. There is 3 cars parked inside the garage. Entrance door is closed. Parking garage is full. Car 1 drives to entrance and waits. Car 0 drives to entrance and waits. Car 6 drives to exit and waits. Exit door is opened. Car 6 drives out of the garage and disappears There is 2 cars parked inside the garage. Entrance door is opened. Car 5 drives into the garage and parks. There is 3 cars parked inside the garage. Entrance door is closed. Parking garage is full. Exit door is closed. Car 7 drives to exit and waits. Exit door is opened. Car 7 drives out of the garage and disappears There is 2 cars parked inside the garage. Entrance door is opened. Car 4 drives into the garage and parks. There is 3 cars parked inside the garage. Entrance door is closed. Parking garage is full. Exit door is closed.