mechanicker icon

Check direct IO support

mechanicker | PRO | 02/05/20 06:31:03 PM UTC | 0 ⭐ | 520 👁️ | Never ⏰ | []
C |

820 B

|

None

|

0 👍

/

0 👎

// vim: sw=4
 
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
 
int main(int argc, const char* argv[]) {
    if (argc == 1) {
        return -1;
    }
 
    int fd = open(argv[1],
            O_CREAT | O_EXCL | O_WRONLY | O_SYNC | O_DIRECT,
            S_IRUSR | S_IWUSR);
    if (fd < 0) {
        printf("open failed with error: %d\n", errno);
        return 0;
    }
 
    const size_t sz = 1024 * 4;
    char *data = NULL;
    if (0 == posix_memalign((void **)&data, sz, sizeof(char) * sz)) {
        if (write(fd, data, sizeof(char) * sz) < 0) {
            printf("write failed with error: %d\n", errno);
        }
 
        free(data);
    } else {
        printf("posix_memalign failed with error: %d\n", errno);
    }
 
    close(fd);
 
    return 0;
}

Comments