memset(buf, 0xaa, ps*256);
if( (fd = open(FILENAME, O_WRONLY O_DIRECT) ) < 0 ) { perror("Open failed");
exit(ret);
}
bytes_written = 0;
while( (ret = pwrite(fd, buf, ps*256, bytes_written)) == ps*256) { bytes_written += ret;
}
printf("Wrote %lld GB\n", bytes_written/1000/1000/1000); close(fd);
free(buf);
}
Using direct I/O on Windows
Direct I/O on Windows operating systems is set up through the CreateFile() call, using the FILE_FLAG_NO_BUFFERING flag. For more information, see the "CreateFile Function
When using direct IO, HP recommends that you keep read, writes, and buffers used for read/write sector size aligned. For a discussion of alignment requirements, see the “File Buffering
C++ code sample
The following code sample writes to the block device using O_DIRECT. The code sample has a class that encapsulates the operating system file functions that support O_DIRECT.
#include <unistd.h> #include <iostream> #include <errno.h> #include <fcntl.h> using namespace std;
//Short demonstration of a container class that writes using O_DIRECT.
//Compiled using GNU C++.
#define FILENAME "/dev/fioc" class DirectFile
{
public: DirectFile() {}; ~DirectFile() { ::close(fd); };
Programming using direct I/O 26