1.

Write A Program In C/c++ To Implement Reader- Writer Problem?

Answer»

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 start Read()/start Write() to enter the database and it must call end Read()/end Write() to exit the database.

Class Database extends Object {

Private int numReaders = 0;

Private int numWriters = 0;

Private Condition Variable OKtoRead = new Condition Variable ();

private Condition Variable OKtoWrite = new Condition Variable ();

public synchronized void start Read() {

while (numWriters > 0)

wait(OKtoRead);

numReaders++;

}

public synchronized void end Read() {

numReaders--;

notify(OKtoWrite);

}

public synchronized void start Write() {

while (numReaders > 0 || numWriters > 0)

wait(OKtoWrite);

numWriters++;

}

public synchronized void end Write() {

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(Condition Variable cond) {

put the calling thread on the “wait set” of cond;

release LOCK;

Thread.currentThread.suspend();

acquire lock;

}

notify(Condition Variable cond){

CHOOSE t from wait set of cond;

t.resume();

}

notifyAll(Condition Variable cond){

for all 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.

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 start Read()/start Write() to enter the database and it must call end Read()/end Write() to exit the database.

Class Database extends Object {

Private int numReaders = 0;

Private int numWriters = 0;

Private Condition Variable OKtoRead = new Condition Variable ();

private Condition Variable OKtoWrite = new Condition Variable ();

public synchronized void start Read() {

while (numWriters > 0)

wait(OKtoRead);

numReaders++;

}

public synchronized void end Read() {

numReaders--;

notify(OKtoWrite);

}

public synchronized void start Write() {

while (numReaders > 0 || numWriters > 0)

wait(OKtoWrite);

numWriters++;

}

public synchronized void end Write() {

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(Condition Variable cond) {

put the calling thread on the “wait set” of cond;

release lock;

Thread.currentThread.suspend();

acquire lock;

}

notify(Condition Variable cond){

choose t from wait set of cond;

t.resume();

}

notifyAll(Condition Variable cond){

for all 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.



Discussion

No Comment Found