ACE Tutorial 020
Sharing your Memories with persistence


Here's our new server using a memory mapped file instead of Unix System V shared memory. Print out both servers and hold 'em up to a strong light. I think you'll see there isn't much difference.

The filename is defined in mmap.h as mmapfile. You can cat it any time to see what's in there. If you're feeling brave, try writting some data into it. On my system it causes unpleasant things to happen. I'd recommend that you don't do that!



// $Id$

#include "mmap.h"

#include "ace/Log_Msg.h"

int
main (int, char *[])
{
        /*
          The default behavior of the memory map wrapper is to create
          the file if it doesn't exist.  This is a minor change from
          the SV shared memory wrapper.
        */
    ACE_Shared_Memory_MM shm_server (SHM_KEY, SHMSZ);
    char *shm = (char *) shm_server.malloc ();
    char *s   = shm;

    ACE_DEBUG ((LM_INFO, "(%P|%t) Memory Mapped file is at 0x%x\n",
                shm ));

    for (char c = 'a'; c <= 'z'; c++)
        *s++ = c;

    *s = '\0';

    while (*shm != '*')
        ACE_OS::sleep (1);

    for (s = shm; *s != '\0'; s++)
    {
        putchar (*s);
    }

    putchar ('\n');

    if (shm_server.remove () < 0)
        ACE_ERROR ((LM_ERROR, "%p\n", "remove"));

    return 0;
}


BTW: In ACE 4.6.7 and prior there is a bug that prevents the remove() method from actually removing the physical file.


[Tutorial Index] [Continue This Tutorial]