#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #define PAGE_SIZE 4096 int test(char* file) { char* buf = NULL; int ret = 0; int i = 0; posix_memalign((void**)(&buf), PAGE_SIZE, PAGE_SIZE); memset(buf, 0, PAGE_SIZE); for (i = 0 ; i < PAGE_SIZE ; ++i) buf[i] = i; int fd = open(file, O_WRONLY|O_CREAT|O_DIRECT|O_SYNC); if (fd == -1) { fprintf(stderr, "%s: %s\n", file, strerror(errno)); exit(1); } struct stat st; ret = fstat(fd, &st); if (ret != 0) { fprintf(stderr, "%s: %s\n", file, strerror(errno)); exit(1); } int offset = st.st_size; ret = pwrite(fd, buf, PAGE_SIZE, offset); if (ret != PAGE_SIZE) { fprintf(stderr, "write fail: ret %d %s\n", ret, strerror(errno)); } close(fd); return 0; } int main(int argc, char ** argv) { int ret = 0; char file[1024] = {}; if (argc != 2) { fprintf(stderr, "usage: %s path-to-write\n", argv[0]); exit(2); } snprintf(file, sizeof(file), "%s_%d", argv[1], 0); test(file); return 0; }