Advertisement
Guest User

11111

a guest
Mar 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <semaphore.h>
  3. #include <iostream>
  4.  
  5. #include "Message.h"
  6.  
  7. const int CUT_TIME_MIN = 1000,
  8. CUT_TIME_MAX = 5000,
  9. COME_TIME_MIN = 1000,
  10. COME_TIME_MAX = 1000;
  11.  
  12. int COUNT_BARBER = 1, // Количество брадобреев
  13. COUNT_CUSTOMER = 5, // Количество посетителей
  14. COUNT_CHAIR = 3; // Количество кресел
  15. COUNT_WAITING = 0; //Количество ожидающих
  16. atomic_int _end;
  17.  
  18. pthread_mutex_t mutexEnter, // Мьютекс для разрешения входа
  19. mutexInc; // Для подсчета посетителей
  20. sem_t semCustomers, // Ожидающие посетители
  21. semBarbers, // Брадобреи
  22. semChairs; // Кресла брадобреев
  23.  
  24. int RESULT = 0;
  25.  
  26. void* BarberFunction(void*);
  27. void* CustomerFunction(void*);
  28.  
  29. int main(int argc, char** argv)
  30. {
  31. srand(time(0));
  32.  
  33. // Разбор параметров
  34. if (argc > 1) COUNT_BARBER = atoi(argv[1]);
  35. if (argc > 2) COUNT_CUSTOMER = atoi(argv[2]);
  36. if (argc > 3) COUNT_CHAIR = atoi(argv[3]);
  37.  
  38. // Инициализация семафоров и мьютексов
  39. //InitWriteMutex();
  40. //pthread_mutex_init(&mutexEnter, NULL);
  41. //pthread_mutex_init(&mutexInc, NULL);
  42. sem_init(&semCustomers, 0, 0);//COUNT_CHAIR + COUNT_BARBER);
  43. sem_init(&semBarbers, 0, 0);
  44. //sem_init(&semChairs, 0, 0);
  45.  
  46.  
  47.  
  48.  
  49. // Создание потоков
  50. pthread_t threadBarber[COUNT_BARBER], threadCustomer[COUNT_CUSTOMER];
  51. pthread_create(&threadBarber[1], NULL, &BarberFunction, NULL);
  52. for (int i = 0; i < COUNT_CUSTOMER; i++)
  53. {
  54. int come = rand() % (COME_TIME_MAX - COME_TIME_MIN) + COME_TIME_MIN;
  55. usleep(come);
  56. pthread_create(&threadCustomer[i], NULL, &CustomerFunction, NULL);
  57. }
  58.  
  59. // Завершение потоков
  60. for (int i = 0; i < COUNT_CUSTOMER; i++)
  61. pthread_join(threadCustomer[i], NULL);
  62.  
  63. pthread_cancel(threadBarber[1]);
  64.  
  65. // Уничтожение семафоров и мьютексов
  66. sem_destroy(&semChairs);
  67. sem_destroy(&semBarbers);
  68. sem_destroy(&semCustomers);
  69. pthread_mutex_destroy(&mutexInc);
  70. pthread_mutex_destroy(&mutexEnter);
  71. DestrouWriteMutex();
  72.  
  73. std::cout << '\n';
  74. if (RESULT == COUNT_CUSTOMER)
  75. WriteCorrect(RESULT, "клентов побрито");
  76. else if (RESULT == 0)
  77. WriteError(RESULT, "клентов побрито");
  78. else
  79. WriteWarning(RESULT, "клентов побрито");
  80.  
  81. return 0;
  82. }
  83.  
  84.  
  85. void* BarberFunction(void*)
  86. {
  87. /*while (true)
  88. {
  89. if (sem_trywait(&semChairs) != 0)
  90. {
  91. WriteInfo((int)pthread_self(), "Брадобрей засыпает");
  92. sem_wait(&semChairs);
  93. WriteInfo((int)pthread_self(), "Брадобрей просыпается");
  94. }
  95. WriteInfo((int)pthread_self(), "Брадобрей бреет");
  96. int cut = rand() % (CUT_TIME_MAX - CUT_TIME_MIN) + CUT_TIME_MIN;
  97. usleep(cut);
  98. WriteInfo((int)pthread_self(), "Брадобрей закончил");
  99. sem_post(&semBarbers);
  100. sem_post(&semCustomers);
  101. }*/
  102.  
  103. while(_end != -1)
  104. {
  105. sem_wait(&semCustomers);
  106. COUNT_WAITING--;
  107. WriteInfo((int)pthread_self(), "Брадобрей бреет");
  108. int cut = rand() % (CUT_TIME_MAX - CUT_TIME_MIN) + CUT_TIME_MIN;
  109. usleep(cut);
  110. sem_post(&semBarbers);
  111. }
  112. return NULL;
  113. }
  114.  
  115. void* CustomerFunction(void*)
  116. {
  117. /*
  118. pthread_mutex_lock(&mutexEnter);
  119. int come = rand() % (COME_TIME_MAX - COME_TIME_MIN) + COME_TIME_MIN;
  120. usleep(come);
  121. pthread_mutex_unlock(&mutexEnter);
  122.  
  123. WriteWarning((int)pthread_self(), "Клиент пришел");
  124. if (sem_trywait(&semCustomers) != 0)
  125. {
  126. WriteError((int)pthread_self(), "Клиент ушел небритый");
  127. }
  128. else
  129. {
  130. sem_post(&semChairs);
  131. sem_wait(&semBarbers);
  132. pthread_mutex_lock(&mutexInc);
  133. RESULT++;
  134. pthread_mutex_unlock(&mutexInc);
  135. WriteCorrect((int)pthread_self(), "Клиент ушел побритый");
  136. }*/
  137.  
  138. _end++;
  139. WriteWarning((int)pthread_self(), "Клиент пришел");
  140. if(COUNT_WAITING < COUNT_CHAIR)
  141. {
  142. COUNT_WAITING++;
  143. WriteWarning((int)pthread_self(), "Клиент ждёт");
  144. sem_post(&semCustomers);
  145. sem_wait(&semBarbers);
  146. WriteWarning((int)pthread_self(), "Клиент ушёл побритый");
  147. RESULT++;
  148. }
  149. else
  150. {
  151. WriteError((int)pthread_self(), "Клиент ушел небритый");
  152. }
  153. _end--;
  154. return NULL;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement