转自: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. POJ 1035题目描述

    Description You, as a member of a development team for a new spell checking program, are to write a ...

  2. Apache中压力测试工具ab的操作说明

    1.压力测试工具ab(ApacheBench)的简单说明 1)     网站性能压力测试是性能调优过程中必不可少的一环.只有让服务器处在高压情况下才能真正体现出各种设置所暴露的问题.Apache中有个 ...

  3. linux设备模型_转

    建议原博文查看,效果更佳. 转自:http://www.cnblogs.com/wwang/category/269350.html Linux设备模型 (1) 随着计算机的周边外设越来越丰富,设备管 ...

  4. 计算NSString含有多少个相同字符串

    // //  NSString+NSStringExt.m //  01-NSString类 // //  Created by apple on 14-3-20. //  Copyright (c) ...

  5. windows下使用pthreads

    pthread-win32在Windows上实现了线程相关的Posix标准,接口一模一样 包含: thread mutex cond swlock spin sem barrier https://s ...

  6. django上传下载大文件

    上传 def upFile(file): upload_dir = '/tmp/upload/%s' % username if request.method == 'POST': upload_fi ...

  7. Python:安装mssql模块功能,并实现与sqlserver连接、查询

    由于我系统是x64系统,所以下载python2.7 x64.下载地址:https://www.python.org/downloads/release/python-2712/, 经过测试发现这个版本 ...

  8. Lintcode: Partition Array

    Given an array "nums" of integers and an int "k", Partition the array (i.e move ...

  9. Excel操作增强包

    一.前期准备1)  pywin32安装包(根据系统要求选择32位装包还是64位安装包)2)  xlwings安装包二.安装过程1)  pywin32安装包为exe文件,直接进行安装即可2)  xlwi ...

  10. 20145207 《Java程序设计》第二周学习总结

    开源中国的代码托管 不算调查问卷的话,这是第二篇博客,怎么说呢……感觉好麻烦!哈哈哈哈!不过也就这样吧.按照同学传达的老师的意思就是“写博客就是在重复一天的所学,虽然可能会花一定的时间,但是对于自己是 ...