After Hours Academic

Question 7: Single access blog

Jesse has built an blog hosting service where user's can write and read blogs. Jesse wants to guarantee that a recruiter will always see the most recent and consistent (i.e., not an in-progress) version of a blog. That is, Jesse wants to avoid scenario wherein Gus is editing his blog and Hank sees Gus's half-edited blog. So Jesse only allows a single user to access any given blog at any given point of time. He implemented this by using a lock for each blog that a user has to acquire before accessing the blog. Jesse's boss, Walter, is not too happy with this solution because this also disallows two users to read a blog simultaneously.

Can you suggest a mechanism that Jesse can use to address Walter's concern?

Solution

Instead of using a generic lock (which any user has to acquire to access the blog), Jesse should use a reader-writer lock. A reader writer lock allows multiple readers to access a resource simultaneously. But it enforces that only a single writer (i.e., no other readers or writers) can access the resource if it holds a reader-writer lock. So, when a user wants to update their blog, they can acquire the reader-writer lock as a writer. This would ensure that no one can read the blog while it is being modified. Once the update is done, the user can release the lock and other users (e.g., multiple readers) can acquire it as a reader and access the blog simultaneously.

#computer-science #concurrency #qna