The Sleeping-Barber Problem. A barbershop consists of a waiting room with n chai
ID: 3690490 • Letter: T
Question
The Sleeping-Barber Problem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but the chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a C++ program using threads to coordinate the barber and the the customer.
Explanation / Answer
i done it two files using sourcefile and header file.
so i m putting two seperate codes for both the filed.while you executing program
1st one save as .cpp format
2nd save as .h format
1st source.cpp
#include "1.h"
using namespace Utilities;
using namespace Synchronization;
Barber::Barber(const std::string &name, Salon *salon) : MyObject(name)
{
this->salon = salon;
(new Thread(this))->start();
}
void Barber::run()
{
while (true)
{
salon->wantToCut();
}
}
Customer::Customer(const std::string &name, int id, int cutTime, int growTime, Salon *salon) : MyObject(name + " " + id)
{
this->id = id;
this->salon = salon;
this->cutTime = cutTime;
this->growTime = growTime;
(new Thread(this))->start();
}
void Customer::run()
{
int napping;
while (true)
{
napping = 1 + static_cast<int>(random(growTime));
std::cout << std::string("age=") << age() << std::string(", ") << getName() << std::string(" growing hair ") << napping << std::string("ms") << std::endl;
nap(napping);
std::cout << std::string("age=") << age() << std::string(", ") << getName() << std::string(" needs a haircut") << std::endl;
salon->wantHairCut(id, cutTime);
}
}
Salon::Salon(const std::string &name, int numChairs) : MyObject(name)
{
this->numChairs = numChairs;
customers = new CountingSemaphore(0);
barber = new CountingSemaphore(0);
mutex = new BinarySemaphore(1);
cutting = new CountingSemaphore(0);
}
void Salon::wantToCut()
{
std::cout << std::string("age=") << age() << std::string(", Barber free, waiting for a customer") << std::endl;
P(customers);
P(mutex);
waiting--;
V(barber);
std::cout << std::string("age=") << age() << std::string(", Barber has customer, waiting=") << waiting << std::endl;
V(mutex);
std::cout << std::string("age=") << age() << std::string(", Barber cutting hair") << std::endl;
P(cutting);
}
void Salon::wantHairCut(int i, int cutTime)
{
int napping;
P(mutex);
if (waiting < numChairs)
{
waiting++;
std::cout << std::string("age=") << age() << std::string(", Customer ") << i << std::string(" in room, waiting=") << waiting << std::endl;
V(customers);
V(mutex);
P(barber);
napping = 1 + static_cast<int>(random(cutTime));
std::cout << std::string("age=") << age() << std::string(", Customer ") << i << std::string(" getting haircut for ") << napping << std::string(" ms") << std::endl;
nap(napping);
std::cout << std::string("age=") << age() << std::string(", Customer ") << i << std::string(" finished haircut") << std::endl;
V(cutting);
}
else
{
std::cout << std::string("age=") << age() << std::string(", Customer ") << i << std::string(" room full, waiting=") << waiting << std::endl;
V(mutex);
}
}
void SleepingBarber::main(std::vector<std::string> &args)
{
// parse command line options, if any, to override defaults
GetOpt *go = new GetOpt(args, "Us:C:c:g:R:");
go->optErr = true;
std::string usage = std::string("Usage: -s numChairs -C numCustomers") + std::string(" -c cutTime -g growTime -R runTime");
int ch = -1;
int numChairs = 5;
int numCustomers = 10;
int cutTime = 2; // defaults
int growTime = 4; // in
int runTime = 60; // seconds
while ((ch = go->getopt()) != go->optEOF)
{
if (static_cast<char>(ch) == 'U')
{
std::cout << usage << std::endl;
exit(0);
}
else if (static_cast<char>(ch) == 's')
{
numChairs = go->processArg(go->optArgGet(), numChairs);
}
else if (static_cast<char>(ch) == 'C')
{
numCustomers = go->processArg(go->optArgGet(), numCustomers);
}
else if (static_cast<char>(ch) == 'c')
{
cutTime = go->processArg(go->optArgGet(), cutTime);
}
else if (static_cast<char>(ch) == 'g')
{
growTime = go->processArg(go->optArgGet(), growTime);
}
else if (static_cast<char>(ch) == 'R')
{
runTime = go->processArg(go->optArgGet(), runTime);
}
else
{
System::err::println(usage);
exit(1);
}
}
std::cout << std::string("SleepingBarber: numChairs=") << numChairs << std::string(", numCustomers=") << numCustomers << std::string(", cutTime=") << cutTime << std::string(", growTime=") << growTime << std::string(", runTime=") << runTime << std::endl;
// create the Salon object
Salon *salon = new Salon("Salon", numChairs);
new Barber("Barber", salon); // create the Barber
// create the Customers (self-starting thread)
for (int i = 0; i < numCustomers; i++)
{
new Customer("Customer", i, cutTime*1000, growTime*1000, salon);
}
std::cout << std::string("All Customer threads started") << std::endl;
// let the Customers run for a while
nap(runTime*1000);
std::cout << std::string("age()=") << age() << std::string(", time to stop the Customers and exit") << std::endl;
exit(0);
}
2nd source.h
#pragma once
#include <string>
#include <vector>
#include <iostream>
class Salon;
using namespace Utilities;
using namespace Synchronization;
class Barber : public MyObject, public Runnable
{
private:
Salon *salon = nullptr;
public:
Barber(const std::string &name, Salon *salon);
virtual void run();
};
class Customer : public MyObject, public Runnable
{
private:
int id = 0;
Salon *salon = nullptr;
int cutTime = 0;
int growTime = 0;
public:
Customer(const std::string &name, int id, int cutTime, int growTime, Salon *salon);
virtual void run();
};
class Salon : public MyObject
{
private:
int numChairs = 0;
CountingSemaphore *customers = nullptr;
CountingSemaphore *barber = nullptr;
BinarySemaphore *mutex = nullptr;
CountingSemaphore *cutting = nullptr;
int waiting = 0;
public:
Salon(const std::string &name, int numChairs);
virtual void wantToCut();
virtual void wantHairCut(int i, int cutTime);
};
class SleepingBarber : public MyObject
{
static void main(std::vector<std::string> &args);
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.