Reading Shared Memory Segments

<< prev next >>

This example shows how to read shared memory using the Shared Memory Arena library. This in combination with the next program Writing Shared Memory Segments make up a hello world like program.

First lets run this program and see what it does.

In a shell (bash, csh, tcsh) from the directory examples/tutorial/ run:

./write & sleep 0.3 ; ./read

You should see ./read print the numbers that are read from shared memory. These numbers where being written by the backgrounded program ./write. If all went well all the programs exited in about 10 seconds.

Looking at the Source

From the source file examples/tutorial/read.c

First we include some header files:

#include <unistd.h> /* for usleep() */
#include <shm_arena.h> /* for Shared Memory Arena Library */

We have a main function:

int main(void)
{
int *ptr;
int running = 1;

We get a shared memory segment that is the size of an int with the name "count":

ptr = (int *) sm_get(sizeof(int), "count");

The sm_get() function can do a lot on its first call, like create the shared memory arena file and setup the local process shared memory allocator which we call a local arena object, shm_arena_t. Unless you choose to customize some things, this is all done automatically. Similar to the way the standard C library sets up an intra-process memory allocator the first time malloc() is called.

Since a lot can go wrong we next check for an error, sm_get() returns NULL on error:

if(!ptr)
{
printf("sm_get() failed\n");
return 1;
}

This program sleeps, reads and prints until the value in shared memory is 10 or greater.

while(running)
{
usleep(600000); /* micro seconds */
sm_rdlock(ptr);
printf("read %d\n", *ptr);
if(*ptr >= 10) running = 0;
sm_unlock(ptr);
}

The pointer ptr is used like an object pointer for the read-write lock. The read-write lock is located in an address near ptr. We acquire a read lock with sm_rdlock() before each read access the shared memory pointed to by ptr. Read locks let other processes read at the same time, but writers will be blocked whenever another thread or process holds a read lock or a write lock.

ptr is a pointer to a piece of inter-process shared memory. We do not need to free it unless we wish to make it so that all processes or programs cannot use it any more. We can use the function sm_remove() or the program shm_remove to remove the shared memory segment.

return 0;
}

After this programs runs there will be a shared memory arena file some where on your system, you have chooses as to where it is, but there is a reasonable default. If you have done nothing to effect the default you should see a file with the name of the value of SHM_DEFAULT_ARENA_FILE in the directory SHM_DEFAULT_ARENA_DIR.

You will deadlock the read-write lock if you call sm_rdlock() without calling sm_unlock(), say because your program exits just after calling sm_rdlock(). Shared Memory Arena uses pthreads read-write locks which are very fast at the expense of not having a releasing mechanism for when a program crashes. System V semaphores can be used to make read-write locks that have a releasing mechanism for when a program exits unexpectedly, but system V semaphores are very slow on GNU/Linux systems. As we'll see, catching signals can help us release read-write locks before exiting a program. If you have a deadlocked read-write lock in a shared memory segment you can remove the arena file that the segment is in and let it be recreated when the programs run again.

<< prev next >>


Shared Memory Arena version RC-0.0.25