ISU E6 - Thread synchronization II



Introduction

In this exercise you will implement a Parking Lot Control System (PLCS) which monitors a
parking lot and grants access for cars to enter and exit the parking lot.

Prerequisites

Finished Exercise Thread Synchronization I.

Goal

To give you some routine in mapping real-world problems to multithreaded programmed solutions

This is a sketch of the PLCS:
A simple sketch of the PLCS
These are the 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.


Use the Park-a-Lot 2000 example of the recent lecture as an example of how a car may interact with the PLCS entry (or exit) guard to request access to enter (or exit) the parking lot and receive permission to do so.
These exercises must be implemented using mutex/conditional constructs. Do note that if you want to use global variables then it do by all means.
Finally if design information is left out from you point of view... then consider it by design. Determine what you think should be done in that particular situation and make a note about it

Exercise 1 Implement Park-a-Lot 2000 (60%)

Exercise 1.1 First step

Implement the PLCS and verify that it works with a single car that enters the parking lot, wait there for some time and exits the parking lot again.
Before you implement anything write out your solution in pseudo code or flowcharts. This applies to every exercise...

To find the solution following pseudo code has been used.

 
We have chosen to use two mutexs and two conditionals, one at each port, because the ports have to work individually.

Code:





Execution:




Question: Which statement is valid and why?
  1. One conditional variable used with multiple mutexes.
    • Invalid: If thread don't try to lock the same mutex they have no need to communicate.
  2. Multiple conditional variables used in a single mutex.
    • Valid: When more threads tries to lock the same mutex, they communicate through conditional variables. Threads can use more than one conditional variable to communicate.

Exercise 1.2 The grandiose test

Repeat the above test this time with multiple cars. Furthermore every car should wait a different amount of time in the parking lot before exiting. In this scenario the car park does not have an upper bound on the number of cars it has space for.
Verify that all cars are able to enter and exit as would be expected.
The code is very similar to the code in 1.1. The only thing added is another defined number of cars.
#define NUM_OF_CARS 10;
Execution:



Consider and argue when and why you are to use pthread_cond_broadcast(), also explain what it does.
The pthread_cond_broadcast() function shall unblock all threads currently blocked on the specified condition variable cond.
The pthread_cond_signal() function shall unblock at least  one  of  the threads  that  are blocked on the specified condition variable cond

The pthread_cond_broadcast() function is used whenever the shared-variable state has been changed in a way that more than one thread can proceed with its task. Consider a single producer/multiple consumer problem, where the producer can insert multiple items on a list that is accessed one item at a time by the consumers. By calling the pthread_cond_broadcast() function, the producer would notify all consumers that might be waiting, and thereby the application would receive more throughput on a multi-processor. The pthread_cond_broadcast() function is needed in order to wake up all waiting readers when a writer releases its lock.


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. This is not part of the assignment to ensure an order. Never the less why does this happen, and can you think of an approach that would fix it?

This can be solved in the entry and exit threads. Before entry opens the gate for a car, it compares a variable: "parkedCars" with another variable: "carsAllowed". If parkedCars <carsAllowed then it opens the gate and increments parkedCars. If, however, the parkedCars> = carsAllowed then entry is going to sleep instead without opening the gate.
Now it's up to the exit guard to wake entry guard again. When a car signals the exit guard to open the exit gate, it decrements parkedCars and finally sends a signal to the entry guard in case it is sleeping.

Exercise 2 Extending PCLS, now with a limit on the number of cars

(40%)
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.
Extend your PLCS to handle this situation and verify that it actually does as you expect.
Remember to test the scenario where a car leaves a full parking lot, enabling a waiting car to
enter.

The code has added some few linies.



Then we have also created a new conditional variable called c_full.
Execution

1 kommentar:

  1. the goal to this exercise is to give some routine in mapping real world problems into multithreaded programmed solutions. which it seems you have done in a nice way. the goal in the first part is to use pseudo code to structure your design pattern and afterwards implemente it as af a solution.

    which you have done. it would have been nice if you just implemented the pseudo functions instead of rewriting the code bud still you have folowed the structure of your pseudo code.
    youre solution in part 1.2 is good. you encrement the numbers of cars send into the gareage and get the expected output. you also anser the asked questions and youre ansers is nice explained in a way that external peapol know what you mean.
    in part 2 you have to extend the program whitch you have done.
    youre solution is good and there for you pass.

    SvarSlet