I3ISU_Opgave6

=Exercise 1 Implement Park-a-Lot 2000=

Exercise 1.1 First step
We have chosen not to show the entire .cpp file for our solution. Because we feel it is not necessary to show how we implement it and show that we can use conditionals and multithreading. First we show a part of our carController function. This bit shows our implementation of an entering car. After the sleep our implementation of an exiting car would normally be. This can be seen in the .cpp file (link further down in the exercise): code format="cpp" void* carController(void* threadid) {   int *id = reinterpret_cast(threadid);

pthread_mutex_lock(&mutexEntry);

carEntryWait = true; pthread_cond_signal(&carReadyEntry);

while(!entryPortOpened) pthread_cond_wait(&entryPortOpen, &mutexEntry);

printf("Car %d is driving in\n", id);

carEntryWait = false; entryPortOpened = false; pthread_cond_signal(&entryPortClosed); pthread_mutex_unlock(&mutexEntry);

sleep(5); //Vente tid.. Denne kan ændres efter ønske. code Next we have our entryController. We have chosen not to show our exitController because it is basically the same as the entryController: code format="cpp" void* entryController(void* threadid) {   while(1) {       pthread_mutex_lock(&mutexEntry); while(!carEntryWait) pthread_cond_wait(&carReadyEntry, &mutexEntry);

cout << "Opening entry gate!" << endl;

entryPortOpened = true; pthread_cond_signal(&entryPortOpen);

while(entryPortOpened) pthread_cond_wait(&entryPortClosed, &mutexEntry);

cout << "Closing entry gate!" << endl;

pthread_mutex_unlock(&mutexEntry); } } code This is the output when the program is executed with one car: code Opening entry gate! Car 0 is driving in Closing entry gate! Opening exit gate! Car 0 is driving out Closing exit gate! code As we can see the car enters and exits as expected. Link to full .cpp

Exercise 1.2 The grandiose test
For this exercise we have not changed anything in our code except the number of cars in out system. Output with 3 cars: code Opening entry gate! Car 0 is driving in Closing entry gate! Opening entry gate! Car 1 is driving in Closing entry gate! Opening entry gate! Car 2 is driving in Closing entry gate! Opening exit gate! Car 0 is driving out Closing exit gate! Opening exit gate! Car 1 is driving out Closing exit gate! Opening exit gate! Car 2 is driving out Closing exit gate! code Once again the cars enter and exit as expected.