#include #include #include #include #include #include #include struct list { char data[500]; off_t next; }; int main(int argc, char *argv[argc+1]) { size_t len = 10 << 20; int fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, S_IRUSR); if (fd == -1) { perror("open"); return EXIT_FAILURE; } // grow the file if (ftruncate(fd, len) == -1) { perror("truncate"); return EXIT_FAILURE; } void *base = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (base == MAP_FAILED) { perror("mmap"); return EXIT_FAILURE; } // Do 0..n-1 items for (int i = 0; i < 99; i++) { void *addr = (void*) (base + (i * sizeof(struct list))); off_t next = (off_t) (i+1)* sizeof(struct list); struct list *item = (struct list*)addr; sprintf(item->data, "i am item # %d", i); item->next = next; } off_t addr = (off_t) (base + (99 * sizeof(struct list))); struct list *item = (struct list*)addr; sprintf(item->data, "i am item # 99"); msync(base, len, MS_SYNC); munmap(base, len); return EXIT_SUCCESS; }