/* * compile with -DDO_FSYNC=1 and then with -DDO_FSYNC=0 */ #include #include #include #include #include #if !defined(DO_FSYNC) # error "You must define DO_FSYNC" #endif #define MYBUFSIZ BUFSIZ #define BYTES_TO_WRITE (32*1024*1024) /* 32MB */ int main(int argc, char *argv[]) { int fd, rc, i; char buf[MYBUFSIZ] = { '\0', }; fd = open("testfile", O_WRONLY|O_CREAT, 0600); if (fd < 0) { perror("open"); exit(1); } for (i = 0; i < (BYTES_TO_WRITE/MYBUFSIZ); i++) { rc = lseek(fd, 0, SEEK_SET); if (rc < 0) { perror("lseek"); exit(1); } rc = write(fd, buf, sizeof(buf)); if (rc < 0) { perror("write"); exit(1); } #if DO_FSYNC fdatasync(fd); if (rc < 0) { perror("fdatasync"); exit(1); } #endif } }