Skip to content
Snippets Groups Projects

SHM test

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Ben Gamari

    A small test testing the accounting properties of POSIX shared memory mappings

    Edited
    shm_test.c 786 B
    #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;
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment