Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <semaphore.h>
- #include <iostream>
- #include "Message.h"
- const int CUT_TIME_MIN = 1000,
- CUT_TIME_MAX = 5000,
- COME_TIME_MIN = 1000,
- COME_TIME_MAX = 1000;
- int COUNT_BARBER = 1, // Количество брадобреев
- COUNT_CUSTOMER = 5, // Количество посетителей
- COUNT_CHAIR = 3; // Количество кресел
- COUNT_WAITING = 0; //Количество ожидающих
- atomic_int _end;
- pthread_mutex_t mutexEnter, // Мьютекс для разрешения входа
- mutexInc; // Для подсчета посетителей
- sem_t semCustomers, // Ожидающие посетители
- semBarbers, // Брадобреи
- semChairs; // Кресла брадобреев
- int RESULT = 0;
- void* BarberFunction(void*);
- void* CustomerFunction(void*);
- int main(int argc, char** argv)
- {
- srand(time(0));
- // Разбор параметров
- if (argc > 1) COUNT_BARBER = atoi(argv[1]);
- if (argc > 2) COUNT_CUSTOMER = atoi(argv[2]);
- if (argc > 3) COUNT_CHAIR = atoi(argv[3]);
- // Инициализация семафоров и мьютексов
- //InitWriteMutex();
- //pthread_mutex_init(&mutexEnter, NULL);
- //pthread_mutex_init(&mutexInc, NULL);
- sem_init(&semCustomers, 0, 0);//COUNT_CHAIR + COUNT_BARBER);
- sem_init(&semBarbers, 0, 0);
- //sem_init(&semChairs, 0, 0);
- // Создание потоков
- pthread_t threadBarber[COUNT_BARBER], threadCustomer[COUNT_CUSTOMER];
- pthread_create(&threadBarber[1], NULL, &BarberFunction, NULL);
- for (int i = 0; i < COUNT_CUSTOMER; i++)
- {
- int come = rand() % (COME_TIME_MAX - COME_TIME_MIN) + COME_TIME_MIN;
- usleep(come);
- pthread_create(&threadCustomer[i], NULL, &CustomerFunction, NULL);
- }
- // Завершение потоков
- for (int i = 0; i < COUNT_CUSTOMER; i++)
- pthread_join(threadCustomer[i], NULL);
- pthread_cancel(threadBarber[1]);
- // Уничтожение семафоров и мьютексов
- sem_destroy(&semChairs);
- sem_destroy(&semBarbers);
- sem_destroy(&semCustomers);
- pthread_mutex_destroy(&mutexInc);
- pthread_mutex_destroy(&mutexEnter);
- DestrouWriteMutex();
- std::cout << '\n';
- if (RESULT == COUNT_CUSTOMER)
- WriteCorrect(RESULT, "клентов побрито");
- else if (RESULT == 0)
- WriteError(RESULT, "клентов побрито");
- else
- WriteWarning(RESULT, "клентов побрито");
- return 0;
- }
- void* BarberFunction(void*)
- {
- /*while (true)
- {
- if (sem_trywait(&semChairs) != 0)
- {
- WriteInfo((int)pthread_self(), "Брадобрей засыпает");
- sem_wait(&semChairs);
- WriteInfo((int)pthread_self(), "Брадобрей просыпается");
- }
- WriteInfo((int)pthread_self(), "Брадобрей бреет");
- int cut = rand() % (CUT_TIME_MAX - CUT_TIME_MIN) + CUT_TIME_MIN;
- usleep(cut);
- WriteInfo((int)pthread_self(), "Брадобрей закончил");
- sem_post(&semBarbers);
- sem_post(&semCustomers);
- }*/
- while(_end != -1)
- {
- sem_wait(&semCustomers);
- COUNT_WAITING--;
- WriteInfo((int)pthread_self(), "Брадобрей бреет");
- int cut = rand() % (CUT_TIME_MAX - CUT_TIME_MIN) + CUT_TIME_MIN;
- usleep(cut);
- sem_post(&semBarbers);
- }
- return NULL;
- }
- void* CustomerFunction(void*)
- {
- /*
- pthread_mutex_lock(&mutexEnter);
- int come = rand() % (COME_TIME_MAX - COME_TIME_MIN) + COME_TIME_MIN;
- usleep(come);
- pthread_mutex_unlock(&mutexEnter);
- WriteWarning((int)pthread_self(), "Клиент пришел");
- if (sem_trywait(&semCustomers) != 0)
- {
- WriteError((int)pthread_self(), "Клиент ушел небритый");
- }
- else
- {
- sem_post(&semChairs);
- sem_wait(&semBarbers);
- pthread_mutex_lock(&mutexInc);
- RESULT++;
- pthread_mutex_unlock(&mutexInc);
- WriteCorrect((int)pthread_self(), "Клиент ушел побритый");
- }*/
- _end++;
- WriteWarning((int)pthread_self(), "Клиент пришел");
- if(COUNT_WAITING < COUNT_CHAIR)
- {
- COUNT_WAITING++;
- WriteWarning((int)pthread_self(), "Клиент ждёт");
- sem_post(&semCustomers);
- sem_wait(&semBarbers);
- WriteWarning((int)pthread_self(), "Клиент ушёл побритый");
- RESULT++;
- }
- else
- {
- WriteError((int)pthread_self(), "Клиент ушел небритый");
- }
- _end--;
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement