Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdlib.h>
- //
- static pthread_mutex_t mut_in = PTHREAD_MUTEX_INITIALIZER;
- static pthread_mutex_t mut_out = PTHREAD_MUTEX_INITIALIZER;
- //
- static pthread_mutex_t mut_carIn = PTHREAD_MUTEX_INITIALIZER;
- static pthread_mutex_t mut_carOut = PTHREAD_MUTEX_INITIALIZER;
- //
- static pthread_mutex_t mut_parking = PTHREAD_MUTEX_INITIALIZER;
- //
- static pthread_cond_t con_entry = PTHREAD_COND_INITIALIZER;
- static pthread_cond_t con_exit = PTHREAD_COND_INITIALIZER;
- static pthread_cond_t con_parking = PTHREAD_COND_INITIALIZER;
- //
- bool carWaitingEntry = false;
- bool entryDoorOpen = false;
- bool carWaitingExit = false;
- bool exitDoorOpen = false;
- static unsigned int parkingSpots = 7;
- void carEnterCarPark(int carNumber)
- {
- pthread_mutex_lock(&mut_in);
- carWaitingEntry = true;
- //std::cout << "Car number " << carNumber << " waiting at entry gate" << std::endl;
- pthread_cond_signal(&con_entry);
- while(!entryDoorOpen)
- {
- pthread_cond_wait(&con_entry, &mut_in);
- }
- carWaitingEntry = false;
- //std::cout << "Car number " << carNumber << " is through entry gate" << std::endl;
- pthread_cond_signal(&con_entry);
- while(entryDoorOpen)
- {
- pthread_cond_wait(&con_entry, &mut_in);
- }
- pthread_mutex_unlock(&mut_in);
- }
- void carExitCarPark(int carNumber)
- {
- pthread_mutex_lock(&mut_out);
- carWaitingExit = true;
- // std::cout << "Car number " << carNumber << " waiting at exit gate" << std::endl;
- pthread_cond_signal(&con_exit);
- while(!exitDoorOpen)
- {
- pthread_cond_wait(&con_exit, &mut_out);
- }
- carWaitingExit = false;
- // std::cout << "Car number " << carNumber << " is through exit gate" << std::endl;
- pthread_cond_signal(&con_exit);
- while(exitDoorOpen)
- {
- pthread_cond_wait(&con_exit, &mut_out);
- }
- pthread_mutex_unlock(&mut_out);
- }
- void* carDriverThread(void* arg)
- {
- int carNumber = (int) arg;
- //std::cout << "Car number " << carNumber << " driving up to car park" << std::endl;
- pthread_mutex_lock(&mut_carIn);
- carEnterCarPark(carNumber);
- pthread_mutex_unlock(&mut_carIn);
- int waitingTime = rand() % 5 + 1;
- // std::cout << "Car number " << carNumber << " is now in the park and is gonna stay there for " << waitingTime << " sec" << std::endl;
- sleep(waitingTime);
- // std::cout << "Car number " << carNumber << " is now halfway through it's waiting time" << std::endl;
- //sleep(waitingTime/2);
- //std::cout << "Car number " << carNumber << " is now leaving the park" << std::endl;
- pthread_mutex_lock(&mut_carOut);
- carExitCarPark(carNumber);
- pthread_mutex_unlock(&mut_carOut);
- //std::cout << "Car number " << carNumber << " is now out of the car park" << std::endl;
- }
- void entryGate()
- {
- pthread_mutex_lock(&mut_in);
- //std::cout << "Entry gate is waiting for car" << std::endl;
- while(!carWaitingEntry)
- {
- pthread_cond_wait(&con_entry, &mut_in);
- }
- entryDoorOpen = true;
- //std::cout << "Entry gate is open" << std::endl;
- pthread_cond_signal(&con_entry);
- while(carWaitingEntry)
- {
- pthread_cond_wait(&con_entry, &mut_in);
- }
- entryDoorOpen = false;
- //std::cout << "Entry gate is closed" << std::endl;
- pthread_cond_signal(&con_entry);
- pthread_mutex_unlock(&mut_in);
- }
- void* entryGuardThread(void* arg)
- {
- int i = 0;
- //std::cout << "Entry gate is alive" << std::endl;
- while(1)
- {
- pthread_mutex_lock(&mut_parking);
- while(parkingSpots == 0)
- {
- pthread_cond_wait(&con_parking, &mut_parking);
- }
- entryGate();
- --parkingSpots;
- ++i;
- pthread_mutex_unlock(&mut_parking);
- std::cout << "Entry gate report " << parkingSpots << " parking spots free" << std::endl;
- std::cout << i << " number of cars have entered" << std::endl;
- }
- }
- void exitGate()
- {
- pthread_mutex_lock(&mut_out);
- //std::cout << "Exit gate is waiting for car" << std::endl;
- while(!carWaitingExit)
- {
- pthread_cond_wait(&con_exit, &mut_out);
- }
- exitDoorOpen = true;
- // std::cout << "Exit gate is open" << std::endl;
- pthread_cond_signal(&con_exit);
- while(carWaitingExit)
- {
- pthread_cond_wait(&con_exit, &mut_out);
- }
- exitDoorOpen = false;
- //std::cout << "Exit gate is closed" << std::endl;
- pthread_cond_signal(&con_exit);
- pthread_mutex_unlock(&mut_out);
- }
- void* exitGuardThread(void* arg)
- {
- int i = 0;
- //std::cout << "Exit gate is alive" << std::endl;
- while(1)
- {
- exitGate();
- pthread_mutex_lock(&mut_parking);
- ++parkingSpots;
- ++i;
- std::cout << "Exit gate report " << parkingSpots << " parking spots free" << std::endl;
- std::cout << i << " number of cars have left" << std::endl;
- pthread_cond_signal(&con_parking);
- pthread_mutex_unlock(&mut_parking);
- }
- }
- int main()
- {
- pthread_t carID[10];
- pthread_t entryID, exitID;
- int numberOfCars = 10;
- pthread_create(&entryID, NULL, entryGuardThread, NULL);
- pthread_create(&exitID, NULL, exitGuardThread, NULL);
- for(int i = 0; i < numberOfCars; ++i)
- {
- pthread_create(&carID[i], NULL, carDriverThread, (void*) i);
- }
- for(int i = 0; i < numberOfCars; ++i)
- {
- pthread_join(carID[i], NULL);
- }
- pthread_join(entryID, NULL);
- pthread_join(exitID, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement