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