• 一个epoll/aio/eventfd结合使用的简单例子_sunfly_新浪博客


    一个epoll/aio/eventfd结合使用的简单例子_sunfly_新浪博客

        一个epoll/aio/eventfd结合使用的简单例子
        (2012-05-24 00:37:41)
        转载▼
        标签:
        epoll
        libaio
        eventfd
        杂谈
           
        #define _GNU_SOURCE
        #define __STDC_FORMAT_MACROS
        #include <stdio.h>
        #include <errno.h>
        #include <libaio.h>
        #include <sys/eventfd.h>
        #include <sys/epoll.h>
        #include <stdlib.h>
        #include <sys/types.h>
        #include <unistd.h>
        #include <stdint.h>
        #include <sys/stat.h>
        #include <fcntl.h>
        #include <inttypes.h>
        #define TEST_FILE   "aio_test_file"
        #define TEST_FILE_SIZE  (127 * 1024)
        #define NUM_EVENTS  128
        #define ALIGN_SIZE  512
        #define RD_WR_SIZE  1024
        struct custom_iocb
        {
            struct iocb iocb;
            int nth_request;
        };
        void aio_callback(io_context_t ctx, struct iocb *iocb, long res, long res2)
        {
            struct custom_iocb *iocbp = (struct custom_iocb *)iocb;
            printf("nth_request: %d, request_type: %s, offset: %lld, length: %lu, res: %ld, res2: %ld\n",
                    iocbp->nth_request, (iocb->aio_lio_opcode == IO_CMD_PREAD) ? "READ" : "WRITE",
                    iocb->u.c.offset, iocb->u.c.nbytes, res, res2);
        }
        int main(int argc, char *argv[])
        {
            int efd, fd, epfd;
            io_context_t ctx;
            struct timespec tms;
            struct io_event events[NUM_EVENTS];
            struct custom_iocb iocbs[NUM_EVENTS];
            struct iocb *iocbps[NUM_EVENTS];
            struct custom_iocb *iocbp;
            int i, j, r;
            void *buf;
            struct epoll_event epevent;
            efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
            if (efd == -1) {
                perror("eventfd");
                return 2;
            }
            fd = open(TEST_FILE, O_RDWR | O_CREAT | O_DIRECT, 0644);
            if (fd == -1) {
                perror("open");
                return 3;
            }
            ftruncate(fd, TEST_FILE_SIZE);
           
            ctx = 0;
            if (io_setup(8192, &ctx)) {
                perror("io_setup");
                return 4;
            }
            if (posix_memalign(&buf, ALIGN_SIZE, RD_WR_SIZE)) {
                perror("posix_memalign");
                return 5;
            }
            printf("buf: %p\n", buf);
            for (i = 0, iocbp = iocbs; i < NUM_EVENTS; ++i, ++iocbp) {
                iocbps[i] = &iocbp->iocb;
                io_prep_pread(&iocbp->iocb, fd, buf, RD_WR_SIZE, i * RD_WR_SIZE);
                io_set_eventfd(&iocbp->iocb, efd);
                io_set_callback(&iocbp->iocb, aio_callback);
                iocbp->nth_request = i + 1;
            }
            if (io_submit(ctx, NUM_EVENTS, iocbps) != NUM_EVENTS) {
                perror("io_submit");
                return 6;
            }
            epfd = epoll_create(1);
            if (epfd == -1) {
                perror("epoll_create");
                return 7;
            }
            epevent.events = EPOLLIN | EPOLLET;
            epevent.data.ptr = NULL;
            if (epoll_ctl(epfd, EPOLL_CTL_ADD, efd, &epevent)) {
                perror("epoll_ctl");
                return 8;
            }
            i = 0;
            while (i < NUM_EVENTS) {
                uint64_t finished_aio;
                if (epoll_wait(epfd, &epevent, 1, -1) != 1) {
                    perror("epoll_wait");
                    return 9;
                }
                if (read(efd, &finished_aio, sizeof(finished_aio)) != sizeof(finished_aio)) {
                    perror("read");
                    return 10;
                }
                printf("finished io number: %"PRIu64"\n", finished_aio);
           
                while (finished_aio > 0) {
                    tms.tv_sec = 0;
                    tms.tv_nsec = 0;
                    r = io_getevents(ctx, 1, NUM_EVENTS, events, &tms);
                    if (r > 0) {
                        for (j = 0; j < r; ++j) {
                            ((io_callback_t)(events[j].data))(ctx, events[j].obj, events[j].res, events[j].res2);
                        }
                        i += r;
                        finished_aio -= r;
                    }
                }
            }
           
            close(epfd);
            free(buf);
            io_destroy(ctx);
            close(fd);
            close(efd);
            remove(TEST_FILE);
            return 0;
        }
        说明:
        1. 在centos 6.2 (libaio-devel 0.3.107-10) 上运行通过
        2. struct io_event中的res字段表示读到的字节数或者一个负数错误码。在后一种情况下,-res表示对应的
           errno。res2字段为0表示成功,否则失败
        3. iocb在aio请求执行过程中必须是valid的
        4. 在上面的程序中,通过扩展iocb结构来保存额外的信息(nth_request),并使用iocb.data
           来保存回调函数的地址。如果回调函数是固定的,那么也可以使用iocb.data来保存额外信息。

    unsigned int 与 unsigned long 一样吗? - candycaicai - 博客园

     

    unsigned long int

    unsigned long int在C语言中是无符号长整形变量,是整形变量的一种。
          unsigned long int 与unsigned long是等价的,即定义的时候int可以不写。  C语言字节数为4,与long型数据一样。
          unsigned long int的取值范围: 0~4294967295 即 0~(2的32次方-1)
          unsigned 是无符号  long是长  int是整型数据  unsigned long int 就是无符号长整型数据

    unsigned

    整型的每一种都有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如果需声明无符号类型的话就需要在类型前加上unsigned。无符号版本和有符号版本的区别就是无符号类型能保存2倍于有符号类型的数据,比如16位系统中一个int能存储的数据的范围为-32768~32767,而unsigned能存储的数据范围则是0~65535。由于在计算机中,整数是以补码形式存放的。根据最高位的不同,如果是1,有符号数的话就是负数拉;如果是无符号数,则都解释为正数。同时在相同位数的情况下,所能表达的整数范围变大。另外,unsigned若省略后一个关键字,大多数编译器都会认为是unsigned int

    int

     int数据类型,在32位系统中(现在基本上int都是32位),范围-2147483648~+2147483647。unsigned类型 的int 范围:0~4294967295 即 0~(2的32次方-1)
      
     所以我觉得unsigned int 与 unsigned long 在一般情况下是一样的!
  • 相关阅读:
    10个好用的 HTML5 特性
    数字营销2.0时代已到来
    云原生时代,.NET5必将称王!
    为什么需要堡垒机
    从SOA和微服务到云原生解决方案实践
    阿里云资深技术专家李克畅谈边缘云计算趋势与实践
    OpenKruise v0.9.0 版本发布:新增 Pod 重启、删除防护等重磅功能
    浅谈分布式一致性:Raft 与 SOFAJRaft
    数仓架构的持续演进与发展 — 云原生、湖仓一体、离线实时一体、SaaS模式
    如何基于MaxCompute快速打通数据仓库和数据湖的湖仓一体实践
  • 原文地址:https://www.cnblogs.com/lexus/p/2856115.html
Copyright © 2020-2023  润新知