转自:http://blog.chinaunix.net/uid-24567872-id-87677.html

首先,贴一下异步IO中用的的一些结构体,因为平常很少用,整理起来方便查看。

aio.h中的struct aiocb

struct aiocb
{
  int aio_fildes;        /* File desriptor. */
  int aio_lio_opcode;        /* Operation to be performed. */
  int aio_reqprio;        /* Request priority offset. */
  volatile void *aio_buf;    /* Location of buffer. */
  size_t aio_nbytes;        /* Length of transfer. */
  struct sigevent aio_sigevent;    /* Signal number and value. */

/* Internal members. */
  struct aiocb *__next_prio;
  int __abs_prio;
  int __policy;
  int __error_code;
  __ssize_t __return_value;
};

siginfo.h中的struct sigevent和union sigval

typedef struct sigevent
  {
    sigval_t sigev_value;
    int sigev_signo;
    int sigev_notify;

union
      {
    int _pad[__SIGEV_PAD_SIZE];

/* When SIGEV_SIGNAL and SIGEV_THREAD_ID set, LWP ID of the
     thread to receive the signal. */
    __pid_t _tid;

struct
     {
     void (*_function) (sigval_t);    /* Function to start. */
     void *_attribute;            /* Really pthread_attr_t. */
     } _sigev_thread;
      } _sigev_un;
  } sigevent_t;

/* POSIX names to access some of the members. */
# define sigev_notify_function _sigev_un._sigev_thread._function
# define sigev_notify_attributes _sigev_un._sigev_thread._attribute

typedef union sigval
  {
    int sival_int;
    void *sival_ptr;
  } sigval_t;

例子1:

#include <aio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>

void async_read(int s, siginfo_t * info, void * context)
{
    struct aiocb *ptr = 
        (struct aiocb *)info->si_value.sival_ptr;
    printf("read=%s", (char *)ptr->aio_buf);    
}

int main(void)
{
    struct aiocb cb;
    char sbuf[100];
    int ret;
    struct sigaction act;
    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_RESTART | SA_SIGINFO;
    act.sa_sigaction = async_read;

sigaction(SIGUSR1, &act, NULL);

bzero(&cb, sizeof(cb));

cb.aio_fildes = 0;
    cb.aio_buf = sbuf;
    cb.aio_nbytes = 100;
    cb.aio_offset = 0;

cb.aio_sigevent.sigev_value.sival_ptr = &cb;
    cb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
    cb.aio_sigevent.sigev_signo = SIGUSR1;
    ret = aio_read(&cb);
    if (ret == -1) {
        perror("aio_read");
        exit(1);
    }
    int i = 0;
    while (1) {
        printf("%d\n",i++);
        sleep(3);
    }

return 0;
}

运行结果:
注意:要加相应的库,-lrt

 $ ./gcc -o test aio_signal.c -lrt 

$ ./test
0
1
h2
ell3
o
read=hello
4
^C

例子2:

#include <aio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

void async_read(sigval_t val)
{
    struct aiocb *ptr = 
        (struct aiocb *)val.sival_ptr;
    printf("read=%s", (char *)ptr->aio_buf);    
}

int main(void)
{
    struct aiocb cb;
    char sbuf[100];
    int ret;

bzero(&cb, sizeof(cb));

cb.aio_fildes = 0;
    cb.aio_buf = sbuf;
    cb.aio_nbytes = 100;
    cb.aio_offset = 0;

cb.aio_sigevent.sigev_value.sival_ptr = &cb;
    cb.aio_sigevent.sigev_notify = SIGEV_THREAD;
    cb.aio_sigevent.sigev_notify_function = 
        async_read;
    cb.aio_sigevent.sigev_notify_attributes = NULL;

ret = aio_read(&cb);
    if (ret == -1) {
        perror("aio_read");
        exit(1);
    }
    
    int i = 0;
    while (1) {
        printf("%d\n",i++);
        sleep(1);
    }

return 0;
}

运行结果同上。

