Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

how is suppose to look is, when you run the programm, it keeps writing, 0123456789 but when you type from keyboard, for example: im using chegg, and hit enter, the aplhabate and numbers start mixing and can come in any ways like, 0i12m345 us6i78n9g0 and on...

i have an incomplete code, below. it need to be complete, with screenshot of the output.

.....................................................................................................

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

template class RingBuffer { private: std::vector mObjects; std::atomic mHead; std::atomic mTail; std::mutex mMutex; std::condition_variable_any mReadConditionVariable; std::condition_variable_any mWriteConditionVariable;

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);

std::jthread number_writer( std::bind(writeThread, std::ref(buffer), "0123456789", 500, std::placeholders::_1)); std::jthread reader_thread( std::bind(readThread, std::ref(buffer), 1000, std::placeholders::_1)); std::jthread keyboard_thread( std::bind(keyboardThread, std::ref(buffer), 500, std::placeholders::_1));

keyboard_thread.join();

reader_thread.request_stop(); reader_thread.join();

number_writer.request_stop();

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

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

Ty e2y Evaluate the integral dy

Answered: 1 week ago

Question

LO1 Summarize the organizations strategic planning process.

Answered: 1 week ago