Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created December 5, 2017 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadaustin/8e4ba1e2cd2d023ff6f9eff921eb8bfc to your computer and use it in GitHub Desktop.
Save chadaustin/8e4ba1e2cd2d023ff6f9eff921eb8bfc to your computer and use it in GitHub Desktop.
// g++ -Wall -o mmaptest mmaptest.cpp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <assert.h>
int main() {
if (system("dd if=/dev/urandom of=mmaptest.tmp bs=4096 count=6")) {
perror("system failed");
return 1;
}
// just larger than three pages
if (system("dd if=/dev/urandom of=mmaptest.tmp bs=12294 count=1")) {
perror("system failed");
return 1;
}
int fd = open("mmaptest.tmp", O_RDONLY, 0);
if (fd == -1) {
perror("open failed");
return 1;
}
// map all four pages
void* addr = mmap(NULL, 4096*4, PROT_READ, MAP_PRIVATE, fd, 0);
if (!addr) {
perror("mmap failed");
return 1;
}
char* map = (char*)addr;
for (int i = 12294; i < 4096*4; ++i) {
assert(0 == map[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment