Write a program in C/C++ to implement reader- writer problem

Answer Posted / pranaybaldi

Readers-Writers Problem:
The readers-writers problem is a classic synchronization
problem in which two
distinct classes of threads exist, reader and writer.
Multiple reader threads
can be present in the Database simultaneously. However, the
writer threads must
have exclusive access. That is, no other writer thread, nor
any reader thread,
may be present in the Database while a given writer thread
is present. Note:
the reader/writer thread must call startRead()/startWrite()
to enter the
Database and it must call endRead()/endWrite() to exit the
Database.
class Database extends Object {
private int numReaders = 0;
private int numWriters = 0;
private ConditionVariable OKtoRead = new ConditionVariable
();
private ConditionVariable OKtoWrite = new ConditionVariable
();
public synchronized void startRead() {
while (numWriters > 0)
wait(OKtoRead);
numReaders++;
}
public synchronized void endRead() {
numReaders--;
notify(OKtoWrite);
}
public synchronized void startWrite() {
while (numReaders > 0 || numWriters > 0)
wait(OKtoWrite);
numWriters++;
}
public synchronized void endWrite() {
numWriters--;
notify(OKtoWrite);
notifyAll(OKtoRead);
}
}
class Reader extends Object implements Runnable {
private Monitor m = null;
public Reader(Monitor m) {
this.m = m;
new Thread(this).start();
}
public void run() {
//do something;
m.startRead();
//do some reading…
m.endRead();
// do something else for a long time;
}
}
class Writer extends Object implements Runnable {
private Monitor m = null;
public Writer(Monitor m) {
this.m = m;
new Thread(this).start();
}
public void run() {
//do something;
m.startWrite();
//do some writing…
m.endWrite();
// do something else for a long time;
}
}
- 2 -
wait(ConditionVariable cond) {
put the calling thread on the “wait set” of cond;
release lock;
Thread.currentThread.suspend();
acquire lock;
}
notify(ConditionVariable cond){
choose t from wait set of cond;
t.resume();
}
notifyAll(ConditionVariable cond){
forall t in wait set of cond;
t.resume()
}
For the following scenarios, we assume that only one reader
thread and one writer
thread are running on the processor.

Is This Answer Correct ?    17 Yes 43 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

In what scenario does the Logical file and Physical file being used?

2328


What is stl language?

682


What is stl in oop?

684


i wanted to know about questions about c,c++ , which is required for placements.... im a fresher

1702


a program using one dimensional array that searches a number if it is found on the list of given input numbers given by the user and locate its exact location in the list.

1383






how to use C++?

2067


What are the symptoms of stl?

609


help me i need a c++ program which takes sequesnce of characters and outputed sequence of their token taypes, work same compiler in lexical analysis phase

1886


Is there any error below, its a code to delete all entires from a map #include #include iostream.h int main() { int i =0; map TestMap; while(i<3) { TesMap.insert(TestMap::value_type(i,Test)); i++; } typedef map :: iterator mapIter =TestMap.begin(); if(mapIter!=TestMap.end()) { TestMap.erase(mapItrer); ++mapIter; } return 0; }

1896


What are the components of stl?

628


What is the stl, standard template library?

600


What is a standard template library (stl)? What are the various types of stl containers?

674


When did c++ add stl?

742


What are the various types of stl containers?

739


Can we use stl in coding interviews?

1284