The sleeping barbers have fallen so deeply asleep that their program no longer functions! The good news is the "heavy lifting" is done in that the code is provided and follows the code trace you...

1 answer below »

The sleeping barbers have fallen so deeply asleep that their program no longer functions! The good news is the "heavy lifting" is done in that the code is provided and follows the code trace you worked previously with binary and general semaphores. The bad news is it is not running to completion :(


Adjust the code so the 2-barber, 4-customer setup runs to completion as expectedunder any interleaving. To help, you are shown an area -- ONLY MAKE CHANGES BELOW THIS LINE -- which helps show you where we expect the fix to be happening. (And nothing should be changed above that line - duh.)



SleepingBarberBrokenInterleavingV1.cpp

Preview the document



If you need a Makefile, this should work (or at least worked for me on campus machines!). For anyone not familiar with Makefiles, just put this file in the same directory as the source code file and enter 'make' into the terminal while in that directory. This will compile the program and create an executable named program in the directory (if it builds correctly)Makefile


Please upload ONLY the working .cpp file (file extensions are restricted). Feel free to add comments that document your fix!

Answered Same DayOct 25, 2021

Answer To: The sleeping barbers have fallen so deeply asleep that their program no longer functions! The good...

Arun Shankar answered on Oct 28 2021
135 Votes
#include
#include
#include
#include
#include
#
include
using namespace std;
class binary_semaphore
{
public:
explicit binary_semaphore(int init_count = count_max)
: count_(init_count) {}
// P-operation / acquire
void wait()
{
std::unique_lock lk(m_);
cv_.wait(lk, [=] { return 0 < count_; });
--count_;
}
bool try_wait()
{
std::lock_guard lk(m_);
if (0 < count_) {
--count_;
return true;
}
else {
return false;
}
}
// V-operation / release
void signal()
{
std::lock_guard lk(m_);
if (count_ < count_max) {
++count_;
cv_.notify_one();
}
}
// Lockable requirements
void lock() { wait(); }
bool try_lock() { return try_wait(); }
void unlock() { signal(); }
private:
static const int count_max = 1;
int count_;
std::mutex m_;
std::condition_variable cv_;
};
class general_semaphore
{
private:
std::mutex mutex_;
std::condition_variable...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here