转自: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. asm/aam links

    http://personalpages.manchester.ac.uk/staff/timothy.f.cootes/asm_links.html

  2. iTOP-4412 开发板镜像的烧写

    镜像就是源代码编译并连接以后生成的可执行文件包,把这些镜像文件烧写到开发板的存储芯片里,开机就可以运行了. 烧写方式有两种,通过TF卡烧写以及使用OTG接口烧写 OTG方式:只能在WIN7 或者XP ...

  3. 仅使用处理单个数字的I/O例程,编写一个过程以输出任意实数(可以是负的)

    题目取自:<数据结构与算法分析:C语言描述_原书第二版>——Mark Allen Weiss   练习1.3 如题. 补充说明:假设仅有的I/O例程只处理单个数字并将其输出到终端,我们将这 ...

  4. Effective C++ 3.资源管理

    //条款13:以对象管理资源 // 1.C++程序中最常使用的资源就是动态分配内存,并且还包括文件描述器,互斥锁,GDI对象.数据库连接.网络socket等.不管哪一种资源,当不再使用的时候必须将其归 ...

  5. smarty -- foreach用法

    {foreach},{foreachelse} 用于像访问序数数组一样访问关联数组 {foreach},{foreachelse} {foreach} is used to loop over an  ...

  6. 转:Java的各种类型转换汇总

    java类型转换 Integer String Long Float Double Date 1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Intege ...

  7. [reprint]如何编写引导程序 Hello World

    在存储介质(硬盘.软盘.光盘)中有一块特殊的区域,叫做引导区.在计算机启动后,BIOS会读取引导区内的代码到内存中去,然后将执行这些代码.引导区的位置和大小与计算机的平台有关,对于IBM-PC兼容机, ...

  8. Java基础(39):数据的四舍五入、去整、产生随机数---Math类的应用

    使用 Math 类操作数据 Math 类位于 java.lang 包中,包含用于执行基本数学运算的方法, Math 类的所有方法都是静态方法,所以使用该类中的方法时,可以直接使用类名.方法名,如: M ...

  9. Json lib集成stucts2的使用方法 抛出 NestableRuntimeException异常的解决办法

    首先贴出struts 2.3.16需要导入的包 因为使用的是2.3 版本,必须要导入这个包,否则会报java.lang.NoClassDefFoundError: org/apache/commons ...

  10. extjs中的下载并对文件重命名功能的实现

    在小白的学习extjs的过程中,如果需要了解多文件的上传功能,也可以查看小白的上篇随笔,希望给大家帮助.http://www.cnblogs.com/wangqc/p/extjsFileUpload. ...