From f1b6435b7e8fc20d76492cd644f75b6835e2ad56 Mon Sep 17 00:00:00 2001 From: Ceccarelli Luca Date: Tue, 17 Oct 2023 20:09:06 +0200 Subject: [PATCH] changing the problem to a car wash problem --- main.cpp | 86 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/main.cpp b/main.cpp index a44401f..7b15d69 100644 --- a/main.cpp +++ b/main.cpp @@ -12,79 +12,79 @@ mutex cout_mutex; #define N 25 // GLOBAL VARIABLES -int customers = 0; -mutex mutexCustomer; -counting_semaphore customer(0); -counting_semaphore barber(0); -counting_semaphore customerDone(0); -counting_semaphore barberDone(0); +int carsWaiting = 0; +mutex mutexCar; +counting_semaphore carArrived(0); +counting_semaphore bayFree(0); +counting_semaphore carWashed(0); +counting_semaphore washingDone(0); -void client(size_t id) +void car(size_t id) { - // customer enters - print("Customer " << id << " enters the barbershop"); - mutexCustomer.lock(); - if (customers == N) { - mutexCustomer.unlock(); - print("Customer " << id << " bulks"); + // car arrives + print("Car " << id << " arrives at the car wash"); + mutexCar.lock(); + if (carsWaiting == N) { + mutexCar.unlock(); + print("Car " << id << " leaves due to no waiting space"); return; } - ++customers; - mutexCustomer.unlock(); + ++carsWaiting; + mutexCar.unlock(); // rendezvous 1 - customer.release(); // signal X - barber.acquire(); // wait Y + carArrived.release(); // signal X + bayFree.acquire(); // wait Y - // getHairCut - print("Customer " << id << " get his hair cut"); + // getCarWash + print("Car " << id << " is being washed"); this_thread::sleep_for(chrono::seconds(1)); - print("Customer " << id << " got a haircut"); + print("Car " << id << " is washed"); // rendezvous 2 - customerDone.release(); // signal X' - barberDone.acquire(); // wait Y' + carWashed.release(); // signal X' + washingDone.acquire(); // wait Y' - // customer leaves - mutexCustomer.lock(); - --customers; - mutexCustomer.unlock(); - print("Customer " << id << " leaves the barbershop"); + // car leaves + mutexCar.lock(); + --carsWaiting; + mutexCar.unlock(); + print("Car " << id << " leaves the car wash"); } -void barber() +void attendant() { while (true) { // rendezvous 1 - customer.acquire(); // wait X - barber.release(); // signal Y + carArrived.acquire(); // wait X + bayFree.release(); // signal Y - // cutHair - print("Barber starts to cut hair"); + // washCar + print("Attendant starts washing a car"); this_thread::sleep_for(chrono::seconds(1)); - print("Barber stops to cut hair"); + print("Attendant finishes washing"); // rendezvous 2 - customerDone.acquire(); // wait X' - barberDone.release(); // signal Y' + carWashed.acquire(); // wait X' + washingDone.release(); // signal Y' } } int main() { - print("Barbershop opening with " << N << " chairs"); - thread barberThread(barberProcedure); - vector clients {}; + print("Car wash opening with " << N << " waiting spaces"); + thread attendantThread(attendant); + vector cars {}; for(size_t i = 0; i < N+1; ++i) { - thread t(client, i); - clients.push_back(std::move(t)); + thread t(car, i); + cars.push_back(std::move(t)); } - barber_thread.join(); - for (auto &client: clients) { - client.join(); + attendantThread.join(); + for (auto &car: cars) { + car.join(); } return 0;