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 Object Oriental Progam

2 Answers  


what is template and type convertion

0 Answers   TATA,


To modify an, existing worksheet. What steps are involved for: 1. Inserting and deleting rows and columns. 2. Printing cell formulas 3Jld displayed values 3. Using the page setup command

0 Answers  


What is stl in oop?

0 Answers  


totoo po ba ang manga aliens!

0 Answers  






Name the different types of stl containers.

0 Answers  


what is an algorithm in terms of STL?

1 Answers   Lucent, Wipro,


what is a template?

2 Answers   Amazon, BITS, IBS, Wipro,


write a c++ program to create an object of a class called employee containing the employee code name designation basic salarry HRA Da gross salary as data 10 such objects "members process "

1 Answers  


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

0 Answers  


What is the Difference between CArray and CList?

1 Answers   ProdEx Technologies, Siemens,


Define the terms: field, record, table and database

5 Answers  


Categories