#define _XOPEN_SOURCE 600 #include #include #include #include #include #include #include #include #include #include #include int main(void) { char tmpfname[256]; long page_size; long total_size; void *pa; size_t len; int i, fd; pid_t child; char *ch; int exit_val; page_size = sysconf(_SC_PAGE_SIZE); /* Size of the file to be mapped */ total_size = page_size / 2; /* mmap will create a partial page */ len = page_size / 2; snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_11_5_%d", getpid()); /* Create shared file */ unlink(tmpfname); fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); if (fd == -1) { printf("Error at open(): %s\n", strerror(errno)); return 1; } if (ftruncate(fd, total_size) == -1) { printf("Error at ftruncate(): %s\n", strerror(errno)); return 1; } pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (pa == MAP_FAILED) { printf("Error at mmap(): %s\n", strerror(errno)); return 1; } ch = (char*)pa + len + 1; /* Check the patial page is ZERO filled */ for (i = 0; i < page_size/2 - 1; i++) { if (ch[i] != 0) { printf("Test FAILED: The partial page at the " "end of the file is not zero-filled\n"); return 1; } } /* Write to the partial page */ *ch = 'b'; //msync(pa, len, MS_SYNC); munmap(pa, len); close(fd); /* Open and map it again */ fd = open(tmpfname, O_RDWR, 0); unlink(tmpfname); pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (pa == MAP_FAILED) { printf("Error at 2nd mmap(): %s\n", strerror(errno)); return 1; } ch = pa + len + 1; if (*ch == 'b') { printf("Test FAILED: Modification of the partial page " "at the end of an object is written out\n"); return 1; } close(fd); munmap(pa, len); printf("Test PASSED\n"); return 1; }