↑ On-the-Shelf Software ↑

GEONius.com
20-Jan-2023
 E-mail 

shm_util - Shared Memory Utilities

The SHM utilities provide a high-level interface to the underlying operating system's shared memory facility.

Creating and/or mapping to a shared memory segment is as simple as this:

    #include  "shm_util.h"			-- Shared memory utilities.
    SharedMemory  memory ;
    long  size = numBytes ;
    void  *address ;
    ...
    address = NULL ;
    shmCreate ("name", size, &address, &memory) ;
    ...
    ... access shared memory at address returned in address ...
    ...

The first process that calls shmCreate() for a given segment creates the shared memory segment. Subsequent calls to shmCreate() by other processes map to the existing segment.

If the operating system supports it, shared memory segments can be created at specific addresses. This is accomplished by setting the address argument to the desired address before calling shmCreate():

    ...
    address = desiredAddress ;
    shmCreate ("name", size, &address, &memory) ;
    ...

Presumably, the creator and users of that memory segment know what they are doing.

The binary contents of a shared memory segment can be saved to a file and restored from a file with shmSave() and shmLoad(), respectively:

    shmSave (memory, "fileName") ;		-- Save contents.
    ...
    shmLoad (memory, "fileName") ;		-- Restore contents.

A shared memory segment is unmapped by calling shmDestroy():

    shmDestroy (memory) ;

The shared memory segment isn't deleted from the system until the last process mapped to it deletes it.


Implementation Notes (UNIX)

The UNIX shared memory functions, shmget(2) et al, are used to create, attach to, and delete shared memory segments. The name/IPC identifier mappings and reference counts are stored in the named object database (see nob_util.c).

Processes should delete all shared memory segments before exiting; if a process exits prematurely, the named object database could be left in an inconsistent state.


Implementation Notes (VxWorks)

Since all tasks run in a single address space, "shared" memory segments are simply allocated from the malloc(3) heap; under VxMP, the segments are allocated from global memory. Segments can be located at arbitrary addresses by specifying an address in the shmCreate() call. The name/address mappings and reference counts are stored in the named object database (see nob_util.c).

Processes should delete all shared memory segments before exiting; if a process exits prematurely, the named object database could be left in an inconsistent state.


Public Procedures

shmAddress() - returns the address of a shared memory segment.
shmCreate() - creates and/or maps to a shared memory segment.
shmDestroy() - deletes a shared memory segment.
shmId() - returns the IPC identifier for a shared memory segment.
shmLoad() - loads the contents of a file into a shared memory segment.
shmSave() - saves the contents of a shared memory segment to a file.
shmSizeOf() - returns the size of a shared memory segment.

Source Files

shm_util.c
shm_util.h

Alex Measday  /  E-mail