I need some help understanding the implementation of the one-writer many-readers problem shown below as I am new to this concept:
I have a rough idea that there will be starvation/deadlock among the readers as there is one writer that multiple readers are trying to access at once. Can anyone please provide some further insight regarding the problems with the implementation of this situation?
I am not looking for any coding, that was just the sample code (pseudocode) I am working with. I am looking for the problem of the implementation. Upon further research I have assumed the following (based on the line: if readcount == 1 then semWait(wrt); :
- There is one writer and many readers
- The readers have priority over the writers
- The possible problem: The writers cannot write until the reader has finished reading therefore starvation occurs
However upon re-evaluation of the code I have also assumed the following based on the lines:
semWait(mutex); readcount := readcount - 1; if readcount == 0 then up semSignal(mutex); Could I not also say that only one reader may read at a time therefore the other readers will be starved?
Therefore would either of these be the correct way of interpreting the coding to the problem is of the implementation or would I be wrong?
Asked By : Osiris93
Answered By : Ariel
First,I would suggest (in the future as well) to write the pseudocode yourself (the commands for the readers/writers), It will be cleaner and easier to read than the image. Also, i would suggest briefly explaining the semantics of your operations (wait/signal/up, since you used all three in the code).
To your answer, readers not being able to read simultaneously does not mean starvation, although simultaneous reading ability is an additional requirement, at least in the classical formulation of the problem (moreover, readers can read simultaneously in this protocol). Starvation means that there exists a process stuck waiting while other processes share the resource (if one reader $r_1$ has to wait until $r_2$ is done, then he might read after $r_2$, who eventually will be done reading, so $r_1$ is not stuck).
This protocol (as it seems to me) is the same as the one described here (under First readers-writers problem). It is easy to see (as mentioned in the link) that writers can be starved here. Suppose $r_1,r_2$ are reading at the same time, so the count is $2$. Imagine a scenario where whenever $r_1,r_2$ read together, the first to finish (and decrements the counter to $1$) immediately requests reading again (so the counter is back to $2$). This way the counter will never reach $0$ and the writer will be starved (stuck waiting).
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/45804
0 comments:
Post a Comment
Let us know your responses and feedback