Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Version 1 Write a multi-threaded C++ program that gives readers priority over writers in accessing a shared variable. If any readers are waiting, they

C++

Version 1 Write a multi-threaded C++ program that gives readers priority over writers in accessing a shared variable. If any readers are waiting, they have priority over writer threads. Writers can only write when there are no readers waiting. It is acceptable to have multiple processes reading the shared variable at the same time. However, if one process is updating (writing) the shared variable, no other process may have access to the shared variable.

Implementation Notes for Version 1 Use the Pthreads (POSIX threads) library for threads and mutexes. Multiple readers and writers must be supported, and you should easily be able to vary the number of readers and writers. (Using a constant for this is fine.) Readers must read the shared variable X number of times before exiting. (X can be represented as a constant in your program.) Writers must write a random integer to a shared integer variableWriters must write the shared variable X number of times before exiting. Readers must print: o the thread type (reader or writer), thread number and value read. o the number of readers present in the critical region when the value is read. Sample output reader 2 read 17 3 reader(s) Writers must print: o the thread type (reader or writer), thread number and value written. o The number of readers present in the critical region when the value is written (should be 0). Sample output writer 2 wrote 147 0 reader(s) Be sure your output is aligned in a way that is easy to read. This will make testing and debugging your program easier.Before a reader or writer attempts to access the shared variable, it should wait some random amount of time. This will help ensure that reads and writes do not occur all at once. o Use the function usleep() to put a thread to sleep. It takes one argument, a time in microseconds. I had my threads sleep randomly between 10 and 100 microseconds each time before accessing the shared variable. You will need to experiment to find random times that work for you.

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

Step: 3

blur-text-image

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

Students also viewed these Databases questions

Question

6. How do histories influence the process of identity formation?

Answered: 1 week ago