Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. //
  6. static pthread_mutex_t mut_in = PTHREAD_MUTEX_INITIALIZER;
  7. static pthread_mutex_t mut_out = PTHREAD_MUTEX_INITIALIZER;
  8. //
  9. static pthread_mutex_t mut_carIn = PTHREAD_MUTEX_INITIALIZER;
  10. static pthread_mutex_t mut_carOut = PTHREAD_MUTEX_INITIALIZER;
  11. //
  12. static pthread_mutex_t mut_parking = PTHREAD_MUTEX_INITIALIZER;
  13. //
  14. static pthread_cond_t con_entry = PTHREAD_COND_INITIALIZER;
  15. static pthread_cond_t con_exit = PTHREAD_COND_INITIALIZER;
  16. static pthread_cond_t con_parking = PTHREAD_COND_INITIALIZER;
  17. //
  18.  
  19.  
  20. bool carWaitingEntry = false;
  21. bool entryDoorOpen = false;
  22. bool carWaitingExit = false;
  23. bool exitDoorOpen = false;
  24.  
  25. static unsigned int parkingSpots = 7;
  26.  
  27.  
  28.  
  29.  
  30. void carEnterCarPark(int carNumber)
  31. {
  32.     pthread_mutex_lock(&mut_in);
  33.     carWaitingEntry = true;
  34.     //std::cout << "Car number " << carNumber << " waiting at entry gate" << std::endl;
  35.     pthread_cond_signal(&con_entry);
  36.     while(!entryDoorOpen)
  37.     {
  38.         pthread_cond_wait(&con_entry, &mut_in);
  39.     }
  40.     carWaitingEntry = false;
  41.     //std::cout << "Car number " << carNumber << " is through entry gate" << std::endl;
  42.     pthread_cond_signal(&con_entry);
  43.     while(entryDoorOpen)
  44.     {
  45.         pthread_cond_wait(&con_entry, &mut_in);
  46.     }
  47.     pthread_mutex_unlock(&mut_in);
  48. }
  49.  
  50. void carExitCarPark(int carNumber)
  51. {
  52.     pthread_mutex_lock(&mut_out);
  53.     carWaitingExit = true;
  54.    // std::cout << "Car number " << carNumber << " waiting at exit gate" << std::endl;
  55.     pthread_cond_signal(&con_exit);
  56.     while(!exitDoorOpen)
  57.     {
  58.         pthread_cond_wait(&con_exit, &mut_out);
  59.     }
  60.     carWaitingExit = false;
  61.    // std::cout << "Car number " << carNumber << " is through exit gate" << std::endl;
  62.     pthread_cond_signal(&con_exit);
  63.     while(exitDoorOpen)
  64.     {
  65.         pthread_cond_wait(&con_exit, &mut_out);
  66.     }
  67.     pthread_mutex_unlock(&mut_out);
  68. }
  69.  
  70. void* carDriverThread(void* arg)
  71. {
  72.     int carNumber = (int) arg;
  73.     //std::cout << "Car number " << carNumber << " driving up to car park" << std::endl;
  74.    
  75.     pthread_mutex_lock(&mut_carIn);
  76.     carEnterCarPark(carNumber);
  77.     pthread_mutex_unlock(&mut_carIn);
  78.    
  79.     int waitingTime = rand() % 5 + 1;
  80.    // std::cout << "Car number " << carNumber << " is now in the park and is gonna stay there for " << waitingTime << " sec" << std::endl;
  81.     sleep(waitingTime);
  82.    // std::cout << "Car number " << carNumber << " is now halfway through it's waiting time" << std::endl;
  83.     //sleep(waitingTime/2);
  84.     //std::cout << "Car number " << carNumber << " is now leaving the park" << std::endl;
  85.    
  86.     pthread_mutex_lock(&mut_carOut);
  87.     carExitCarPark(carNumber);
  88.     pthread_mutex_unlock(&mut_carOut);
  89.    
  90.     //std::cout << "Car number " << carNumber << " is now out of the car park" << std::endl;
  91. }
  92.  
  93. void entryGate()
  94. {
  95.     pthread_mutex_lock(&mut_in);
  96.     //std::cout << "Entry gate is waiting for car" << std::endl;
  97.     while(!carWaitingEntry)
  98.     {
  99.         pthread_cond_wait(&con_entry, &mut_in);
  100.     }
  101.     entryDoorOpen = true;
  102.     //std::cout << "Entry gate is open" << std::endl;
  103.     pthread_cond_signal(&con_entry);
  104.     while(carWaitingEntry)
  105.     {
  106.         pthread_cond_wait(&con_entry, &mut_in);
  107.     }
  108.     entryDoorOpen = false;
  109.     //std::cout << "Entry gate is closed" << std::endl;
  110.     pthread_cond_signal(&con_entry);
  111.     pthread_mutex_unlock(&mut_in);
  112. }
  113.  
  114. void* entryGuardThread(void* arg)
  115. {
  116.     int i = 0;
  117.     //std::cout << "Entry gate is alive" << std::endl;
  118.     while(1)
  119.     {
  120.         pthread_mutex_lock(&mut_parking);
  121.         while(parkingSpots == 0)
  122.         {
  123.             pthread_cond_wait(&con_parking, &mut_parking);
  124.         }
  125.         entryGate();
  126.         --parkingSpots;
  127.         ++i;
  128.         pthread_mutex_unlock(&mut_parking);
  129.         std::cout << "Entry gate report " << parkingSpots << " parking spots free" << std::endl;
  130.         std::cout << i << " number of cars have entered" << std::endl;
  131.     }
  132. }
  133.  
  134. void exitGate()
  135. {
  136.     pthread_mutex_lock(&mut_out);
  137.     //std::cout << "Exit gate is waiting for car" << std::endl;
  138.     while(!carWaitingExit)
  139.     {
  140.         pthread_cond_wait(&con_exit, &mut_out);
  141.     }
  142.     exitDoorOpen = true;
  143.    // std::cout << "Exit gate is open" << std::endl;
  144.     pthread_cond_signal(&con_exit);
  145.     while(carWaitingExit)
  146.     {
  147.         pthread_cond_wait(&con_exit, &mut_out);
  148.     }
  149.     exitDoorOpen = false;
  150.     //std::cout << "Exit gate is closed" << std::endl;
  151.     pthread_cond_signal(&con_exit);
  152.     pthread_mutex_unlock(&mut_out);
  153. }
  154.  
  155. void* exitGuardThread(void* arg)
  156. {
  157.     int i = 0;
  158.     //std::cout << "Exit gate is alive" << std::endl;
  159.     while(1)
  160.     {
  161.         exitGate();
  162.         pthread_mutex_lock(&mut_parking);
  163.         ++parkingSpots;
  164.         ++i;
  165.         std::cout << "Exit gate report " << parkingSpots << " parking spots free" << std::endl;
  166.         std::cout << i << " number of cars have left" << std::endl;
  167.         pthread_cond_signal(&con_parking);
  168.         pthread_mutex_unlock(&mut_parking);
  169.     }
  170. }
  171.  
  172. int main()
  173. {
  174.     pthread_t carID[10];
  175.     pthread_t entryID, exitID;
  176.     int numberOfCars = 10;
  177.    
  178.    
  179.     pthread_create(&entryID, NULL, entryGuardThread, NULL);
  180.     pthread_create(&exitID, NULL, exitGuardThread, NULL);
  181.     for(int i = 0; i < numberOfCars; ++i)
  182.     {
  183.         pthread_create(&carID[i], NULL, carDriverThread, (void*) i);
  184.     }
  185.    
  186.     for(int i = 0; i < numberOfCars; ++i)
  187.     {
  188.         pthread_join(carID[i], NULL);
  189.     }
  190.    
  191.     pthread_join(entryID, NULL);
  192.     pthread_join(exitID, NULL);
  193.    
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement