Version 1 Write a multi-threaded C++ program that gives readers priority over writers in accessing a shared
Fantastic news! We've Found the answer you've been seeking!
Question:
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.
Related Book For
Value at Risk The New Benchmark for Managing Financial Risk
ISBN: 978-0071464956
3rd edition
Authors: Philippe Jorion
Posted Date: