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

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.