linux下异步IO的简单例子【转】

简介:

转自: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;
}

运行结果同上。










本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sky-heaven/p/5847456.html,如需转载请自行联系原作者


相关文章
|
11天前
|
存储 缓存 Linux
Linux IO的奥秘:深入探索数据流动的魔法
Linux I/O(输入/输出)系统是其核心功能之一,负责处理数据在系统内部及与外界之间的流动。为了优化这一流程,Linux进行了一系列努力和抽象化,以提高效率、灵活性和易用性。🚀
Linux IO的奥秘:深入探索数据流动的魔法
|
18天前
|
算法 数据处理 Python
Python并发编程:解密异步IO与多线程
本文将深入探讨Python中的并发编程技术,重点介绍异步IO和多线程两种常见的并发模型。通过对比它们的特点、适用场景和实现方式,帮助读者更好地理解并发编程的核心概念,并掌握在不同场景下选择合适的并发模型的方法。
|
30天前
|
存储 监控 Linux
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
50 0
|
9天前
|
Linux
Linux操作系统调优相关工具(三)查看IO运行状态相关工具 查看哪个磁盘或分区最繁忙?
Linux操作系统调优相关工具(三)查看IO运行状态相关工具 查看哪个磁盘或分区最繁忙?
20 0
|
11天前
|
存储 缓存 安全
Linux IO:打开数据之窗的魔法
Linux I/O(输入/输出)是操作系统中一个至关重要的组成部分,它涉及到数据在内存🧠、存储设备💾、网络接口🌐等之间的传输过程。在Linux中,I/O操作不仅仅是文件读写那么简单,它包括了一系列复杂的机制和策略,旨在提高数据处理的效率,保证系统的稳定性和性能。📊
Linux IO:打开数据之窗的魔法
|
23天前
|
监控 算法 Unix
【Linux 异步操作】深入理解 Linux 异步通知机制:原理、应用与实例解析
【Linux 异步操作】深入理解 Linux 异步通知机制:原理、应用与实例解析
56 0
|
24天前
|
Unix Linux C++
【C/C++ 造轮子】Linux异步计时器:深入探讨和应用 (Linux Asynchronous Timers: An In-depth Exploration and Application)
【C/C++ 造轮子】Linux异步计时器:深入探讨和应用 (Linux Asynchronous Timers: An In-depth Exploration and Application)
53 1
|
30天前
|
NoSQL Java Linux
【Linux IO多路复用 】 Linux 网络编程 认知负荷与Epoll:高性能I-O多路复用的实现与优化
【Linux IO多路复用 】 Linux 网络编程 认知负荷与Epoll:高性能I-O多路复用的实现与优化
60 0
|
1月前
|
调度 数据库 Python
Python中的并发编程:使用asyncio库实现异步IO
传统的Python程序在面对IO密集型任务时,往往会遇到性能瓶颈。本文将介绍如何利用Python中的asyncio库,通过异步IO的方式来提升程序的效率和性能,让你的Python程序能够更好地处理并发任务。