The Readers-Writers Problem
Reader/writer problem
- when writers are writing, only one writer can access
- When readers are reading, as many readers can access as long as no writers are writing.
To implement:
- you can only write if no one is reading or writing
- when you read, make sure you grab the semaphore. (But if there’s another thread reading, then you don’t have to acquire anything).
- Use a read count counter to know who to up / down semaphore. Avoids race condition on read_count
- Writers don’t need a mutex because they don’t care about read count.
Shared bounded queue or “bounded buffer: a set of buffered shared by producer (inserts into buffer) and consumer (removes resources)threads.
- producer and consumer execute at different rates
- nc is number consumed, np is number produced, N is the size of the buffer:
Implementation:
- requires three semaphores
- empty: counts the number of empty buffers or “slots”
- Empty = (nc - np) + N
- full: counts the number of full buffered or “items”
- Full = np - nc
- Mutex: mutual exclusion to shared set of buffer. Use a binary semaphore
- empty: counts the number of empty buffers or “slots”
Linked from Operating Systems