linux下异步IO的简单例子【转】的更多相关文章

  1. Linux下libaio的一个简单例子

    转载:http://www.cnblogs.com/aLittleBitCool/archive/2011/10/18/2216646.html 异步io,很好玩的一个东西,从接口来看,封装的比较厉害 ...

  2. linux 下 异步IO

    方法一:使用fcntl来置O_ASYNC位. 这个方法的效果是,当输入缓存中的输入数据就绪时(输入数据可读),内核向用F_SETOWN来绑定的那个进程发送SIGIO信号.此时程序应该用getchar等 ...

  3. Linux下的IO监控与分析

    Linux下的IO监控与分析 近期要在公司内部做个Linux IO方面的培训, 整理下手头的资料给大家分享下 各种IO监视工具在Linux IO 体系结构中的位置 源自 Linux Performan ...

  4. linux下的IO模型---学习笔记

    1.linux文件系统和缓存 文件系统接口 文件系统-一种把数据组织成文件和目录的存储方式,提供了基于文件的存取接口,并通过文件权限控制访问. 存储层次 文件系统缓存 主存(通常时DRAM)的一块区域 ...

  5. Linux下的IO模式

    对于一次IO访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间.所以说,当一个read操作发生时,它会经历两个阶段:1. 等待数据准 ...

  6. Linux下面的IO模型

    1. Linux下的五种I/O模型 阻塞I/O模型: 一直阻塞      应用程序调用一个IO函数,导致应用程序阻塞,等待数据准备好. 如果数据没有准备好,一直等待….数据准备好了,从内核拷贝到用户空 ...

  7. Linux下getopt()函数的简单使用

    最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点,但确实是忠告.步入正题: 我们的主角----getopt()函数. 英雄不问出处,getopt()函数的 ...

  8. 【转载】Linux下的IO监控与分析

    近期要在公司内部做个Linux IO方面的培训, 整理下手头的资料给大家分享下 各种IO监视工具在Linux IO 体系结构中的位置 源自 Linux Performance and Tuning G ...

  9. Linux下控制器IO地址

    在Linux下使用cat /proc/ioports可以查看控制器使用的IO地址范围

随机推荐

  1. Android 菜单(OptionMenu)

    菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 一.概述 ...

  2. Inside Kolla - 05 从 tools 目录开始

    从 tools 目录开始 在上一篇中已介绍了 Kolla 的源代码目录结构和各个目录中源代码的基本作用,现在开始深入到各个目录,介绍目录中的具体内容.每个人分析代码的思路和方法不同,会选择从不同的目录 ...

  3. jboss漏洞导致服务器中毒

    中毒现象 1. 网络出现拥塞,访问延迟增加. 2. 系统定时任务表中出现异常的定时任务. 3. 出现异常进程. 4. $JBOSS_HOME/bin或/root目录下出现大量的异常文件.     现象 ...

  4. 再探Java基础——throw与throws

    http://blog.csdn.net/luoweifu/article/details/10721543 异常处理机制 异常处理是对可能出现的异常进行处理,以防止程序遇到异常时被卡死,处于一直等待 ...

  5. 面向对象编程(九)——面向对象三大特性之继承以及重写、Object类的介绍

    面向对象三大特性 面向对象三大特征:继承 :封装/隐藏 :多态(为了适应需求的多种变化,使代码变得更加通用!) 封装:主要实现了隐藏细节,对用户提供访问接口,无需关心方法的具体实现. 继承:很好的实现 ...

  6. VS2013+qt-vs-addin-1.2.4安装配置

    --------------------------开头这部分可以跳过,仅作为笔记----------------------- 我问过的问题: http://www.codeproject.com/ ...

  7. nodejs weixin 笔记

    http://www.oschina.net/code/snippet_218887_25870 好文章: http://codelife.me/blog/2013/04/23/developing- ...

  8. 在xocde运行profile 遇到"Existing default base temp directory '/Library/Caches/com.apple.dt.instruments' has insufficient privileges for user id 505. Please have the owner delete this directory"

    找到这篇文章 http://stackoverflow.com/questions/34153795/xcode-7-unable-to-open-instruments-from-developer ...

  9. Effective C++ 4.设计与声明

    //条款18:让接口容易被正确使用,不易被误用 // 1.如果客户企图使用某个接口而却没有获得他所预期的行为,那么这个代码就不该通过编译. // 2.促进正确使用的方法包括接口的一致性,以及与内置类型 ...

  10. Lintcode: Minimum Adjustment Cost

    Given an integer array, adjust each integers so that the difference of every adjcent integers are no ...