-
-
Save run/fba03b5a13d3500fe643c08cfa564b13 to your computer and use it in GitHub Desktop.
Revisions
-
garcia556 created this gist
Dec 3, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ #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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ #!/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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ #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; }