Master the fundamental concepts of threads & concurrency through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksReader-writer locks optimize read-heavy workloads: concurrent readers proceed in parallel, writers take exclusive access. The lock must track active reader count and block writers until readers drain.
Common implementation fields:
For example, with 8 threads parsing a config file, all 8 may hold the read lock simultaneously; a writer updating the file waits until reader_count drops to 0.
POSIX's pthread_rwlock_t and Java's ReentrantReadWriteLock exist because read-mostly workloads like configuration caches and in-memory databases waste enormous throughput serializing readers behind a plain mutex. The writer-starvation problem you're asked to prevent here is a real, documented pitfall: naive Linux glibc rwlock implementations historically favored readers so heavily that writers could starve indefinitely under sustained read load.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will build rwlock_rdlock, rwlock_wrlock, and unlock paths from pthread-style primitives or your mutex. This exercise requires a test where readers overlap and a writer serializes updates without data races.
Implement a reader-writer lock: many readers at once, or one writer alone. The interesting part is the policy for choosing who goes next when both kinds are waiting. Reader preference can starve writers, writer preference can starve readers, and a FIFO policy trades throughput for fairness. Drive the lock with an explicit sequence of operations and show every grant.
cLoading…
Immediate grant (for a new request):
| Policy | Read granted if | Write granted if |
|---|---|---|
| reader | no writer holds | no holders and no reader waiting |
| writer | no writer holds and no writer waiting | no holders |
| fair | no writer holds and the queue is empty | no holders and the queue is empty |
Otherwise the thread joins the FIFO queue, and a try-operation returns EBUSY instead.
On release (a wunlock, or the last runlock), hand the lock over:
Errors:
EDEADLK (already reading|writing|waiting).EDEADLK (upgrading a read lock is not supported).EPERM (not reading|writing).policy: lock in use.: unknown operation.cLoading…
The policy names are reader-preferring, writer-preferring and fair (FIFO). A release that grants nothing prints just the release line. state lists readers in order of first appearance. The last line is always the maximum number of concurrent readers.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover writer starvation under the reader policy, reader starvation under the writer policy, alternating batches under the fair policy, try-operations, upgrade attempts, double requests, unlocking without holding, and changing the policy while the lock is in use.