Changeset 22f94a4 for examples/io/cat.c


Ignore:
Timestamp:
Aug 11, 2020, 4:40:15 PM (5 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:
0d070ca
Parents:
07d867b (diff), 129674b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into new-ast

File:
1 moved

Legend:

Unmodified
Added
Removed
  • examples/io/cat.c

    r07d867b r22f94a4  
     1/*
     2This is a simple "cat" example that uses io_uring in IORING_SETUP_IOPOLL mode.
     3It demonstrates the bare minimum needed to use io_uring in polling mode.
     4It uses liburing for simplicity.
     5*/
     6
     7
     8#ifndef _GNU_SOURCE
     9#define _GNU_SOURCE
     10#endif
     11
    112#include <fcntl.h>
    213#include <liburing.h>
    314#include <stdio.h>
     15#include <string.h>
    416#include <unistd.h>
    517
    618struct io_uring ring;
     19
     20__attribute__((aligned(1024))) char data[1024];
    721
    822int main(int argc,  char * argv[]) {
     
    1226      }
    1327
    14       int fd = open(argv[1], 0);
     28      int fd = open(argv[1], O_DIRECT);
    1529      if(fd < 0) {
    1630            printf("Could not open file %s.\n", argv[1]);
     
    1933
    2034      /* prep the array */
    21       char data[512];
    22       struct iovec iov = { data, 512 };
     35      struct iovec iov = { data, 1024 };
    2336
    2437      /* init liburing */
    25       io_uring_queue_init(256, &ring, 0);
     38      io_uring_queue_init(256, &ring, IORING_SETUP_IOPOLL);
    2639
    2740      /* declare required structs */
     
    3245      sqe = io_uring_get_sqe(&ring);
    3346      io_uring_prep_readv(sqe, fd, &iov, 1, 0);
     47      // io_uring_prep_read(sqe, fd, data, 1024, 0);
    3448
    35       sqe->user_data = data;
     49      sqe->user_data = (uint64_t)(uintptr_t)data;
    3650
    3751      /* tell the kernel we have an sqe ready for consumption */
     
    4054      /* wait for the sqe to complete */
    4155      int ret = io_uring_wait_cqe(&ring, &cqe);
    42      
     56
    4357      /* read and process cqe event */
    4458      if(ret == 0) {
    45             char * out = cqe->user_data;
     59            char * out = (char *)(uintptr_t)cqe->user_data;
    4660            signed int len = cqe->res;
    4761            io_uring_cqe_seen(&ring, cqe);
     
    4963            if(len > 0) {
    5064                  printf("%.*s", len, out);
     65            }
     66            else if( len < 0 ) {
     67                  fprintf(stderr, "readv/read returned error : %s\n", strerror(-len));
    5168            }
    5269      }
Note: See TracChangeset for help on using the changeset viewer.