Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • examples/io/filereader.c

    r5b15c4f r2a35f14  
    1 /*
    2 This is a file reading example that users io_uring in non-blocking mode.
    3 It demonstrates the bare minimum needed to use io_uring.
    4 It also optionally pre-registers the file descriptors (and a pipe, just to show it works).
    5 It uses liburing for simplicity.
    6 */
    7 
    8 
    91#include <errno.h>
    102#include <fcntl.h>
     
    179
    1810int main(int argc, char * argv[]) {
    19         if(argc != 3 && argc != 4) {
    20             printf("usage:   %s FILE TIMES [fixed] - read FILE from disk TIMES times\n", argv[0]);
     11        if ( argc != 3 ) {
     12                printf( "usage\n" );
     13                exit( EXIT_FAILURE );
     14        }
     15
     16        if(argc != 3) {
     17            printf("usage:   %s FILE TIMES - read FILE from disk TIMES times\n", argv[0]);
    2118            return EXIT_FAILURE;
    2219      }
    2320
    24         bool fixed = false;
    25         if(argc == 4) {
    26                 fixed = 0 == strcmp(argv[3], "fixed");
    27         }
    2821
    2922      int times = atoi( argv[2] );
     
    3932      }
    4033
    41         int rfd = fd;
    42 
    4334        /* prep the array */
    4435      char data[100];
     
    4940      io_uring_queue_init(256, &ring, 0);
    5041
    51         int pipefds[2];
    52         if(fixed) {
    53                 int ret = pipe(pipefds);
    54                 if( ret < 0 ) {
    55                         printf("Pipe Error : %s\n", strerror( errno ));
    56                         return EXIT_FAILURE;
    57                 }
    58                 rfd = 0;
    59                 int fds[] = {
    60                         fd, pipefds[0], pipefds[1]
    61                 };
    62                 int cnt = sizeof(fds) / sizeof(fds[0]);
    63                 printf("Registering %d files as fixed\n", cnt);
    64                 ret = io_uring_register_files(&ring, fds, cnt);
    65                 if( ret < 0 ) {
    66                         printf("Register Error : %s\n", strerror( -ret ));
    67                         return EXIT_FAILURE;
    68                 }
    69         }
    70 
    7142      /* declare required structs */
    7243        printf("Reading %s(%d) %d times\n", argv[1], fd, times);
     
    7546                /* get an sqe and fill in a READV operation */
    7647              struct io_uring_sqe * sqe = io_uring_get_sqe(&ring);
    77                 io_uring_prep_readv(sqe, rfd, &iov, 1, 0);
    78                 if(fixed) {
    79                         sqe->flags = IOSQE_FIXED_FILE;
    80                 }
     48                io_uring_prep_readv(sqe, fd, &iov, 1, 0);
    8149
    8250                /* tell the kernel we have an sqe ready for consumption */
     
    11583
    11684      close(fd);
    117 
    118         if(fixed) {
    119                 close(pipefds[0]);
    120                 close(pipefds[1]);
    121         }
    12285}
Note: See TracChangeset for help on using the changeset viewer.