#include #include #include #include #include #define MAP_SIZE 32 #define TEST_FILE "textfile.txt" // TEST_FILE must be larger than a (16 * pagesize) + 32 bytes int main(int argc, char **argv) { int fd, offset; void * startAddr; fd = open(TEST_FILE, O_RDONLY); /* map with non-zero offset */ offset = 0; startAddr = mmap(0, MAP_SIZE, PROT_READ, MAP_SHARED, fd, offset); printf("Mapped file with zero offset at %d\n", (int)startAddr); printf("%c\n", *((char *)startAddr)); munmap(startAddr, MAP_SIZE); /* map with non-zero offset */ offset = 16 * getpagesize(); startAddr = mmap(0, MAP_SIZE, PROT_READ, MAP_SHARED, fd, offset); printf("Mapped file with nonzero offset at %d\n", (int)startAddr); printf("%c\n", *((char *)startAddr)); munmap(startAddr, MAP_SIZE); close(fd); return 0; }