首先,贴一下异步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("%dn",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("%dn",i++);  sleep(1);  }  return 0;
}

结果和例子1相同

linux下异步IO的简单例子相关推荐

  1. Linux下异步IO(libaio)的使用以及性能

    Linux下异步IO是比较新的内核里面才有的,异步io的好处可以参考这里. 但是文章中aio_*系列的调用是glibc提供的,是glibc用线程+阻塞调用来模拟的,性能很差,千万千万不要用. 我们今天 ...

  2. Linux 原生异步 IO 原理与使用

    目录 什么是异步 IO? Linux 原生 AIO 原理 Linux 原生 AIO 使用 什么是异步 IO? 异步 IO:当应用程序发起一个 IO 操作后,调用者不能立刻得到结果,而是在内核完成 IO ...

  3. linux使用flask设计网站,linux下Flask框架搭建简单网页

    开始安装FLASK需要创建一个虚拟环境,虚拟环境可以不干扰正在使用的系统环境,避免影响,并且也不需要完全的root权限,更加安全可靠. 搭建环境 Python3.4 进入到microblog目录下创建 ...

  4. Linux 下UVCamp;V4L2技术简单介绍(二)

    通过前文Linux 下UVC&V4L2技术简单介绍(一)我们了解了UVC和V4L2的简单知识. 这里是USB设备的文档描写叙述:http://www.usb.org/developers/do ...

  5. linux编写一个简单的端口扫描程序,小弟我在linux下写了个简单的多线程端口扫描程序,运行时出现有关问题,请问一下(2)...

    当前位置:我的异常网» Linux/Unix » 小弟我在linux下写了个简单的多线程端口扫描程序, 小弟我在linux下写了个简单的多线程端口扫描程序,运行时出现有关问题,请问一下(2) www. ...

  6. Linux下面的IO模型

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

  7. Linux下磁盘IO读写测试工具-FIO详解

    FIO简介 FIO是Linux下开源的一款IOPS测试工具,主要用来对磁盘进行压力测试和性能验证. 它可以产生许多线程或进程来执行用户特定类型的I/O操作,通过编写作业文件(类似于k8s的yaml)或 ...

  8. windows下异步IO一

    介绍 简单讲解下我们程序进行IO的过程,当线程进行一个同步的设备IO请求时,他会被挂起,直到设备完成IO请求,返回给阻塞线程,线程激活继续处理.当进行一个异步的设备IO请求时,该线程可以先去做其他事, ...

  9. [转载]Linux下getopt()函数的简单使用

    转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...

最新文章

  1. 初识FPGA(一)(初步介绍FPGA)
  2. numpy.linalg.svd
  3. 文件/目录权限相关命令:chmod、chown、umask、lsattr/chattr命令解析
  4. 用PHPcms V9四步完成WAP手机站搭建
  5. java笔试题(一):斐波那契数列
  6. [渝粤教育] 长沙民政职业技术学院 高职公共英语(一) 参考 资料
  7. 初探Backbone
  8. 教你如何塑造JavaScript牛逼形象
  9. 微信小程序开发学习笔记006--微信小程序组件详解02
  10. 如何在Mac上备份和共享文本替换?
  11. QT编程入门之QT designer
  12. 【Python】利用tkinter开发AI对战井字棋游戏
  13. vue-动手做个选择城市
  14. hao.360.cn不停跳....
  15. Python WEB 开发,什么是 WSGI ?uWSGI、Gunincorn 都是啥玩意儿?
  16. Android App接入支付功能
  17. Pytorch+cpp_cuda extension 课程二
  18. java截取视频片段_使用javacv 截取视频指定帧节
  19. LTspice基础教程-020.绘制伯德图
  20. flowable中BPM实现核心对象

热门文章

  1. 百度车牌识别API-Python版
  2. python使用正则表达式删除字符串中的数字
  3. jupyter 功能插件
  4. android广播内容显示在屏幕上,在Android本机来电屏幕上弹出窗口,例如真正的来电者Android应用...
  5. 项目Beta冲刺 随笔集合
  6. Spring Boot 2.2 增加了一个新功能,启动飞起~
  7. vue-router组件重用 路由切换时的问题
  8. shell export 命令
  9. MATLAB生成正弦波
  10. Python 三元表达式、列表推导式、生成器表达式