g++ -pthreads code.cpp
./a.out
- begin: This semaphore allows only one process at a time to execute ENTRY section for readers, and protects readers from entering while writer is in CRITICAL section
- finish: This semaphore allows only one reader process to execute EXIT section.
- writer_wait: This semaphore is allows waiting writer to get priority if readers are in CRITICAL section.
- reader_enter: This integer variable stores the number of reader processes that executed ENTRY section.
- reader_exit: This integer variable stores the number of reader processes that executed the EXIT section.
- writer_waiting: This boolean variable stores whether any writer is waiting while readers are in CRITICAL section.
- DATA: Global shared data. Here it is integer type for simplicity.
- ENTRY SECTION: Takes control of
begin
semaphore to enter ENTRY section if possible, otherwise wait till it gets control. Increment thereader_enter
variable. Release thebegin
semaphotre. - CRITICAL SECTION: Read the
DATA
. - EXIT SECTION: Take control of
finish
semaphore to enter EXIT section if possible, otherwise wait till it gets control. Increment thereader_exit
variable. Check if a writer process is waiting and no other reader process is in CRITICAL SECTION, then Signal thewriter_wait
semaphore. Finally, release thefinish
semaphore.
-
ENTRY SECTION: Take control of
begin
semaphore and thenfinish
semaphore, if possible, otherwise wait till it gets control. This ensure no reader process is in ENTRY and EXIT sections. Also, check if no Reader process is in CRITICAL SECTION. If true, Releasefinish
semaphore. Otherwise, Update thewriter_waiting
variable to true, to indicate to reader processes that a writer process is waiting; Also, releasefinish
semaphore to allow reader processes in CRITICAL section to exit; Wait tillwriter_wait
semaphore is taken to control; Also updatewriter_waiting
to false. -
CRITICAL SECTION: Process or Modify
DATA
-
EXIT SECTION: Release the semaphore
begin
, to allow other readers and writers process to enter.