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

how to swap two numbers in a linked list without exchanging the data but only the links?

3 Answers   Wipro,


What do stl stand for?

0 Answers  


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

0 Answers  


what are you now programming Languages C+

1 Answers   HCL,


Question 1)Read the data from one file and remove all the duplicated words and generate an output file containg only using words.Program should also print number of occurance of each words on standard output.(Program must be use STL and iostream object). Question 2)Write a program to convert the lower case contents of file to upper case using STL. Question 3)What is the output of this problem. int i=12; int &r =i; r+r/4; int +p =&r; int *p = &r; P+=r; return 1; Answer :a)12 b)17 c) 30 d)24 E)15 Question 4) #include #include #include void main() { char srcstr[30],desstr[30]; int i,len; clrscr(); cout<<"\nenter the string\n"; cin>>srcstr; len=strlen(srcstr); for(i=0;srcstr[i]!='\0';i++) { desstr[--len]=srcstr[i]; } desstr[i]='\0'; cout<<"\nreversed string is\n"; for(i=0;desstr[i]!='\0';i++) { cout< } getch(); } Answer : A) string output b)tuptuo gnirts c) string d)output Question 5) Class Test { static const Tk=LEN; int q; public: foo(intx =LEN):q(x){}; TGet Q() const { return q:} void call test Foo<long :9> foo(10); std::const<< foo.Getq(); Answer: 7,8,9,10,11 question 6) class A { int a; char b; }; class B:public A { char b; int a; }; What is the size? Answer a)5 bytes for class A and 5 bytes class B b)4 bytes for class A and 8 bytes class B. c).... d)....

2 Answers   Bally Technologies,






What is 2*2?

7 Answers  


write a program to demonstrate,how constructor and deconstructor work under multilevel inheritance

3 Answers   TCS,


Why should a c++ programmer be interested in stl?

0 Answers  


What is the STL?

2 Answers   Epson, HP,


What is the Difference between CArray and CList?

1 Answers   ProdEx Technologies, Siemens,


5. Write c++ function that would intake a string and return the number of occurrences of a given character in that sring Ex:- if the word is “Colombo” and count the occurrences of the letter “o” the function would return 3

1 Answers  


why we are using the fork command?.. how it works?

1 Answers  


Categories