source: examples/io_uring.c @ 918b90c

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 918b90c was f100a83, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Updated io_uring example to be less hacky

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