Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.