Back to the 2025 paper

Module 3: Inter-process Communication

20257m

Write an algorithm for Readers-Writers Problem solution using Semaphores.

Worked SolutionAI Assisted

Readers-Writers with Semaphores

shared int readcount=0; sem mutex=1, wrt=1;
Reader: wait(mutex); readcount++; if(readcount==1) wait(wrt); signal(mutex); read; wait(mutex); readcount--; if(readcount==0) signal(wrt); signal(mutex);
Writer: wait(wrt); write; signal(wrt);
Readers preference variant, writer may starve; for writer preference add extra sem.

Similar questions

Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.