Re: Locking & Unlocking mechanism in UNIX shared memory

From: Nick Landsberg (hukolau_at_NOSPAM.att.net)
Date: 02/27/04


Date: Fri, 27 Feb 2004 13:00:19 GMT


Loic Domaigne wrote:
> Hi Aravind,
>
>
>>I have a set of processes which work on a shared memory.The shared memory
>>is loaded with the data retrieved from table.I want to know the system
>>calls to lock & unlock a part( say a record) of the shared memory, so
>>that other processes should be allowed to access other records
>>available in the memory when this process locks its memory segment.
>
>
> Your issue doesn't belong to c.p.t, but rather to comp.unix.programmer.
> So I have cross-posted.
>
> A possibility would to use an IPC between your processes in order
> to coordinate the access to the shared memory (Posix or SysV semaphores is
> probably an adequate synchronization device).
>

To add to Loic's suggestion here:

Coordinating multiple writers to shared memory
segments can get very tricky, even with semaphores.
(Many possible race conditions to worry about.)
In my experience a "single writer" design is simplest.
By that I mean that all but one of the processes/threads
are normally free to read any record, but only
one of them can update a record. While the modification
of the record is in progress there may be a need
to prevent readers in order to guarantee that
readers get a consistent record. (Choose your
own semaphore granularity. Anywhere from
semaphore per record to semaphore on the whole
segment. There are arguments for both ways.)

Processes/threads which need to update a record
would send an IPC to the "writer" process/thread
which would perform the actual update.

The IPC is slower, but I have found it to be
safer than mucking around with multiple
(possibly conflicting) writers.

>
> HTH,
> Loic.

-- 
Ñ
"It is impossible to make anything foolproof because fools are so 
ingenious" - A. Bloch


Relevant Pages

  • Re: Inter-processor atomic operationss
    ... difference between the implementation of this hypothetical synchronization ... Need to either always use a keyed event for the wait semaphore ... Don't refer to any memory not in the shared memory region. ... Does the Interlocked API set guarantee inter CPU atomic operations ...
    (microsoft.public.win32.programmer.kernel)
  • Inter Process Communication
    ... Two or several processes can exchange data through this resource. ... The shared memory ID is given as program argument. ... sem_create: creates a semaphore set. ...
    (comp.unix.programmer)
  • Re: Suggestions of design (contd.)
    ... The SNMP subagent creates a shared memory region and semaphore ... If a snmpget request comes then it needs to answer the desired ... but the safe functions are the minority out ...
    (comp.unix.programmer)
  • TSP and CreateSemaphore/CreateFileMapping
    ... shared memory & a semephore. ... The other process creates the semaphore and the shared ... Now when I run each of these, all return values from the Win32 APIs indicate ... GetLastError, it returns ERROR_ALREADY_EXISTS. ...
    (microsoft.public.win32.programmer.tapi)
  • Re: suggestion for process shared mutex
    ... Ittay Freiman wrote: ... > did they provide such a feature for shared memory, ... Each process using the semaphore should, ... in that shared region. ...
    (comp.os.linux.development.system)