I/O复用
来源:互联网 发布:公司记账软件 知乎 编辑:程序博客网 时间:2024/06/03 12:41
select系统调用:
select系统调用的用途是:在一段时间内,监听用户感兴趣的文件描述符上的可读,可写和异常等事件。
select系统调用的原型如下:
int select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout);
(1)nfds指定被监控的文件描述符的总数。
(2)readfds,writefds,exceptfds参数分别指向可读,可写和异常等时间对应的文件描述符集合。
(3)timeout参数用来设置select函数的超时时间.
select成功时返回就绪(可读,可写和异常)文件描述符的总数。若果在超时时间内没用任何文件描述符就绪,select将返回0,select失败时返回-1并设置errno。如果在 select等待时间内,程序接受到信号,则select立即-1,并设置errno为EINTER。
fd_set结构的定义如下:
#include
#define __FD_SETSIZE 1024
#include
#define FD_SETSIZE __FD_SETSIZE
typedef long int __fd_mask;
#undef __NFDBITS
#dfine NFDBITS (8 * (int)sizeof(__fd_mask)
typedef sruct
{
#ifdef __use_xopen
__fd_mask fds_bits[__FD_SETSIZE /__NFDBITS];
#define __FDS_BITS(set) ((set)->fds_bits)
#else
__fd_mask __fds_bits[__FD_SETSIZE/__NFDBITS]
#define __FDS_BITS(set) ((set)->__fds_bits);
#endif
} fd_set;
用以上定义可见,fd_set结构体仅包含一个整形数组,该数组的每个元素的每一位标记一个文件描述符。fd_set能容纳的文件描述符数量由FD_SETSIZE指定,这就限制了select能同时处理的文件描述符的总量。
poll系统调用:
poll系统调用和select类似,也是在指定时间内轮询一定数量的文件描述符,以测试其中是否有就绪者。poll的原型如下:
1)fds参数是一个pollfd结构类型的数组,它指定所有我们感兴趣的文件描述符上发生的可读,可写和异常等事件。pollfd结构体定义如下:
struct pollfd
{
int fd;//文件描述符
short events;//注册的事件
short revents;//实际发生的事件,由内核填充
};
poll事件类型 POLLIN POLLOUT POLLERR POLLRDHUP等。
2)nfds参数指定被监听事件集合fds的大小。
typedef unsigned long nfds_t;
3)timeout参数指定poll的超时值,单位是毫秒。timeout为-1,poll调用将永远阻塞,直到某个事件发生;当timeout为0时,poll 调用将立即返回。
返回值含义与select相同。
poll的一个简单应用:运行结果如下:
epoll是linux特有的I/O复用函数。它在实现上和使用上与select,poll有很大区别。首先epoll用一组函数来完成任务,而不是单个函数,其次epoll把用户关心的文件描述符上的事件放在内核里的一个事件表中。
epoll需要使用一个额外的文件描述符,来唯一标识内核中的这个事件表。这个文件描述符使用如下函数创建:
size参数只是给内核一个提示,告诉它事件表有多大。
下面的函数用来操作epoll的内核事件表
fd参数是要操作的文件描述符,op参数指定操作类型,event参数指定事件。
epoll_event定义如下:
struct epoll_event
{
__uint32_t events; /*epoll事件*/
epoll_data_t data; //用户数据
};
typedef union epoll_data
{
void *ptr;
int fd;//使用做多的
uint32_t u32;
uint64_t u64;
}epoll_data_t;
epoll_ctl成功时返回0,失败则返回-1并设置errno。
epoll系统调用的主要接口是epoll_wait函数,它在一段超时时间内等待一组文件描述符上的事件,其原型如下:
成功时返回就绪的文件描述符个数,失败时返回-1并设置errno的值。
maxevents参数指定最多监听多少个事件。
epoll_wait函数如果检测到事件,就将所有的就绪事件从内核事件表中复制到它的第二个参数events指向的数组中。这个数组只用于输出epoll检测到的就绪事件。
select系统调用的用途是:在一段时间内,监听用户感兴趣的文件描述符上的可读,可写和异常等事件。
select系统调用的原型如下:
int select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout);
(1)nfds指定被监控的文件描述符的总数。
(2)readfds,writefds,exceptfds参数分别指向可读,可写和异常等时间对应的文件描述符集合。
(3)timeout参数用来设置select函数的超时时间.
select成功时返回就绪(可读,可写和异常)文件描述符的总数。若果在超时时间内没用任何文件描述符就绪,select将返回0,select失败时返回-1并设置errno。如果在 select等待时间内,程序接受到信号,则select立即-1,并设置errno为EINTER。
fd_set结构的定义如下:
#include
#define __FD_SETSIZE 1024
#include
#define FD_SETSIZE __FD_SETSIZE
typedef long int __fd_mask;
#undef __NFDBITS
#dfine NFDBITS (8 * (int)sizeof(__fd_mask)
typedef sruct
{
#ifdef __use_xopen
__fd_mask fds_bits[__FD_SETSIZE /__NFDBITS];
#define __FDS_BITS(set) ((set)->fds_bits)
#else
__fd_mask __fds_bits[__FD_SETSIZE/__NFDBITS]
#define __FDS_BITS(set) ((set)->__fds_bits);
#endif
} fd_set;
用以上定义可见,fd_set结构体仅包含一个整形数组,该数组的每个元素的每一位标记一个文件描述符。fd_set能容纳的文件描述符数量由FD_SETSIZE指定,这就限制了select能同时处理的文件描述符的总量。
poll系统调用:
poll系统调用和select类似,也是在指定时间内轮询一定数量的文件描述符,以测试其中是否有就绪者。poll的原型如下:
点击(此处)折叠或打开
- #include<poll.h>
- int poll(struct pollfd *fds,nfds_t nfds,int timeout)
struct pollfd
{
int fd;//文件描述符
short events;//注册的事件
short revents;//实际发生的事件,由内核填充
};
poll事件类型 POLLIN POLLOUT POLLERR POLLRDHUP等。
2)nfds参数指定被监听事件集合fds的大小。
typedef unsigned long nfds_t;
3)timeout参数指定poll的超时值,单位是毫秒。timeout为-1,poll调用将永远阻塞,直到某个事件发生;当timeout为0时,poll 调用将立即返回。
返回值含义与select相同。
poll的一个简单应用:
在/root/pro/fd1 /root/pro/fd2中分别有内容,
1234
5678
和
1122
3344
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <stropts.h>
- #include <sys/poll.h>
- #include <sys/stropts.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <poll.h>
- #define BUFSIZE 1024
- int main(int argc, char *argv[])
- {
- char buf[BUFSIZE];
- int bytes;
- struct pollfd *pollfd;
- int i=0;
- int nummonitor=0;
- int numready;
- int errno;
- char *str;
- if(argc != 3)
- {
- fprintf(stderr,"Usage:the argc num error\n");
- exit(1);
- }
- if((pollfd = (struct pollfd*)calloc(2, sizeof(struct pollfd))) == NULL) //为struct pollfd分配空间
- exit(1);
- for(i; i<2; i++) //初始化化struct pollfd结构
- {
- str = (char*)malloc(14*sizeof(char));
- memcpy(str,"/root/pro/",14);
- strcat(str,argv[i+1]);//注意,需要把路劲信息放到str中,否则opne("/root/pro/argv[i]",O_RDONLY)会出错
- printf("str=%s\n",str);//原因在于,在” “之中的argv[i]是字符串,不会用变量代替argv[i].
- (pollfd+i)->fd = open(str,O_RDONLY);
- if((pollfd+i)->fd >= 0)
- fprintf(stderr, "open (pollfd+%d)->fd:%s\n", i, argv[i+1]);
- nummonitor++;
- (pollfd+i)->events = POLLIN;//与下面的pollfd->revent对应
- }
- printf("nummonitor=%d\n",nummonitor);
-
- while(nummonitor > 0)
- {
- numready = poll(pollfd, 2, -1);
- if ((numready == -1) && (errno == EINTR))
- continue; //被信号中断,继续等待
- else if (numready == -1)
- break; //poll真正错误,推出
- printf("numready=%d\n",numready);
- for (i=0;nummonitor>0 && numready>0; i++)
- {
- if((pollfd+i)->revents & POLLIN)
- {
-
- bytes = read(pollfd[i].fd, buf, BUFSIZE);
- numready--;
- printf("pollfd[%d]->fd read buf:\n%s \n", i, buf);
- nummonitor--;
- }
- }
- }
- for(i=0; i<nummonitor; i++)
- close(pollfd[i].fd);
- free(pollfd);
- return 0;
- }
epoll是linux特有的I/O复用函数。它在实现上和使用上与select,poll有很大区别。首先epoll用一组函数来完成任务,而不是单个函数,其次epoll把用户关心的文件描述符上的事件放在内核里的一个事件表中。
epoll需要使用一个额外的文件描述符,来唯一标识内核中的这个事件表。这个文件描述符使用如下函数创建:
点击(此处)折叠或打开
- #include<sys/epoll.h>
- int epoll_create(int size);
下面的函数用来操作epoll的内核事件表
点击(此处)折叠或打开
- #include<sys/epoll.h>
- int epoll_ctl(int epfd,int op,int fd,struct epoll_event *event);
epoll_event定义如下:
struct epoll_event
{
__uint32_t events; /*epoll事件*/
epoll_data_t data; //用户数据
};
typedef union epoll_data
{
void *ptr;
int fd;//使用做多的
uint32_t u32;
uint64_t u64;
}epoll_data_t;
epoll_ctl成功时返回0,失败则返回-1并设置errno。
epoll系统调用的主要接口是epoll_wait函数,它在一段超时时间内等待一组文件描述符上的事件,其原型如下:
点击(此处)折叠或打开
- #icnldue<sys/epoll.h>
- int epoll_event(int epfd,struct epoll_event *events,int maxevents,int timeout);
maxevents参数指定最多监听多少个事件。
epoll_wait函数如果检测到事件,就将所有的就绪事件从内核事件表中复制到它的第二个参数events指向的数组中。这个数组只用于输出epoll检测到的就绪事件。
1
上一篇:elf文件格式与动态链接库(非常之好)-----不可不看
下一篇:嵌入汇编
相关热门文章
- linux 常见服务端口
- xmanager 2.0 for linux配置
- 【ROOTFS搭建】busybox的httpd...
- openwrt中luci学习笔记
- Linux里如何查找文件内容...
给主人留下些什么吧!~~
评论热议
阅读全文
0 0
- I/O模型以及I/O复用
- epoll I/O复用
- I/O复用
- I/O 复用
- Linux I/O复用
- I/O复用-epoll
- I/O复用
- I/O复用
- I/O复用
- I/O复用poll
- I/O复用epoll
- I/O复用--《APUE1》
- I/O复用
- I/O复用
- I/O复用
- I/O复用
- I/O复用------select
- I/O复用------poll
- wait_event_interruptible_out的返回值
- task _struct注释
- C语言的5种存储类以及关键字volatile、restrict
- LINUX多路复用select,poll,epoll
- elf文件格式与动态链接库(非常之好)-----不可不看
- I/O复用
- 嵌入汇编
- HDU 6064 RXD and numbers(生成树计数+行列式)
- http https
- 几种所谓的“高级IO接口”,其中你知道几个
- java 键盘输入多种方法
- select poll epoll的区别
- ”奇葩“的递归
- linux iostat命令
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
都梁
大崩溃 都梁
都灵
都灵队
都灵裹尸布
依维柯都灵
都灵之马
大都灵时代
都灵大学
都灵美院
意大利都灵
都灵理工大学
都灵足球俱乐部
依维柯都灵a32
意大利都灵美术学院
依维柯都灵6座价格
都灵理工大学世界排名
战士野蛮护甲4件都值得做吗
甲三项都检查什么
甲功都查什么
资深路人甲
路人甲
下甲
妻大都督
妻为大都督
大明都督
妻乃大都督
都督
妻是大都督
大都督
三国大都督
都督的意思
五军都督府
都督 甘中护
都督解说
都督怎么读
大唐从大都督开始到皇帝
大唐从大都督开始到
都督大人宠妻录
羽林都督作品
妻为大都督全文阅读