Skip to content

Instantly share code, notes, and snippets.

@jdtournier
Last active April 13, 2023 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdtournier/a1cc524b87ed7c0f4a77946fe2fdb8b2 to your computer and use it in GitHub Desktop.
Save jdtournier/a1cc524b87ed7c0f4a77946fe2fdb8b2 to your computer and use it in GitHub Desktop.
demo for using memfd_create() to shared raw memory between processes
//#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);
}
#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