Semaphore: Block = 1, ReadMutex = 1, ReadCountMutex = 1, WriteMutex = 1, WriteCountMutex = 1,
A reader process has the following form:
while { Wait(Block); Wait(ReadMutex); Wait(ReadCountMutex); ReadCount++; if (ReadCount == 1) Wait(WriteMutex); Signal(ReadCountMutex); Signal(ReadMutex); Signal(Block); ..... read the database ..... Wait(ReadCountMutex); ReadCount--; if (ReadCount == 0) Signal(WriteMutex); Signal(ReadCountMutex); }
A writer process has the following form:
The initial values of ReadCount and WriteCount are both 0. Please study this solution and answer the following questions:while { Wait(WriteCountMutex); WriteCount++; if (WriteCount == 1) Wait(ReadMutex); Signal(WriteCountMutex); Wait(WriteMutex); ..... write the database ..... Signal(WriteMutex); Wait(WriteCountMutex); WriteCount--; if (WriteCount == 0) Signal(ReadMutex); Signal(WriteCountMutex); }
to the following?while { Wait(Block); Wait(ReadMutex); Wait(ReadCountMutex); ReadCount++; if (ReadCount == 1) Wait(WriteMutex); Signal(ReadCountMutex); Signal(ReadMutex); Signal(Block); ............... }
Removing Block is the most natural suggestion, since it is used in the reader process and is used exactly once. Explain why you cannot do this. If Block is removed, what will happen to the solution.while { Wait(ReadMutex); Wait(ReadCountMutex); ReadCount++; if (ReadCount == 1) Wait(WriteMutex); Signal(ReadCountMutex); Signal(ReadMutex); ..... read the database ..... Wait(ReadCountMutex); ReadCount--; if (ReadCount == 0) Signal(WriteMutex); Signal(ReadCountMutex); }
You do not have to turn in your paper. What I really expect you to do is using these problems to gauge your understanding of the subject. So, do the problems after finish reading the above sections. |