[comp.sys.apollo] Apollo BSD Shared Memory

ssf@mimsy.umd.edu (Sterling S. Foster) (06/07/90)

We have tried unsuccessfully to use the mmap call on a DN10000 to share
memory among forks. Does anyone have a short example which does this?

	

pato@apollo.HP.COM (Joe Pato) (06/08/90)

In article <24779@mimsy.umd.edu>, ssf@mimsy.umd.edu (Sterling S. Foster)
writes:
|> We have tried unsuccessfully to use the mmap call on a DN10000 to share
|> memory among forks. Does anyone have a short example which does this?
|> 
|> 	

The following code sequence was extracted from a portion of the registry
client agent in /lib/rgylib that uses mmap on both 68k's and dn10000's.

    typedef struct {
        boolean initialized;
        ... more data
    } global_info_t;

    global_info_t   *global_info = NULL;

    fd = open(LOCATION_CACHE_FILE, O_RDWR, 0666);

    if (fd == -1) {
        /*
         * Cache file did not exist, create it
         */
        fd = open(LOCATION_CACHE_FILE, O_RDWR | O_CREAT, 0666);
        created_cache = true;
    }

    if (fd != -1) {
        /*
         * Obtain an exclusive lock on the cache file to guarantee
         * a single thread running through this initialization code.
         */
        (void) flock(fd, LOCK_EX);

        len = sizeof(global_info_t);

        /*
         * Map the data in the file for read/write and allow the mapping
         * to be shared and inherited by offspring of this process.
         */
        global_info = (global_info_t *)
                            mmap(NULL, &len, PROT_READ | PROT_WRITE, 
                                    MAP_FILE | MAP_SHARED | MAP_INHERIT,
                                    fd, 0);

        if (global_info != NULL) {
            if (!global_info->initialized || created_cache) {
                /*
                 * We're the first process to access shared memory initialize
                 * data areas as appropriate...
                 */
                global_info->initialized = true;
            }
            /*
             * Close the file - retains shared memory, but releases
             * the advisory lock.  Subsequent access to shared memory
             * should be controlled under a mutex lock (see <apollo/mutex.h>
             */
            close(fd);
        } 
    }

    if (global_info == NULL) {
        /*
         * We're unable to map shared memory, generate a private area...
         * or take appropriate error action.  Also close the file if it
         * was opened.
         */
        if (fd != -1) {
            close(fd);
        }
    }

                    -- Joe Pato
                       Cooperative Object Computing Operation
                       Hewlett-Packard Company
                       pato@apollo.hp.com