Home Libraries Author Links

Abstract Data Type Family StmSharedMemory<ItemType>
[System: Operating System Dependent Facilities]

Collaboration diagram for Abstract Data Type Family StmSharedMemory<ItemType>:

Detailed Description

StmSharedMemory<ItemType> is an abstract data type for named shared memory objects of abstract data type ItemType.

If, as in the example below, the abstract shared memory type StmSharedMemorySampleItemType is defined by the macro call

its timeout is 100 millseconds and the types and functions documented in StmSharedMemory<ItemType> Example are generated.

Example:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stm/system.h>


StmAbstractType (SampleItemType)  /* SampleSharedMemory */


struct SampleItemTypeImpl_
{
    int counter;
    double value;
};

static int stmSampleItemTypeInit (SampleItemType item, void *data)
{
    item -> counter = 0;
    item -> value = * (double *) data;
    return 0;
}

static int stmSampleItemTypeDeinit (SampleItemType item)
{
    return 0;
}

StmSharedMemoryItemDefine (SampleItemType, 100)  /* SampleSharedMemory */


static StmSharedMemorySampleItemType shm;

int main (int argc, char **argv)
{
    pid_t child = (pid_t) -1;
    int i;
    double begval = 0.99;
    shm = stmSharedMemorySampleItemTypeCreate
    (
        argc == 1 ? "SampelItemTypeTestShm%p" : argv [1],
        StmAttach,
        &begval
    );
    if (!shm)
    {
        printf ("Process %d: stmSharedMemorySampleItemTypeCreate failed: %s\n",
                getpid (), strerror (errno));
        exit (1);
    }
    if (argc == 1)
    {
        char *const childArgv [] =
        {
            argv [0],
            (char *) stmSharedMemorySampleItemTypeName (shm),
            NULL
        };
        if ((child = stmProcessSpawn (argv [0], childArgv, StmFalse)) < 0)
        {
            printf ("%s: stmProcessSpawn failed: %s\n",
                    argv [0], strerror (errno));
            exit (1);
        }
    }
    for (i = 0; i < 10; ++ i)
    {
        SampleItemType item = stmSharedMemorySampleItemTypeLock (shm);
        if (!item)
        {
            printf ("Process %d: stmSharedMemorySampleItemTypeLock failed: %s\n",
                    getpid (), strerror (errno));
        }
        else
        {
            ++ item -> counter;
            item -> value = (item -> value - 0.1) * 1.25;
            printf ("Process %d: Counter: %d, value: %f\n",
                    getpid (), item -> counter, item -> value);
            stmSharedMemorySampleItemTypeUnlock (shm);
            stmSleepMs (10);
        }
    }
    stmSharedMemorySampleItemTypeDestroy (shm);
    if (child > 0)
    {
        stmProcessWait (child, NULL);
    }
    return 0;
}
If the example program above is run from the command line without arguments it will output something like this:
Process 4416: Counter: 1, value: 1.112500
Process 2796: Counter: 2, value: 1.265625
Process 4416: Counter: 3, value: 1.457031
Process 2796: Counter: 4, value: 1.696289
Process 4416: Counter: 5, value: 1.995361
Process 2796: Counter: 6, value: 2.369202
Process 4416: Counter: 7, value: 2.836502
Process 2796: Counter: 8, value: 3.420628
Process 4416: Counter: 9, value: 4.150784
Process 2796: Counter: 10, value: 5.063481
Process 4416: Counter: 11, value: 6.204351
Process 2796: Counter: 12, value: 7.630438
Process 4416: Counter: 13, value: 9.413048
Process 2796: Counter: 14, value: 11.641310
Process 4416: Counter: 15, value: 14.426638
Process 2796: Counter: 16, value: 17.908297
Process 4416: Counter: 17, value: 22.260371
Process 2796: Counter: 18, value: 27.700464
Process 4416: Counter: 19, value: 34.500580
Process 2796: Counter: 20, value: 43.000725


Modules

 StmSharedMemory<ItemType> Example
 Example of an abstract shared memory type with the abstract item type SampleItemType.

Defines

#define StmSharedMemoryItemDefine(ItemType, TimeoutMs)   StmSharedMemoryItemImpl_ (ItemType, TimeoutMs)
 Definition of the abstract data type StmSharedMemory<ItemType> providing a named shared memory object of the abstract data type ItemType.


Define Documentation

#define StmSharedMemoryItemDefine ( ItemType,
TimeoutMs   )     StmSharedMemoryItemImpl_ (ItemType, TimeoutMs)

Definition of the abstract data type StmSharedMemory<ItemType> providing a named shared memory object of the abstract data type ItemType.

Parameters:
[in] ItemType Abstract data type of the shared memory object.
[in] TimeoutMs Timeout in milliseconds used for creation and destruction.
Requires:
ItemType is an abstract data type (see StmAbstractType). That means ItemType is convertible to and from void* and has an accompanying type ItemTypeImpl_.
ItemType has a method function named stm<ItemType>Init taking a first argument of type ItemType and a second one of type void * returning int.
ItemType has a method function named stm<ItemType>Deinit taking a single argument of type ItemType returning int.
Note:
For an example documenting all method functions of the abstract shared memory type StmSharedMemorySampleItemType for the ItemType SampleItemType type see StmSharedMemory<ItemType> Example.
Examples:
sharedmemorytest.c.

Definition at line 881 of file system.h.


© Copyright Tom Michaelis 2002-2007

Distributed under the SysToMath Software License (See the accompanying file license.txt or a copy at www.SysToMath.com).