/* * 测试成功时输出: * " Hello, mmap success." * 测试失败时输出: * "mmap error." */ #include #include #include #include #include #include #include #include static struct stat kst; void test_mmap(void){ char *array; const char *str = " Hello, mmap success."; int fd; fd = open("test_mmap.txt", O_RDWR | O_CREAT, S_IRUSR|S_IWUSR); write(fd, str, strlen(str)); fstat(fd, &kst); printf("file len: %d\n", (int)kst.st_size); array = mmap(NULL, kst.st_size, PROT_WRITE | PROT_READ, MAP_FILE | MAP_SHARED, fd, 0); printf("mmap addr: %x\n", (unsigned int)array); if (array == MAP_FAILED) { printf("mmap error.\n"); }else{ printf("mmap content: %s\n", array); munmap(array, kst.st_size); } close(fd); } int main(void){ test_mmap(); return 0; }