readers-writers problem using SYSV semaphores

readers-writers problem using SYSV semaphores

Post by Jonathan H N Ch » Wed, 17 Feb 1999 04:00:00

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 H N Chin, 1 kyu | deputy computer | Newton Institute, Cambridge, UK

                "respondeo etsi mutabor" --Rosenstock-Huessy


1. Semaphores, one writer, multiple readers? (Linux)

I have read some basic concepts about semaphores
but haven't found a solution to my problem. I assume there
is an effective way to create some kind of locking mechanism for
"single writer, multiple readers" type of system?

I have a shared memory area in my system, which has to be read
very frequently by many processes, and updated infrequently by
exactly one process. If I use simple semaphore solution, which
has semaphore initial count set to one, then only one process
can read that memory area at a time, which, I assume, is not the
most effective solution? I need to protect that shared memory area,
because one process sometimes updates it (with memcpy, which I assume
is not atomic), so the memory area might contain partially old, partially
new data, which I don't want. However, there are no problems if
several processes read the area simultaneously, my only concern
is to prevent simulatenous write & read operations.

How can I implement this kind of "one writer, multiple readers"
solution in the most effective way?


 -----  Posted via NewsOne.Net: Free (anonymous) Usenet News via the Web  ----- -- Free reading and anonymous posting to 60,000+ groups
   NewsOne.Net prohibits users from posting spam.  If this or other posts

2. Customisation trouble with fvwm95 on RedHat v4

3. "tee", but with fast writer, 1 slow reader and 1 fast reader

4. Curiosity with NCSA parsed HTML and gzip

5. ide cdrom and cdrom writer with scsi harddisk using cdrecord can't find cd reader

6. restricting port forwarding???

7. SYSV IPC Semaphore Problem on Linux

8. XF86Conf monitor refresh rates

9. Reader/writer problem.

10. Is this a reader writer problem?

11. pipe() problem - one reader, multiple writers?

12. Disk based buffer, one writer, many readers problem

13. reader-writer livelock problem