SHM test
The snippet can be accessed without any authentication.
Authored by
Ben Gamari
A small test testing the accounting properties of POSIX shared memory mappings
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
int main () {
size_t sz = 1024*1024*1024;
shm_unlink("/test");
int fd = shm_open("/test", O_RDWR | O_CREAT | O_EXCL, 0777);
assert(fd != -1);
printf("set size\n");
int res = ftruncate(fd, sz);
assert(res == 0);
getc(stdin);
printf("map it\n");
uint8_t *p = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
getc(stdin);
printf("touch\n");
memset(p, 7, sz);
getc(stdin);
printf("dontneed it\n");
madvise(p, sz, MADV_DONTNEED);
getc(stdin);
printf("unmap it\n");
munmap(p, sz);
getc(stdin);
printf("done\n");
return 0;
}
Please register or sign in to comment