#include #include #include #include #include #include #define MMAP_FILE "mmap-test-file" #define MMAP_SIZE 16384*1024 main() { int fd; char* maddr; printf("creating " MMAP_FILE "\n"); fd = open(MMAP_FILE, O_CREAT|O_TRUNC|O_RDWR, 0666); if(fd < 0) { perror("Could not open " MMAP_FILE); exit(1); } printf("Writing zeros\n"); { int n = MMAP_SIZE; char buf[1024]; memset(buf, 0, 1024); while(n > 0) { write(fd, buf, 1024); n -= 1024; } } printf("mmap-ing\n"); maddr = mmap(NULL, MMAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if(maddr == NULL || maddr == MAP_FAILED) { perror("mmap"); exit(1); } printf("writing ones to mmap region\n"); { int n; for(n = 0; n < MMAP_SIZE; n++) maddr[n] = 1; } printf("done!\n"); }