a firstworks project
Rudiments
About Documentation Download Licensing News

Using the sharedmemory class

Shared memory allows seperate processes to access a common block of memory. The sharedmemory class provides methods for creating and accessing shared memory.

This program creates a shared memory segment, copies some data into it, then goes to sleep, giving another program time to access the segment.

#include <rudiments/sharedmemory.h>
#include <rudiments/charstring.h>
#include <rudiments/permissions.h>
#include <rudiments/file.h>
#include <rudiments/error.h>
#include <rudiments/snooze.h>
#include <rudiments/process.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

	// create a file called "shm"
	file::createFile("shm",permissions::parsePermString("rw-------"));

	// create a 128 byte shared memory segment, keyed to the file "shm"
        sharedmemory    shm;
        if (!shm.create(file::generateKey("shm",1),128,
                                permissions::parsePermString("rw-------"))) {
		stdoutput.printf("error: %s\n",error::getErrorString());
		process::exit(1);
	}

	// write a string into the shared memory
        char    *shmptr=(char *)shm.getPointer();
        charstring::copy(shmptr,"This string is in shared memory.");

	// sleep for 10 seconds, giving another process some time to access
	// the shared memory...
        snooze::macrosnooze(10);

	// remove the file "shm"
	file::remove("shm");
}

This program reads the data from shared memory.

#include <rudiments/sharedmemory.h>
#include <rudiments/permissions.h>
#include <rudiments/file.h>
#include <rudiments/error.h>
#include <rudiments/process.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

	// attach to the shared memory segment keyed to the file "shm"
        sharedmemory    shm;
        if (!shm.attach(file::generateKey("shm",1),128)) {
		stdoutput.printf("error: %s\n",error::getErrorString());
		process::exit(1);
	}

	// display the data contained in the shared memory segment
        stdoutput.printf("%s\n",(char *)shm.getPointer());
}

Note that on unix-like systems, the first program should not be killed. Shared memory segments are persistent in the kernel and remain unless specifically removed. The sharedmemory class will remove the shared memory segment that it created when the instance of the class is destroyed, but if the first process is killed then the shared memory segment will persist and subsequent attempts to run the program will fail with a cryptic error like "File exists" or similar.

If this happens, most unix-like systems provide utilites for examining and cleaning up shared memory segments.

Running ipcs -m allows you to examine shared memory segments. It ought to return something like:

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0x010114e8 190021641  dmuse      600        128        1

The shmid can then be used with the ipcrm utility to remove the shared memory segment. For example: ipcrm -m 190021641

None of this is an issue on Windows systems.

Copyright 2017 - David Muse - Contact