Question: In this assignment, you will implement the solution (the one that favors readers over writers) for the Readers-Writers problem. Your implementation must be in C++
In this assignment, you will implement the solution (the one that favors readers over writers) for the Readers-Writers problem. Your implementation must be in C++ only, in a single file named readWrite.cpp.
The database that the readers and writers access consists of a global array x[2] of integers initialized to 0. Each of x[0] and x[1] can be accessed independently, and so must be locked/unlocked independently. The readers and writers are threads (not separate processes). They MUST be implemented using the C++11 library.
Start with the following:
#include
#include
#include
#include
#include
using namespace std;
const double FACTOR 0.000000001;
Then, declare the following global variables: int x[2]={0, 0}, int readCount[2]={0, 0}, and mutexes (binary semaphores) rcMutex[2] and dbMutex[2]. Follow this with the function prototype declarations.
The program will be executed using the command: a.out int1 int2 int3, where int1, int2, and int3 are 3 positive integers. int2 and int3 are the number of reader and writer threads, respectively. int1 is the sleeptime.
The program consists of only three functions: main, reader and writer.
The main function does the following, in that order:
Error check for number of arguments, and their values. Assign them to appropriate variables.
Create the correct number of reader threads, in a for loop. Each reader thread starts its execution in the reader function, and is passed only one parameter: The thread number (between 1 and int2).
Create the correct number of writer threads, in a for loop. Each writer thread starts its execution in the writer function, and is passed only one parameter: The thread number (between 1 and int3).
Sleep for sleeptime and then terminate. NO CALL TO join.
The reader function does the following in its while loop:
Sleep for a random amount of time: int y = rand()*FACTOR; sleep(y);
Get eltNum = rand() % 2.
Access x[eltNum] as in Figure 5.12 (page 221). Print the thread number and the value of x[eltNum], both labeled appropriately.
The writer function does the following in its while loop:
Sleep for a random amount of time.
Get eltNum = rand() % 2.
Access x[eltNum] as in Figure 5.11 (page 221); set x[eltNum] to the thread number. Print the thread number and the value of x[eltNum], both labeled appropriately
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
