Advertisement
mechanicker

Check direct IO support

Feb 5th, 2020
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. // vim: sw=4
  2.  
  3. #define _GNU_SOURCE
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8.  
  9. int main(int argc, const char* argv[]) {
  10.     if (argc == 1) {
  11.         return -1;
  12.     }
  13.  
  14.     int fd = open(argv[1],
  15.             O_CREAT | O_EXCL | O_WRONLY | O_SYNC | O_DIRECT,
  16.             S_IRUSR | S_IWUSR);
  17.     if (fd < 0) {
  18.         printf("open failed with error: %d\n", errno);
  19.         return 0;
  20.     }
  21.  
  22.     const size_t sz = 1024 * 4;
  23.     char *data = NULL;
  24.     if (0 == posix_memalign((void **)&data, sz, sizeof(char) * sz)) {
  25.         if (write(fd, data, sizeof(char) * sz) < 0) {
  26.             printf("write failed with error: %d\n", errno);
  27.         }
  28.  
  29.         free(data);
  30.     } else {
  31.         printf("posix_memalign failed with error: %d\n", errno);
  32.     }
  33.  
  34.     close(fd);
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement