# Understanding the one-writer, many-readers problem?

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);
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?

• Please don't use images as main content of your post. This makes your question impossible to search and inaccessible to the visually impaired; we don't like that. Please transcribe text and mathematics (note that you can use LaTeX) and don't forget to give proper attribution to your sources!
– D.W.
Sep 2 '15 at 20:52
• As far as the question: What are your thoughts? What possible problems have you considered, and what are your thoughts on whether they are or aren't present in this code? "provide further insight regarding the problems with this code" is a bit open-ended/vague. The more specific you can make your question, and the more you show us about what work you've done so far, the more likely that you will get helpful answers.
– D.W.
Sep 2 '15 at 20:54
• @D.W. I have edited the details in my question to meet some of the points you brought up in your first comment. I do apologise for using an image but I could not copy and paste the code as it was presented to me as an image already. Sep 2 '15 at 21:06
• Be specific about what you are asking. Do you want to know if the protocol you have described is correct? (i.e. satisfies the requirements of priority and not allowing simultaneous read/write). If so, note that even the simplest protocol where no one gets to read/write is correct. Are you asking whether your protocol is correct and is deadlock,starvation free? Note that formal proofs might be trickier than you expect. Sep 2 '15 at 21:29
• @Ariel The only information I was given about this coding sample was: "Explain the problem with implementation of the one-writer many-readers problem". That's all, the rest is a bit of a mystery to me, but based on the information it implies there is definitely a problem. Based on what I understand by the coding, I see it as many readers have priority over one writer and I am assuming that the writer may be starved. But I am not sure, it is all a guess based on what I know and the research I have found. What I'm looking for here is validation or correction on my assumption Sep 2 '15 at 21:35

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).