Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Broken Sleeping Barber Code - C++ We've been given a version of the sleeping barber problem but the code doesn't work, the task is to

Broken Sleeping Barber Code - C++

We've been given a version of the sleeping barber problem but the code doesn't work, the task is to make changes so that the bottom cout will print. We can only make changes under the portion below the part that says "ONLY MAKE CHANGES BELOW THIS LINE" the rest is included for context.

/***************************************************

General Includes

****************************************************/ #include #include #include #include #include #include

/***************************************************

Namespaces

****************************************************/ using namespace std;

/***************************************************

Type Definitions

****************************************************/ /* Semaphores */ 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 condition_; unsigned long count_ = 0; // Initialized as locked.

public: void signal() { std::lock_guard lock(mutex_); ++count_; condition_.notify_one(); }

void wait() { std::unique_lock lock(mutex_); while (!count_) // Handle spurious wake-ups. condition_.wait(lock); --count_; }

bool try_wait() { std::lock_guard lock(mutex_); if (count_) { --count_; return true; } return false; } };

/***************************************************

Environment Constants

****************************************************/ /* Environment These can change depending on how big/popular you want your barbershop to be. */ const int NUM_BARBERS = 2; const int NUM_CUSTOMERS = 4;

/***************************************************

Global Variables

****************************************************/ /* Environment These can change depending on how big/popular you want your barbershop to be. */ int numberOfFreeWaitingRoomSeats = 2;

/* Semaphores */ binary_semaphore AccessToWaitingRoomSeats; // semaphore for if you can put people in seats or take them out of seats: auto-assigned to 1 in constructor general_semaphore BarberReady, CustomerReady; // semaphore for if the barber can cut someoneone's hair and if there is a customer who's hair needs to be cut // auto assigned to 0 in constructor

void GetHairCut(int id) { string s = "The Customer " + to_string(id) + " hears Snip Snip "; cout << s; }

void CutHair(int id) { string s = "The Barber " + to_string(id) + " went Snip Snip "; cout << s; }

/***************************************************

ONLY MAKE CHANGES BELOW THIS LINE

****************************************************/

void Barber(int thread_num) { while (true) { CustomerReady.wait(); AccessToWaitingRoomSeats.wait(); numberOfFreeWaitingRoomSeats += 1; BarberReady.signal(); AccessToWaitingRoomSeats.signal(); CutHair(thread_num); } }

void Customer(int thread_num) { AccessToWaitingRoomSeats.wait(); if (numberOfFreeWaitingRoomSeats > 0) { numberOfFreeWaitingRoomSeats -= 1; CustomerReady.signal(); AccessToWaitingRoomSeats.signal(); BarberReady.wait(); GetHairCut(thread_num); } else { //no space, must leave! AccessToWaitingRoomSeats.signal(); } }

int main() { int threadnum = NUM_BARBERS + NUM_CUSTOMERS; thread threads[NUM_BARBERS + NUM_CUSTOMERS];

string s = "Running " + to_string(threadnum) + " threads in parallel: "; cout << s;

/* spawn Barber threads */ for (int id = 0; id < NUM_BARBERS; id++) threads[id] = thread(Barber, id);

/* spawn Customer threads */ for (int id = 0; id < NUM_CUSTOMERS; id++) threads[id + NUM_BARBERS] = thread(Customer, id + NUM_BARBERS);

/* Merge all threads to the main thread */ for (int id = 0; id < threadnum; id++) threads[id].join();

// WHY ISN'T THIS PRINTING? cout << "Completed barbershop example! "; cout << endl;

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

3319234609, 978-3319234601

More Books

Students also viewed these Databases questions