Ignore:
Timestamp:
Jul 27, 2020, 1:31:23 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
a86b1b85
Parents:
7458fea
Message:

Added fixed file example to filereader program

File:
1 edited

Legend:

Unmodified
Added
Removed
  • examples/io/filereader.c

    r7458fea r5b15c4f  
     1/*
     2This is a file reading example that users io_uring in non-blocking mode.
     3It demonstrates the bare minimum needed to use io_uring.
     4It also optionally pre-registers the file descriptors (and a pipe, just to show it works).
     5It uses liburing for simplicity.
     6*/
     7
     8
    19#include <errno.h>
    210#include <fcntl.h>
     
    917
    1018int main(int argc, char * argv[]) {
    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]);
     19        if(argc != 3 && argc != 4) {
     20            printf("usage:   %s FILE TIMES [fixed] - read FILE from disk TIMES times\n", argv[0]);
    1821            return EXIT_FAILURE;
    1922      }
    2023
     24        bool fixed = false;
     25        if(argc == 4) {
     26                fixed = 0 == strcmp(argv[3], "fixed");
     27        }
    2128
    2229      int times = atoi( argv[2] );
     
    3239      }
    3340
     41        int rfd = fd;
     42
    3443        /* prep the array */
    3544      char data[100];
     
    4049      io_uring_queue_init(256, &ring, 0);
    4150
     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
    4271      /* declare required structs */
    4372        printf("Reading %s(%d) %d times\n", argv[1], fd, times);
     
    4675                /* get an sqe and fill in a READV operation */
    4776              struct io_uring_sqe * sqe = io_uring_get_sqe(&ring);
    48                 io_uring_prep_readv(sqe, fd, &iov, 1, 0);
     77                io_uring_prep_readv(sqe, rfd, &iov, 1, 0);
     78                if(fixed) {
     79                        sqe->flags = IOSQE_FIXED_FILE;
     80                }
    4981
    5082                /* tell the kernel we have an sqe ready for consumption */
     
    83115
    84116      close(fd);
     117
     118        if(fixed) {
     119                close(pipefds[0]);
     120                close(pipefds[1]);
     121        }
    85122}
Note: See TracChangeset for help on using the changeset viewer.