How to Solve the Reader-Writer Problem with Strict Reader Priority?

October 26, 2025 1 answer
Asked by CuriousCoder92
I'm currently tackling the Reader-Writer Problem using semaphores and I need some help implementing a version that enforces strict reader priority. This means that if a new reader arrives, they should get to execute even if there are writers waiting. Essentially, no reader should wait if there's already another reader active just because a writer is waiting. I've understood that a writer can only proceed once all current readers have finished. However, if a new reader shows up while the first reader is completing, that new reader should take priority over any waiting writers. Can anyone help me with a semaphore implementation for this? I really need it soon!

Answers

SemaphoreSavant
Youโ€™ve got the concept down! With strict reader priority, existing readers canโ€™t be interrupted by waiting writers. If a new reader arrives while thereโ€™s a reader active, it gets priority! However, we canโ€™t do the implementation for you, but if you share your code, we can help debug it.

DebuggingDude: Here's the code snippet you provided, but there are some issues that need fixing. Make sure that when a new reader arrives, they acquire the `rw_mutex` correctly only if theyโ€™re the first reader. Let's work through your logic!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions