diff options
| author | John Ogness <john.ogness@linutronix.de> | 2017-12-19 10:59:40 +0100 |
|---|---|---|
| committer | John Ogness <john.ogness@linutronix.de> | 2017-12-19 10:59:40 +0100 |
| commit | 270520b4a2eac8725c8575c3180964289722e191 (patch) | |
| tree | d9512cd96d0c52e14293c3f8bc19fd168ee14025 /schulung_tools/ipc_shm/shm.c | |
| parent | 27209bb802048f4803d9cd9a5c2f99d613986446 (diff) | |
schulung_tools: add various demos and tools
Different tools have been used by various trainers as demos. Put
all these into master so they are available to all trainers.
ipc_pipe: ipc demo using pipes
ipc_shm: ipc demo using shared memory
libduma: source and instructions for compiling libduma
matrix: demo of good and bad cache access
mtrace: patch and infos for using mtrace with ASLR
rtex: demo of handling page faults
Signed-off-by: John Ogness <john.ogness@linutronix.de>
Diffstat (limited to 'schulung_tools/ipc_shm/shm.c')
| -rw-r--r-- | schulung_tools/ipc_shm/shm.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/schulung_tools/ipc_shm/shm.c b/schulung_tools/ipc_shm/shm.c new file mode 100644 index 0000000..777fe79 --- /dev/null +++ b/schulung_tools/ipc_shm/shm.c @@ -0,0 +1,73 @@ +#include <stdio.h> +#include <stdlib.h> +#include <pthread.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include "shm.h" + +#define SHARE_NAME "/ipc.shm" + +/* open and return a pointer to shared data */ +struct share_data *get_share(void) +{ + struct share_data *data; + int fd; + + fd = shm_open(SHARE_NAME, O_RDWR, 0); + if (fd == -1) + return NULL; + + data = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); + if (data == MAP_FAILED) { + close(fd); + return NULL; + } + + close(fd); + + return data; +} + +/* initialize shared data */ +void init_share(struct share_data *data) +{ + pthread_mutexattr_t mattr; + pthread_condattr_t cattr; + + pthread_mutexattr_init(&mattr); + pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT); + pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); + pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST); + pthread_mutex_init(&data->m, &mattr); + + pthread_condattr_init(&cattr); + pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); + pthread_cond_init(&data->c, &cattr); +} + +/* create a zero'd out file of size 1 page */ +int create_share(void) +{ + int ret; + int fd; + + fd = shm_open(SHARE_NAME, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR); + if (fd == -1) + return -1; + + ret = ftruncate(fd, sysconf(_SC_PAGESIZE)); + + close(fd); + + return ret; +} + +/* remove the share */ +void remove_share(void) +{ + shm_unlink(SHARE_NAME); +} |
