#include <stdio.h> /* printf() */ #include <stddef.h> /* sizeof() */ #include <signal.h> /* signal(), SIGINT */ #include <sys/mman.h> /* PROT_READ, needed for rtai_shm.h */ #include <sys/types.h> /* off_t, needed for rtai_shm.h */ #include <sys/fcntl.h> /* O_RDWR, needed for rtai_shm.h */ #include <rtai_shm.h> /* rtai_malloc,free() */ #include "common.h" /* SHM_KEY, SHM_HOWMANY */ /* This signal handler just sets the 'done' flag, which we will loop on when printing the shared memory. This lets us quit nicely. */ static int done = 0; static void quit(int sig) { done = 1; } int main(void) { int * shm_array; int head, tail; shm_array = rtai_malloc(SHM_KEY, SHM_HOWMANY * sizeof(int)); if (0 == shm_array) { fprintf(stderr, "can't allocate shared memory\n"); return 1; } /* Attach our signal hander to SIGINT, the signal raised when we hit Control-C. */ signal(SIGINT, quit); while (! done) { /* Read the first and last element of the array. The should be the same, but if we are interrupted by the RT code in between these reads, the tail will be newer. */ head = shm_array[0]; /* we could get interrupted here */ tail = shm_array[SHM_HOWMANY - 1]; /* Print out head and tail of shared memory, and an error if they're different. */ printf("%10d\t%10d\r", head, tail); if (head != tail) { printf("\nsplit read\n"); } } /* Control-C stopped us, so let's exit nicely by freeing up our connection to shared memory. */ rtai_free(SHM_KEY, shm_array); return 0; }