Created
December 5, 2017 21:03
-
-
Save chadaustin/8e4ba1e2cd2d023ff6f9eff921eb8bfc to your computer and use it in GitHub Desktop.
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
// 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