Multi-threading, concurrency, and semaphores. The following process definitions
ID: 3821431 • Letter: M
Question
Multi-threading, concurrency, and semaphores.
The following process definitions were intended to solve the reader-writer problem. they depend on two binary semaphores and a counter variable, initialized as follows:
int readcount;
semaphore mutex, wrt;
mutex=1, wrt=1, readcount=1;
Each writer process (i=1,2,3...) is coded as follows:
Writer:
begin ...
wait(wrt);
...
writing is performed
...
signal(wrt)
...
end.
Note: wait is Dijkstra's P operator and signal is Dijkstra's V operator. The dots(...) denote code that may differ from writer to writer.
Each reader process (j=1,2,3...) is coded as follows:
Reader:
begin ...
wait(mutex);
readcount++;
if(readcount==1) wait(wrt);
signal(mutex);
...
reading is performed
...
wait(mutex);
readcount--;
if(readcount==0) signal(wrt);
signal(mutex);
...
end.
Note: wait is Dijkstra's P operator and signal is Dijkstra's V operator. The dots(...) denote code that may differ from reader to reader.
Based on the above code, answer the fallowing ten questions. Explaim your answers. If a situation is possible, you need to give an example of how it is possible(e.g. when a particular semaphore/counter has a certain value and Reader2 is waiting on Reader1...). If a situation is not possible , you need to explain why(e.g. Reader1 is blocked by the wait in its code while Writer3 has not yet called signal in its code).
a) How many different Writers can access the database(i.e. perform writing) at the same time(i.e. concurently)?
b) Can a Reader access the database to perform reading when a Writer is already performing writing?
c) How many different Readers can access the database(i.e. perform reading) when Writers are neither accessing it nor requesting access to it?
d) How many different Writers can access the database when there are Readers already accessing it?
e) How many Readers can be waiting to access the database at a given time?
f) How many Writers can be waiting to access the database at the given time?
g) What happens if Readers13 calls wait(wrt) instead of wait(mutex) at the start of its code?
h) What happens if Writer7 forgets to call signal(wrt) after it completes writing to the database?
i) What is the situation when wrt=0, mutex=1, and readcount=3. In other words, exactly how many Readers and how many Writers are waiting or accessing the database?
j) What is the situation when wrt=1, mutex=0, and readcount=0. In other words, exactly how many Readers and how many Writers are waiting or accessing the database?
Explanation / Answer
There will not be any problem in the above code it can any number of processes at same time but it cannot have more than one writer at a time which arises in conflict.
a)only one writer can access the database each time to avoid concurrency conflict
b)yes beacuse in this code it is mentioned in the reader process that the writer need to wait while reader performs reading but it is not mentioned in the writer process as there is no signal for the reader to wait.
c)There is no limit for the number of readers upon accessing the database as j=1,2,3... and the staatement readcount++ clearly states that the readers can be more than 1 at a time but if there is only 1 reader then there will be wait signal for writer(as more than 1 then wrt=0) and while the reader count is decreased i.e, readcount-- it need to signal writer process.
d)0. writers cannot access until reader count is zero.
e) 0 no reader is kept waiting until the writer is accesing the database,if writer is accesing then the readers all who try to access are kept waiting until the writer finishes or it is blocked then deadlock occurs
f) 1 only 1 is kept waiting as there is no condition mentioned in the writer process
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.