Last active
April 13, 2023 13:41
-
-
Save jdtournier/a1cc524b87ed7c0f4a77946fe2fdb8b2 to your computer and use it in GitHub Desktop.
demo for using memfd_create() to shared raw memory between processes
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 <sys/memfd.h> | |
#include <sys/syscall.h> | |
#include <sys/mman.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdio.h> | |
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ | |
} while (0) | |
int main(int argc, char *argv[]) | |
{ | |
int fd; | |
char *name; | |
ssize_t i, len; | |
unsigned char* addr; | |
struct stat st; | |
if (argc < 3) { | |
fprintf(stderr, "%s name size\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
name = argv[1]; | |
len = atoi(argv[2]) * 1024LU * 1024LU * 1024LU; | |
/* Create an anonymous file in tmpfs; */ | |
fd = syscall (SYS_memfd_create, name, 0); | |
if (fd == -1) | |
errExit("memfd_create"); | |
/* Size the file as specified on the command line */ | |
printf("length: %zu\n", len); | |
if (ftruncate(fd, len) == -1) | |
errExit("truncate"); | |
if (fstat (fd, &st)) | |
errExit ("fstat"); | |
printf("PID: %ld; fd: %d; /proc/%ld/fd/%d, atime: %lu.%lu\n", | |
(long) getpid(), fd, (long) getpid(), fd, st.st_atim.tv_sec, st.st_atim.tv_nsec); | |
addr = (unsigned char*) mmap (NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
if (addr == MAP_FAILED) | |
errExit("mmap"); | |
for (i = 0; i < len; ++i) | |
addr[i] = i & 0x000000FF; | |
if (fstat (fd, &st)) | |
errExit ("fstat"); | |
printf("length: %zu, atime: %lu.%lu\n", len, st.st_atim.tv_sec, st.st_atim.tv_nsec); | |
/* Keep running, so that the file created by memfd_create() | |
continues to exist */ | |
sleep (60); | |
printf("length: %zu, atime: %lu.%lu\n", len, st.st_atim.tv_sec, st.st_atim.tv_nsec); | |
exit(EXIT_SUCCESS); | |
} |
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 <sys/syscall.h> | |
#include <sys/mman.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdio.h> | |
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ | |
} while (0) | |
int main(int argc, char *argv[]) | |
{ | |
int fd; | |
char *name; | |
ssize_t i, len; | |
unsigned char* addr; | |
struct stat st; | |
if (argc != 2) { | |
fprintf(stderr, "%s /proc/PID/fd/FD\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
fd = open(argv[1], O_RDWR); | |
if (fd == -1) | |
errExit("open"); | |
if (fstat (fd, &st)) | |
errExit ("fstat"); | |
name = argv[1]; | |
len = st.st_size; | |
printf("length: %zu, atime: %lu.%lu\n", len, st.st_atim.tv_sec, st.st_atim.tv_nsec); | |
addr = (unsigned char*) mmap (NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
if (addr == MAP_FAILED) | |
errExit("mmap"); | |
printf ("start: "); | |
for (i = 0; i < 32; ++i) | |
printf ("%i ", addr[i]); | |
printf ("\nend: "); | |
for (i = 0; i < 32; ++i) | |
printf ("%i ", addr[len-32+i]); | |
printf ("\ndone\n"); | |
pause(); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment