版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第六章、I/O复用的服务器与客户端,I/O模型 select函数 IO复用客户端 IO复用服务器 select函数总结(poll与pselcet函数) 测试用例,1. I/O模型,I/O复用技术的引入 第3章,迭代服务器的低效率主要原因是串行执行 第4、5章,采用并行的思路来解决这个问题 从这一章开始,换一个思路 仔细分析迭代服务器模型 串行化还不是造成低效率主要原因 真正的原因是阻塞 程序阻塞在read、accept等系统调用上,不能为其它客户端服务,串行的迭代服务器,socket(),bind(),listen(),accept(),read(),write(),close(),socke
2、t(),connect(),write(),read(),close(),阻塞直到接收 到客户连接请求,TCP服务器端,TCP客户端,网络操纵函数最终被抽象成文件的IO操作,着重对慢系统调用观察,会发现这些函数基本上分成两个阶段: 等待事件发生 程序与系统内核交互,获取发生的事件 根据这两个阶段的不同操作,可以分为五种IO模型 阻塞I/O 非阻塞I/O I/O复用(select、epoll、kqueue) 信号驱动I/O 异步I/O,Blocking I/O Model,The most prevalent I/O model 。By default, all sockets are bloc
3、king,Process blocks in recvfrom,recvfrom,appliction,kernel,No data ready,System call,Process data,Copy complete,System call,Data ready Copy data,Wait for data,Copy data from kernel to user,Nonblocking I/O Model,When a socket is nonblocking, telling kernel: “ when an I/O operation that I request cann
4、ot be completed without putting the process th sleep, do not put the process to sleep”. It is called polling, waste of CPU time.,Process blocks in recvfrom,recvfrom,Appliction,Kernel,No data ready,System call,Process data,Copy complete,System call,Data ready Copy data,Wait for data,Copy data from ke
5、rnel to user,EWOULDBLOCK,recvfrom,No data ready,System call,EWOULDBLOCK,recvfrom,System call,recvfrom,recvfrom,No data ready,No data ready,No data ready,System call,No data ready,EWOULDBLOCK,System call,No data ready,recvfrom,System call,EWOULDBLOCK,No data ready,recvfrom,System call,EWOULDBLOCK,I/O
6、 multiplexing Model,When I/O multiplexing, we call select or poll and block in one of these two system call, instead of blocking in the actual I/O system call.,Process blocks while data copied into application buffer,select,application,kernel,No data ready,System call,Process data,Copy complete,Syst
7、em call,Data ready Copy data,Wait for data,Process blocks in select wait for one of possibly many sockets to become readable,Return readable,recvfrom,System call,Copy data from kernel to user,Notes:in fact there is a slight disadvantage because using select require two system calls. But the advantag
8、e in using select is that we can wait for more than one descriptor to be ready.,Signal Driven I/O Model,Telling the kernel to notify us with the SIGIO signal when the descriptor is ready. Advantage: process is not blocked while waiting for the datagram to arrive.,Asynchronous I/O Model,Asynchronous
9、I/O tell the kernel to start the operation and to notify us when the entire operation is complete.,Main difference with signal-driven I/O?,Comparison of the I/O Model,Wait for data,Copy data from kernel to user,五种IO模型回顾,同步与异步IO Posix定义这两个术语如下 同步I/O操作引起请求进程阻塞,知道I/O操作完成; 异步I/O操作不引起请求进程阻塞; IO的两个阶段都没有被阻
10、塞,才能被称为异步IO 因此,阻塞I/O模型、非阻塞I/O模型、 I/O复用模型和信号驱动模型都是同步I/O模型;最后一种才是异步IO 实际编程中,这几种IO模型经常根据需要混用 首先要避免被阻塞,但是阻塞都不好? 在事件的检查点要阻塞 避免采用信号机制,主要原因是信号是异步的,将使程序变得很难编写。即使写成了这样的代码,也会由于引入防止异步干扰的代码,抵消掉信号带来的优势。,2. I/O Multiplexing,What we need is the capability to tell the kernel that we want to be notified if one or mo
11、re I/O conditions are ready (i.e., input is ready to be read, or the descriptor is capable of taking more output). This capability is called I/O multiplexing and is provided by the select and poll functions.,Scenarios of I/O multiplexing,When a client is handling multiple descriptors (normally inter
12、active input and a network socket). If a TCP server handles both a listening socket and its connected sockets. If a server handles both TCP and UDP. If a server handlers multiple services and perhaps multiple protocols. etc.,Select函数,#include int select(int maxfdp1, fd_set *readfds, fd_set *writefds
13、, fd_set *exceptfds, struct timeval *timeout); RETURN:0, 发生的事件数目;-1, 出错;=0,轮询 readfds、writefds、exceptfds分别是读、写、错误的描述符集合。例如 readfds可能是0, 3 writefds可能是1, 3 timeout表示超时 struct timeval long tv_sec;/* seconds */ longtv_usec;/* microseconds */ ,返回值 当timeout指针为NULL时,如果没有事件发生,则select将一直阻塞下去; 当timeout指针为NULL
14、时,如果事件发生,则返回发生事件的个数; 当timeout指向的结构非0时,如果事件没有发生,则返回0; 如果timeout指向的结构为0,则select会立即返回,此时select用于轮询; 当出错、或者被信号打断时,select返回-1,具体的出错信息放在errno中,fd_set /usr/include/bits/typesizes.h: #define _FD_SETSIZE 1024 /usr/include/sys/select.h: typedef long int _fd_mask; #define _NFDBITS (8 * (int) sizeof (_fd_mask)
15、typedef struct _fd_mask fds_bits_FD_SETSIZE / _NFDBITS; fd_set; 为节省空间,fd_set采用bit位图的形式表示文件描述符,对fd_set的操纵宏 #define FD_SET(fd, fdsetp) _FD_SET (fd, fdsetp) #define FD_CLR(fd, fdsetp) _FD_CLR (fd, fdsetp) #define FD_ISSET(fd, fdsetp) _FD_ISSET (fd, fdsetp) #define FD_ZERO(fdsetp) _FD_ZERO (fdsetp) maxf
16、dp1=max(readfd, writefd, excepfd)+1,这里为什么要带入maxfdp1到select函数? 如果对writefd,exceptfd不感兴趣,可以以NULL带入select函数 需要注意: 第1次使用select函数之前,一定要用FD_ZERO清文件描述符集合 由于select函数返回前会修改这些描述符集合,所以再次调用select函数时,要用FD_SET重新填写这些描述符集合,select函数的能够检测的事件,读事件 The number of bytes of data in the socket receive buffer is greater than
17、or equal to the current size of the low-water mark. The read-half of the connection is closed. The socket is a listening socket and the number of completed connections is nonzero. A socket error is pending.,写事件 The number of bytes of available space in the socket send buffer is greater than or equal
18、 to the current size of the low-water mark; 非阻塞connect连接成功 The write-half of the connection is closed. A write operation one the socket will generate SIGPIPE. A socket error is pending. 异常事件 带外数据需要处理,Linux的扩展,自linux 2.6.28开始,以下事件可以通过select来检测 signalfd,建立一个信号的文件描述符,检测信号是否发生 timerfd,建立定时器的文件描述符,检测定时器
19、eventfd,建立事件的文件描述符,用于进程间的事件通知,以取代pipe inotifyfd,建立对文件inode节点的检测描述符,观察文件的状态变化,select函数的例子 fd_set rfds; struct timeval tv; FD_ZERO(,3. I/O复用客户端,IO复用编程是一种基于事件的编程,程序在select函数上阻塞等待事件的发生 通过对比读、写、异常类别的文件描述符,判断事件是否发生 进行相关处理 如果事件不再需要,则从事件集合中删除,客户端需要哪些事件? connect? 读stdin 写socket 读socket 写stdout 退出时机 读stdin,长度
20、为0,迭代式客户端代码 . socket connect read(stdin) write(socket) read(socket) write(stdout) .,write函数的含义 将应用程序用户空间的数据拷贝到内核socket的buffer内,然后返回 write并不理会数据是否发送完毕 socket的buffer数目一般是固定的 如果socket的buffer并不足以容纳用户数据,该怎么办?通常是阻塞到socket的buffer有足够的空间为止 Linux的man有问题?这里采纳教材中的说法,linux:man 2 write The number of bytes written
21、 may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).) FreeBSD: When u
22、sing non-blocking I/O on objects such as sockets that are subject to flow control, write() and writev() may write fewer bytes than requested; the return value must be noted, and the remainder of the oper- ation should be retried when possible.,因此,阻塞的write有流控的功能,这正是我们所需要的。 如果write(socket)阻塞,说明网络线路可能出
23、现问题。此时,如果再继续从stdin接受数据,这些数据该放在何处? write(stdout)也是同样的道理,stdout可能被重定向到一个文件,如果磁盘IO的速度难以跟上网络传输的速度,此时流控正是我们所需要的。,IO复用客户端代码,gettimeofday( ,else if (io.isReadEventSet(sock.getFd() / checking socket if (length = sock.read(rbuf, BUFFER_SIZE) = 0) io.clearReadEvent(sock.getFd(); break; length = sout.write(rbu
24、f, length); else/ error throw EXCEPTION(); gettimeofday(,4. I/O复用服务器,服务器代码中,等待事件的程序结构与客户端相同。 事件有哪些? accept read write,迭代式服务器代码 . socket bind listen accept read write .,accept事件的处理 accept返回了一个新的套接字,这个套接字代表与客户端的一个新进连接 服务器应该在这个新socket上继续侦听事件 因此,accept的动作包括向read集中添加新客户端读事件 还有一个问题,客户端只需观测两个描述符,而服务器需要观测未知个文件描述符,如何管理这些新进连接?这里采用std:set来管理accept的文件描述符,IO复用服务器流程,IO复用服务器代码,while (true) io.waitEvent(); / / listen socket / if (io.isReadEventSet(lsock.getFd() lsock.accept(asock); io.addReadEvent(a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年7月重庆市南岸区人力资源和社会保障局公益性岗位招聘3人考试备考试题及答案详解
- 2026年喀什地区喀什市住房和城乡建设局人员招聘笔试备考题库及答案详解
- 2026年衡阳市蒸湘区住房和城乡建设局人员招聘笔试参考题库及答案详解
- 人工智能与保险欺诈检测-第8篇
- 2026年十堰市茅箭区住房和城乡建设局人员招聘笔试参考题库及答案详解
- 2026年福建省三明市住房和城乡建设局人员招聘笔试备考试题及答案详解
- 2026榆林佳县纪委监委选调事业编岗位(5人)考试模拟试题及答案详解
- 2026山西兰花煤炭实业集团有限公司基层一线员工招聘787人考试模拟试题及答案详解
- 2026年武汉市乔口区住房和城乡建设局人员招聘笔试参考题库及答案详解
- 2026年金华永康市中医院招聘协议人员6人考试备考题库及答案详解
- DB11-T 2556-2026 城市轨道交通既有线改造技术要求
- 《渔光互补发电项目施工期水土保持方案》
- 混凝土罐车安全培训
- 2026年湖北省工程专业技术职务水平能力测试(规划)综合能力测试题及答案
- 2026-2030中国人力资源服务行业全景调研与发展战略研究咨询报告
- 九年级上册物理期中必考-根据电路图连接实物图练习题(含答案)
- 2026中国电力建设秋招面试题及答案
- 焦虑障碍患者的家属辅导
- 白酒培训销售新人课件
- 2025年新凯来硬件笔试题及答案
- TYH1019-2020立方星内部载荷结构设计要求
评论
0/150
提交评论