Question: I have c++ code but its not working. what is wrong with the code? program suppose to: create a c++ program with parallel process that

I have c++ code but its not working. what is wrong with the code? program suppose to:

create a c++ program with parallel process that uses a ring buffer. the parent process should create the semaphore set and segment and start the children process, then the process should back off and wait until other is finished.

another process shall be created for user input from the keyboard and write them to the ring buffer, also another process shall be created to generate text and put it on the ring buffer. another process shall be created to read from the ring buffer and write it to the screen. all necessary resources must be synchronized.

#include #include #include #include #include #include

template class RingBuffer { private: std::vector mObjects; std::atomic mHead; // atomic head index std::atomic mTail; // atomic tail index std::mutex mMutex; // mutex to protect the buffer std::condition_variable_any mReadConditionVariable; // condition variable for read std::condition_variable_any mWriteConditionVariable; // condition variable for write

public: RingBuffer(size_t size) : mObjects(size), mHead(0), mTail(0) { }

bool IsEmpty() const { return (mHead == mTail); }

bool IsFull() const { return ((mTail + 1) % mObjects.size() == mHead); }

void Put(const T& object) { std::unique_lock lock(mMutex); mWriteConditionVariable.wait(lock, [&] {return !this->IsFull(); });

mObjects[mTail] = object; mTail = (mTail + 1) % mObjects.size();

mReadConditionVariable.notify_one(); }

T Get() { std::unique_lock lock(mMutex); mReadConditionVariable.wait(lock, [&] {return !this->IsEmpty(); });

T result = mObjects[mHead]; mHead = (mHead + 1) % mObjects.size();

mWriteConditionVariable.notify_one();

return result; } };

void keyboardThread(RingBuffer& buffer, int sleepTime, std::stop_token stopToken) { while (!stopToken.stop_requested()) { char input; std::cin >> input; buffer.Put(input); std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); } }

void writeThread(RingBuffer& buffer, const char* text, int sleepTime, std::stop_token stopToken) { const size_t textLength = strlen(text); size_t index = 0;

while (!stopToken.stop_requested()) { buffer.Put(text[index]); ++index; if (index == textLength) { index = 0; } std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); } }

void readThread(RingBuffer& buffer, int sleepTime, std::stop_token stopToken) { while (!stopToken.stop_requested()) { std::cout << buffer.Get() << std::flush; std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); } }

int main() { RingBuffer buffer(3);

// start the write, read, and keyboard threads std::jthread number_writer(writeThread, std::ref(buffer), "0123456789", 500, std::stop_token()); std::jthread reader_thread(readThread, std::ref(buffer), 1000, std::stop_token()); std::jthread keyboard_thread(keyboardThread, std::ref(buffer), 500, std::stop_token());

// wait for the keyboard thread to finish keyboard_thread.join();

// request that the read thread stop reader_thread.request_stop();

// wait for the read and write threads to finish reader_thread.join(); number_writer.join();

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!