-
-
Save pldubouilh/c007a311707798b42f31a8d1a09f1138 to your computer and use it in GitHub Desktop.
POSIX shared memory IPC example (shm_open, mmap), working on Linux and macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/mman.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <string.h> | |
#define STORAGE_ID "/SHM_TEST" | |
#define STORAGE_SIZE 32 | |
int main(int argc, char *argv[]) | |
{ | |
int res; | |
int fd; | |
char data[STORAGE_SIZE]; | |
pid_t pid; | |
void *addr; | |
pid = getpid(); | |
// get shared memory file descriptor (NOT a file) | |
fd = shm_open(STORAGE_ID, O_RDONLY, S_IRUSR | S_IWUSR); | |
if (fd == -1) | |
{ | |
perror("open"); | |
return 10; | |
} | |
// map shared memory to process address space | |
addr = mmap(NULL, STORAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0); | |
if (addr == MAP_FAILED) | |
{ | |
perror("mmap"); | |
return 30; | |
} | |
// place data into memory | |
memcpy(data, addr, STORAGE_SIZE); | |
printf("PID %d: Read from shared memory: \"%s\"\n", pid, data); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function is_linux | |
{ | |
if [[ "$(uname)" == "Linux" ]]; then | |
echo 1 | |
return | |
fi | |
echo 0 | |
} | |
SET="set" | |
GET="get" | |
CFLAGS="" | |
if [[ "$(is_linux)" == "1" ]]; then | |
CFLAGS="-lrt" | |
fi | |
cc ${CFLAGS} -o ${SET} ${SET}.c | |
cc ${CFLAGS} -o ${GET} ${GET}.c | |
./${SET} & | |
sleep 1 | |
./${GET} | |
if [[ "$(is_linux)" == "1" ]]; then | |
echo "/dev/shm:" | |
ls -l /dev/shm | |
fi | |
sleep 1 | |
rm ${SET} ${GET} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/mman.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <string.h> | |
#define STORAGE_ID "/SHM_TEST" | |
#define STORAGE_SIZE 32 | |
#define DATA "Hello, World! From PID %d" | |
int main(int argc, char *argv[]) | |
{ | |
int res; | |
int fd; | |
int len; | |
pid_t pid; | |
void *addr; | |
char data[STORAGE_SIZE]; | |
pid = getpid(); | |
sprintf(data, DATA, pid); | |
// get shared memory file descriptor (NOT a file) | |
fd = shm_open(STORAGE_ID, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); | |
if (fd == -1) | |
{ | |
perror("open"); | |
return 10; | |
} | |
// extend shared memory object as by default it's initialized with size 0 | |
res = ftruncate(fd, STORAGE_SIZE); | |
if (res == -1) | |
{ | |
perror("ftruncate"); | |
return 20; | |
} | |
// map shared memory to process address space | |
addr = mmap(NULL, STORAGE_SIZE, PROT_WRITE, MAP_SHARED, fd, 0); | |
if (addr == MAP_FAILED) | |
{ | |
perror("mmap"); | |
return 30; | |
} | |
// place data into memory | |
len = strlen(data) + 1; | |
memcpy(addr, data, len); | |
// wait for someone to read it | |
sleep(2); | |
// mmap cleanup | |
res = munmap(addr, STORAGE_SIZE); | |
if (res == -1) | |
{ | |
perror("munmap"); | |
return 40; | |
} | |
// shm_open cleanup | |
fd = shm_unlink(STORAGE_ID); | |
if (fd == -1) | |
{ | |
perror("unlink"); | |
return 100; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment