source: examples/io_uring.c @ f3ed2af

arm-ehenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since f3ed2af was f3ed2af, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Fixed example with O_DIRECT to use proper alignment

  • Property mode set to 100644
File size: 1.8 KB
Line 
1#ifndef _GNU_SOURCE
2#define _GNU_SOURCE
3#endif
4
5#include <fcntl.h>
6#include <liburing.h>
7#include <stdio.h>
8#include <string.h>
9#include <unistd.h>
10
11struct io_uring ring;
12
13__attribute__((aligned(1024))) char data[1024];
14
15int main(int argc,  char * argv[]) {
16      if(argc != 2) {
17            printf("usage:   %s FILE - prints file to console.\n", argv[0]);
18            return 1;
19      }
20
21      int fd = open(argv[1], O_DIRECT);
22      if(fd < 0) {
23            printf("Could not open file %s.\n", argv[1]);
24            return 2;
25      }
26
27      /* prep the array */
28      struct iovec iov = { data, 1024 };
29
30      /* init liburing */
31      io_uring_queue_init(256, &ring, IORING_SETUP_IOPOLL);
32
33      /* declare required structs */
34      struct io_uring_sqe * sqe;
35      struct io_uring_cqe * cqe;
36
37      /* get an sqe and fill in a READV operation */
38      sqe = io_uring_get_sqe(&ring);
39      io_uring_prep_readv(sqe, fd, &iov, 1, 0);
40      // io_uring_prep_read(sqe, fd, data, 1024, 0);
41
42      sqe->user_data = (uint64_t)(uintptr_t)data;
43
44      /* tell the kernel we have an sqe ready for consumption */
45      io_uring_submit(&ring);
46
47      /* wait for the sqe to complete */
48      int ret = io_uring_wait_cqe(&ring, &cqe);
49
50      /* read and process cqe event */
51      if(ret == 0) {
52            char * out = (char *)(uintptr_t)cqe->user_data;
53            signed int len = cqe->res;
54            io_uring_cqe_seen(&ring, cqe);
55
56            if(len > 0) {
57                  printf("%.*s", len, out);
58            }
59            else if( len < 0 ) {
60                  fprintf(stderr, "readv/read returned error : %s\n", strerror(-len));
61            }
62      }
63      else {
64            printf("%d\n", ret);
65            io_uring_cqe_seen(&ring, cqe);
66      }
67
68      io_uring_queue_exit(&ring);
69
70      close(fd);
71}
Note: See TracBrowser for help on using the repository browser.