#include #include int main(void) { void* private_noaccess, *private_read, *private_rw, *private_file; FILE* file; int i, buf; /* * Create file to test MAP_PRIVATE of a file */ if(! (file = fopen("/tmp/maptest", "w+b"))) return 1; for(i = 0; i < 4096 / sizeof(int); ++i) fwrite(&i, sizeof(i), 1, file); fflush(file); /* * Will appear as rw-p mapping in smaps with Rss = 0 kiB, * meaning no anonymous memory is actually consumed until written to */ private_file = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(file), 0); fclose(file); /* * Will appear as rw-p with Rss = 0 kiB */ private_rw = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); /* * Will appear as r--p in smaps */ private_read = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); /* * Will appear as ---p in smaps */ private_noaccess = mmap(NULL, 4096, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); /* * Print mapping locations so that we can compare them with smaps */ printf("file=%p rw=%p r=%p noaccess=%p\n", private_file, private_rw, private_read, private_noaccess); /* * Print smaps content */ if(! (file = fopen("/proc/self/smaps", "r"))) return 2; while((buf = getc(file)) != EOF) putchar(buf); }