L4Re – L4 Runtime Environment

Simple shared memory example.

* (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
* economic rights: Technische Universität Dresden (Germany)
* This file is part of TUD:OS and distributed under the terms of the
* GNU General Public License 2.
* Please see the COPYING-GPL-2 file for details.
* This example uses shared memory between two threads, one producer, one
* consumer.
#include <l4/shmc/shmc.h>
#include <l4/util/util.h>
#include <stdio.h>
#include <string.h>
#include <pthread-l4.h>
#include <l4/sys/thread.h>
// a small helper
#define CHK(func) if (func) { printf("failure: %d\n", __LINE__); return (void *)-1; }
static const char some_data[] = "Hi consumer!";
static void *thread_producer(void *d)
l4shmc_chunk_t p_one;
l4shmc_signal_t s_one, s_done;
l4shmc_area_t shmarea;
// attach this thread to the shm object
CHK(l4shmc_attach("testshm", &shmarea));
// add a chunk
CHK(l4shmc_add_chunk(&shmarea, "one", 1024, &p_one));
// add a signal
CHK(l4shmc_add_signal(&shmarea, "prod", &s_one));
CHK(l4shmc_attach_signal_to(&shmarea, "done",
pthread_l4_cap(pthread_self()), 10000, &s_done));
// connect chunk and signal
CHK(l4shmc_connect_chunk_signal(&p_one, &s_one));
printf("PRODUCER: ready\n");
while (1)
while (l4shmc_chunk_try_to_take(&p_one))
printf("Uh, should not happen!\n"); //l4_thread_yield();
memcpy(l4shmc_chunk_ptr(&p_one), some_data, sizeof(some_data));
CHK(l4shmc_chunk_ready_sig(&p_one, sizeof(some_data)));
printf("PRODUCER: Sent data\n");
return NULL;
static void *thread_consume(void *d)
l4shmc_area_t shmarea;
l4shmc_chunk_t p_one;
l4shmc_signal_t s_one, s_done;
// attach to shared memory area
CHK(l4shmc_attach("testshm", &shmarea));
// get chunk 'one'
CHK(l4shmc_get_chunk(&shmarea, "one", &p_one));
// add a signal
CHK(l4shmc_add_signal(&shmarea, "done", &s_done));
// attach signal to this thread
CHK(l4shmc_attach_signal_to(&shmarea, "prod",
pthread_l4_cap(pthread_self()), 10000, &s_one));
// connect chunk and signal
CHK(l4shmc_connect_chunk_signal(&p_one, &s_one));
while (1)
printf("CONSUMER: Received from chunk one: %s\n",
(char *)l4shmc_chunk_ptr(&p_one));
memset(l4shmc_chunk_ptr(&p_one), 0, l4shmc_chunk_size(&p_one));
return NULL;
int main(void)
pthread_t one, two;
// create new shared memory area, 8K in size
if (l4shmc_create("testshm", 8192))
return 1;
// create two threads, one for producer, one for consumer
pthread_create(&one, 0, thread_producer, 0);
pthread_create(&two, 0, thread_consume, 0);
// now sleep, the two threads are doing the work
return 0;
long l4shmc_attach(const char *shmc_name, l4shmc_area_t *shmarea)
Attach to a shared memory area.
long l4shmc_connect_chunk_signal(l4shmc_chunk_t *chunk, l4shmc_signal_t *signal)
Connect a signal with a chunk.
long l4shmc_create(const char *shmc_name, l4_umword_t shm_size)
Create a shared memory area.
long l4shmc_chunk_consumed(l4shmc_chunk_t *chunk)
Mark a chunk as free.
long l4shmc_wait_chunk(l4shmc_chunk_t *chunk)
Wait on a specific chunk.
long l4shmc_chunk_size(l4shmc_chunk_t *chunk)
Get current size of a chunk.
long l4shmc_chunk_ready_sig(l4shmc_chunk_t *chunk, l4_umword_t size)
Mark chunk as filled (ready) and signal consumer.
long l4shmc_chunk_try_to_take(l4shmc_chunk_t *chunk)
Try to mark chunk busy.
long l4shmc_add_chunk(l4shmc_area_t *shmarea, const char *chunk_name, l4_umword_t chunk_capacity, l4shmc_chunk_t *chunk)
Add a chunk in the shared memory area.
void * l4shmc_chunk_ptr(l4shmc_chunk_t *chunk)
Get data pointer to chunk.
long l4shmc_get_chunk(l4shmc_area_t *shmarea, const char *chunk_name, l4shmc_chunk_t *chunk)
Get chunk out of shared memory area.
long l4shmc_wait_signal(l4shmc_signal_t *signal)
Wait on a specific signal.
long l4shmc_trigger(l4shmc_signal_t *signal)
Trigger a signal.
long l4shmc_add_signal(l4shmc_area_t *shmarea, const char *signal_name, l4shmc_signal_t *signal)
Add a signal for the shared memory area.
long l4shmc_attach_signal_to(l4shmc_area_t *shmarea, const char *signal_name, l4_cap_idx_t thread, l4_umword_t timeout_ms, l4shmc_signal_t *signal)
Attach to signal, with timeout.
void l4_sleep_forever(void) L4_NOTHROW)
Go sleep and never wake up.
Definition: util.h:50
Shared memory library header file.