linux下异步IO的简单例子【转】
2016-09-06 22:35
369 查看
转自:http://blog.chinaunix.net/uid-24567872-id-87677.html首先,贴一下异步IO中用的的一些结构体,因为平常很少用,整理起来方便查看。aio.h中的struct aiocb
siginfo.h中的struct sigevent和
例子1:
运行结果:注意:要加相应的库,-lrt
例子2:
运行结果同上。
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; }; |
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; |
#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;} |
$ ./gcc -o test aio_signal.c -lrt$ ./test01h2ell3oread=hello4^C |
#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, _IOR, _IOW, _IOWR 宏解析
- linux 内核驱动编程 简单例子 与_IO, _IOR, _IOW, _IOWR 宏解析
- linux 内核驱动编程 简单例子 与_IO, _IOR, _IOW, _IOWR 宏解析
- linux 内核驱动编程 简单例子 与_IO, _IOR, _IOW, _IOWR 宏解析
- linux socket的IO多路复用简单例子(四)
- linux socket的IO多路复用简单例子(一)
- linux 内核驱动编程 简单例子 与_IO, _IOR, _IOW, _IOWR 宏解析
- 一个简单的Linux下Client/Server应答例子
- Linux DirectFB DFB 简单 初始化 例子 代码
- 一个关于linux socket的简单例子
- Samba 安装及简单配置、windows访问linux的简单例子
- 一个简单的Linux下Client/Server应答例子
- 一个关于linux socket的简单例子
- Linux异步IO支持
- 一个简单的Linux下Client/Server应答例子
- [Linux] 最简单的socket通讯例子
- linux 下net-snmp简单例子
- Linux下C语言引用shell脚本的简单例子
- linux/unix文件编程---简单例子
- 简单的linux c socket例子