Changes in examples/io/filereader.c [5b15c4f:2a35f14]
- File:
-
- 1 edited
-
examples/io/filereader.c (modified) (6 diffs)
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 9 1 #include <errno.h> 10 2 #include <fcntl.h> … … 17 9 18 10 int 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]); 21 18 return EXIT_FAILURE; 22 19 } 23 20 24 bool fixed = false;25 if(argc == 4) {26 fixed = 0 == strcmp(argv[3], "fixed");27 }28 21 29 22 int times = atoi( argv[2] ); … … 39 32 } 40 33 41 int rfd = fd;42 43 34 /* prep the array */ 44 35 char data[100]; … … 49 40 io_uring_queue_init(256, &ring, 0); 50 41 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 71 42 /* declare required structs */ 72 43 printf("Reading %s(%d) %d times\n", argv[1], fd, times); … … 75 46 /* get an sqe and fill in a READV operation */ 76 47 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); 81 49 82 50 /* tell the kernel we have an sqe ready for consumption */ … … 115 83 116 84 close(fd); 117 118 if(fixed) {119 close(pipefds[0]);120 close(pipefds[1]);121 }122 85 }
Note:
See TracChangeset
for help on using the changeset viewer.