To obtain shared-read, exclusive-write and FIFO (assuming the SYSV
semaphore implementation en/dequeues in order), is this algorithm correct?
WRLK = mutex to lock out readers when there is a writer
RDCT = semaphore used to count the number of readers
initially: WRLK=1, RDCT=0
get_shared: | get_exclusive:
Wait(WRLK) | Wait(WRLK)
signal(RDCT) | Sleep till RDCT==0
Signal(WRLK) |
| PROTECTED-REGION
PROTECTED-REGION |
| release_exclusive:
release_shared: | Signal(WRLK)
Wait(RDCT) |
The "Sleep till RCDT==0" is implemented by a semop(2) call with
sem_op=0 on RDCT. Wait() uses sem_op=-1; Signal() uses sem_op=+1.
I have seen an implementation that uses three semaphores and lots of
IPC_NOWAITed semops with sem_op=0, but I didn't understand it.
From my small amount of reading, it seems FIFO queueing of processes
on SYSV semaphores is not guaranteed. Is this correct? If so, this might
explain the extra code in the other implementations that I have seen
that I found to complicated to understand.
-jonathan
--
Jonathan H N Chin, 1 kyu | deputy computer | Newton Institute, Cambridge, UK
"respondeo etsi mutabor" --Rosenstock-Huessy