#include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/mman.h> int main() { int fd, rc; char *addr; const int PAGE_SIZE = sysconf(_SC_PAGESIZE); rc = unlink("file"); if (rc < 0 && errno != ENOENT ) { perror("unlink"); return 1; } fd = open("file", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if (fd < 0) { perror("open"); return 1; } addr = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (addr < 0) { perror("mmap"); return 1; } rc = pwrite(fd, "a", 1, 0); if (rc < 0) { perror("pwrite"); close(fd); return 1; } addr[0] = 'B'; addr[1] = 'U'; addr[2] = 'G'; while (1) { printf("Press enter to change buffer contents\n"); getchar(); addr[3]++; printf("Buffer contents changed\n"); } // This is not reached. rc = munmap(addr, PAGE_SIZE); if (rc < 0) { perror("munmap"); return 1; } close(fd); return 0; }