|
| 1 | +/******************************************************************************** |
| 2 | + File : direct_io.c |
| 3 | + Description : example of direct io |
| 4 | +
|
| 5 | +*********************************************************************************/ |
| 6 | +#define _GNU_SOURCE |
| 7 | +#include <stdio.h> |
| 8 | +#include <errno.h> |
| 9 | +#include <string.h> |
| 10 | +#include <stdlib.h> |
| 11 | + |
| 12 | +#include <sys/types.h> |
| 13 | +#include <sys/stat.h> |
| 14 | +#include <unistd.h> |
| 15 | +#include <fcntl.h> |
| 16 | + |
| 17 | +/* |
| 18 | +================================================================================== |
| 19 | +
|
| 20 | + Prototype Statement |
| 21 | +
|
| 22 | +================================================================================== |
| 23 | +*/ |
| 24 | + |
| 25 | +/* |
| 26 | +================================================================================== |
| 27 | +
|
| 28 | + DEFINES |
| 29 | +
|
| 30 | +================================================================================== |
| 31 | +*/ |
| 32 | + |
| 33 | +/* |
| 34 | +================================================================================== |
| 35 | +
|
| 36 | + Management |
| 37 | +
|
| 38 | +================================================================================== |
| 39 | +*/ |
| 40 | +static char write_strings[ ] = "direct io example"; |
| 41 | + |
| 42 | +/* |
| 43 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 44 | +
|
| 45 | + < Open Functions > |
| 46 | +
|
| 47 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 48 | +*/ |
| 49 | +/* |
| 50 | +_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ |
| 51 | + Function :main |
| 52 | + Input :int argc |
| 53 | + < number of arguments > |
| 54 | + char *argv[ ] |
| 55 | + < arguments > |
| 56 | + Output :void |
| 57 | + Return :int |
| 58 | + < result > |
| 59 | +
|
| 60 | + Description :direct io |
| 61 | +_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ |
| 62 | +*/ |
| 63 | +int main( int argc, char *argv[ ] ) |
| 64 | +{ |
| 65 | + int err; |
| 66 | + |
| 67 | + char *pathname; |
| 68 | + int fd; |
| 69 | + long align; |
| 70 | + char *buf = NULL; |
| 71 | + struct stat stat_buf; |
| 72 | + size_t block_size; |
| 73 | + |
| 74 | + if( argc < 2 ) |
| 75 | + { |
| 76 | + fprintf( stderr, "./%s pathname\n", argv[ 0 ] ); |
| 77 | + return( -1 ); |
| 78 | + } |
| 79 | + |
| 80 | + /* ------------------------------------------------------------------------ */ |
| 81 | + /* open a file with O_DIRECT flag */ |
| 82 | + /* ------------------------------------------------------------------------ */ |
| 83 | + pathname = argv[ 1 ]; |
| 84 | + |
| 85 | + fd = open( pathname, O_CREAT | O_RDWR | O_DIRECT, 0664 ); |
| 86 | + |
| 87 | + if( fd < 0 ) |
| 88 | + { |
| 89 | + perror( "open : " ); |
| 90 | + return( fd ); |
| 91 | + } |
| 92 | + |
| 93 | + /* ------------------------------------------------------------------------ */ |
| 94 | + /* get block size of the filesystem */ |
| 95 | + /* ------------------------------------------------------------------------ */ |
| 96 | + err = fstat( fd, &stat_buf ); |
| 97 | + |
| 98 | + if( err < 0 ) |
| 99 | + { |
| 100 | + perror( "fstat : " ); |
| 101 | + goto err_close; |
| 102 | + } |
| 103 | + |
| 104 | + block_size = ( size_t )stat_buf.st_blksize; |
| 105 | + |
| 106 | + /* ------------------------------------------------------------------------ */ |
| 107 | + /* get alignment restriction */ |
| 108 | + /* ------------------------------------------------------------------------ */ |
| 109 | + align = fpathconf( fd, _PC_REC_XFER_ALIGN ); |
| 110 | + |
| 111 | + if( align < 0 ) |
| 112 | + { |
| 113 | + fprintf( stderr, "error : fpathconf : %ld\n", align ); |
| 114 | + goto err_close; |
| 115 | + } |
| 116 | + |
| 117 | + /* ------------------------------------------------------------------------ */ |
| 118 | + /* allocate aligned memory */ |
| 119 | + /* ------------------------------------------------------------------------ */ |
| 120 | + err = posix_memalign( ( void** )&buf, ( size_t )align, block_size ); |
| 121 | + |
| 122 | + if( err < 0 ) |
| 123 | + { |
| 124 | + perror( "posix_memalign : " ); |
| 125 | + goto err_close; |
| 126 | + } |
| 127 | + |
| 128 | + /* ------------------------------------------------------------------------ */ |
| 129 | + /* write via direct io */ |
| 130 | + /* ------------------------------------------------------------------------ */ |
| 131 | + memset( buf, '\0', block_size ); |
| 132 | + memcpy( buf, write_strings, sizeof( write_strings ) ); |
| 133 | + |
| 134 | + err = write( fd, buf, block_size ); |
| 135 | + |
| 136 | + if( err < 0 ) |
| 137 | + { |
| 138 | + perror( "write : " ); |
| 139 | + } |
| 140 | + |
| 141 | + /* ------------------------------------------------------------------------ */ |
| 142 | + /* free memory */ |
| 143 | + /* ------------------------------------------------------------------------ */ |
| 144 | +err_write: |
| 145 | + free( buf ); |
| 146 | + |
| 147 | + /* ------------------------------------------------------------------------ */ |
| 148 | + /* close a file */ |
| 149 | + /* ------------------------------------------------------------------------ */ |
| 150 | +err_close: |
| 151 | + if( ( err = close( fd ) ) < 0 ) |
| 152 | + { |
| 153 | + perror( "close : " ); |
| 154 | + } |
| 155 | + |
| 156 | + return( err ); |
| 157 | +} |
| 158 | + |
| 159 | +/* |
| 160 | +_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ |
| 161 | + Function :void |
| 162 | + Input :void |
| 163 | + Output :void |
| 164 | + Return :void |
| 165 | +
|
| 166 | + Description :void |
| 167 | +_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ |
| 168 | +*/ |
| 169 | + |
| 170 | +/* |
| 171 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 172 | +
|
| 173 | + < Local Functions > |
| 174 | +
|
| 175 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 176 | +*/ |
| 177 | +/* |
| 178 | +================================================================================== |
| 179 | + Function :void |
| 180 | + Input :void |
| 181 | + Output :void |
| 182 | + Return :void |
| 183 | +
|
| 184 | + Description :void |
| 185 | +================================================================================== |
| 186 | +*/ |
0 commit comments