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



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

Answer / 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

More STL Interview Questions

What is stl stand for?

0 Answers  


sir please send me bpcl previous question papers

0 Answers   BPCL Bharat Petroleum,


totoo po ba ang manga aliens!

0 Answers  


write a program to convert a decimal number in to its equivalent binary number?

0 Answers  


give me the defination of inheritance?

5 Answers   Infosys,






how can u do connectivity in c++ language? plz send me connectivity code in c++ ?

0 Answers   Ascent,


What are the various types of stl containers?

0 Answers  


what is the difference between thread and process

1 Answers   Infosys,


Give the output of the following program main() {char *p='a'; int *i=100/*p; } what will be the value of *i= 1

6 Answers   Sun Microsystems,


How do I convert a stl file?

0 Answers  


what is the acronym of the term 'C.O.M.P.U.T.E.R' ?

17 Answers   Config Systems, Google, Wipro,


Describe how to safeguard a system through acquisition of an antivirus Program and systematic backup.

0 Answers  


Categories