#define _GNU_SOURCE #include #include #include #include #include #include #include #include // Define this to perform the fallocate and the pwrite sequentially // instead of in parallel // #define IN_ORDER int fd; void *falloc_thread(void *arg) { int ret; (void)arg; puts("starting fallocate"); ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, 4096); assert(ret == 0); puts("fallocate done"); return NULL; } int main(int argc, char *argv[]) { pthread_t falloc_thr; int ret; if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC | O_DIRECT, 0666); assert(fd >= 0); void *buf = aligned_alloc(4096, 4096); memset(buf, 42, 4096); io_context_t ctx = 0; ret = io_setup(1, &ctx); assert(ret == 0); ret = pthread_create(&falloc_thr, NULL, &falloc_thread, NULL); assert(ret == 0); #ifdef IN_ORDER ret = pthread_join(falloc_thr, NULL); assert(ret == 0); #endif struct iocb ior; io_prep_pwrite(&ior, fd, buf, 4096, 4096); puts("submitting pwrite"); struct iocb *ios[] = { &ior }; ret = io_submit(ctx, 1, ios); assert(ret == 1); struct io_event evs[1]; ret = io_getevents(ctx, 1, 1, evs, NULL); assert(ret == 1); puts("pwrite done"); #ifndef IN_ORDER ret = pthread_join(falloc_thr, NULL); assert(ret == 0); #endif close(fd); free(buf); puts("\nHexdump should show 4k of 0s and 4k of 42s:\n"); execlp("hexdump", "hexdump", "-C", argv[1], NULL); return 1; }