i/o 模型

comcomxg 2002-04-29 05:23:42
请具体解释一下select模型,有点看不懂!
...全文
57 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kkk16 2002-04-29
  • 打赏
  • 举报
回复

现在好象很少用select模型了。
xuying 2002-04-29
  • 打赏
  • 举报
回复
select就是你先用FD_SET选出一些socket端口告诉系统,我要监视这些端口的活动,比如有数据来或有数据要发送。然后你就用死循环可以不停的测试这些端口是否有满足要求的(select)。如果有,你就可以用FD_ISSET()挑出那个需要处理的端口,这些端口进行处理(send或recv)。好处在于你可以同时监视多个端口,而且不停的测试的时候,系统可以处理其他的事情,而不阻塞。

tianlinyi 2002-04-29
  • 打赏
  • 举报
回复
完成端口实质上也是用的windows Overlapp内核技术,只是在线程管理和OverlappIO结果通知上有不同的方式而已。推荐completion port方式,编程还是比较简单的。
tianlinyi 2002-04-29
  • 打赏
  • 举报
回复
select模型需要一个系统缓冲,每次需要复制缓冲到你的缓冲区里,分配内存和复制的操作肯定浪费时间。
<br />

而且overlapped i/o好像是可以多个线程同时进行读写操作的。也就是说在上一个操作未完成的时候就可以再次操作,就是所谓“重叠”。select不可以,只能干完一件再干一件。在汇编一级上,我觉得overlapped i/o可以省许多call指令。
tianlinyi 2002-04-29
  • 打赏
  • 举报
回复
放弃了select,那是为了兼容berkely socket才有的方法。在windows下不适合使用。
以微软排外的风格,select的背后不一定使用什么低效率的方法去实现呢。微软告诉我们它的模型里overlapped I/O比其他模型都要,那就没错了。
原因是overlapped I/O可以不使用系统缓冲区,减少了copy操作。
而且可以使用多个缓冲区,减少了分配内存的操作,可以应用到“栈式缓冲区

具体还是看MSDN好。以前也认为overlapped I/O是有数量限制的,但后来仔细看了completeRouting,发现可以没有限制的,还有完成端口模型,更是海量了。

xgwlg 2002-04-29
  • 打赏
  • 举报
回复
The select function is used to determine the status of one or more sockets. For each socket, the caller can request information on read, write, or error status. The set of sockets for which a given status is requested is indicated by an fd_set structure. The sockets contained within the fd_set structures must be associated with a single service provider. For the purpose of this restriction, sockets are considered to be from the same service provider if the WSAPROTOCOL_INFO structures describing their protocols have the same providerId value. Upon return, the structures are updated to reflect the subset of these sockets that meet the specified condition. The select function returns the number of sockets meeting the conditions. A set of macros is provided for manipulating an fd_set structure. These macros are compatible with those used in the Berkeley software, but the underlying representation is completely different.

The parameter readfds identifies the sockets that are to be checked for readability. If the socket is currently in the listen state, it will be marked as readable if an incoming connection request has been received such that an accept is guaranteed to complete without blocking. For other sockets, readability means that queued data is available for reading such that a call to recv, WSARecv, WSARecvFrom, or recvfrom is guaranteed not to block.

For connection-oriented sockets, readability can also indicate that a request to close the socket has been received from the peer. If the virtual circuit was closed gracefully, and all data was received, then a recv will return immediately with zero bytes read. If the virtual circuit was reset, then a recv will complete immediately with an error code such as WSAECONNRESET. The presence of OOB data will be checked if the socket option SO_OOBINLINE has been enabled (see setsockopt).

The parameter writefds identifies the sockets that are to be checked for writability. If a socket is processing a connect call (nonblocking), a socket is writeable if the connection establishment successfully completes. If the socket is not processing a connect call, writability means a send, sendto, or WSASendto are guaranteed to succeed. However, they can block on a blocking socket if the len parameter exceeds the amount of outgoing system buffer space available. It is not specified how long these guarantees can be assumed to be valid, particularly in a multithreaded environment.

The parameter exceptfds identifies the sockets that are to be checked for the presence of OOB data (see section DECnet Out-of-band data for a discussion of this topic) or any exceptional error conditions.

Important OOB data will only be reported in this way if the option SO_OOBINLINE is FALSE. If a socket is processing a connect call (nonblocking), failure of the connect attempt is indicated in exceptfds (application must then call getsockopt SO_ERROR to determine the error value to describe why the failure occurred). This document does not define which other errors will be included.

Any two of the parameters, readfds, writefds, or exceptfds, can be given as NULL. At least one must be non-NULL, and any non-NULL descriptor set must contain at least one handle to a socket.

Summary: A socket will be identified in a particular set when select returns if:

readfds:

If listen has been called and a connection is pending, accept will succeed.
Data is available for reading (includes OOB data if SO_OOBINLINE is enabled).
Connection has been closed/reset/terminated.
writefds:

If processing a connect call (nonblocking), connection has succeeded.
Data can be sent.
exceptfds:

If processing a connect call (nonblocking), connection attempt failed.
OOB data is available for reading (only if SO_OOBINLINE is disabled).
Four macros are defined in the header file Winsock2.h for manipulating and checking the descriptor sets. The variable FD_SETSIZE determines the maximum number of descriptors in a set. (The default value of FD_SETSIZE is 64, which can be modified by defining FD_SETSIZE to another value before including Winsock2.h.) Internally, socket handles in an fd_set structure are not represented as bit flags as in Berkeley Unix. Their data representation is opaque. Use of these macros will maintain software portability between different socket environments. The macros to manipulate and check fd_set contents are:

FD_CLR(s, *set)
Removes the descriptor s from set.
FD_ISSET(s, *set)
Nonzero if s is a member of the set. Otherwise, zero.
FD_SET(s, *set)
Adds descriptor s to set.
FD_ZERO(*set)
Initializes the set to the NULL set.
The parameter time-out controls how long the select can take to complete. If time-out is a NULL pointer, select will block indefinitely until at least one descriptor meets the specified criteria. Otherwise, time-out points to a TIMEVAL structure that specifies the maximum time that select should wait before returning. When select returns, the contents of the TIMEVAL structure are not altered. If TIMEVAL is initialized to {0, 0}, select will return immediately; this is used to poll the state of the selected sockets. If select returns immediately, then the select call is considered nonblocking and the standard assumptions for nonblocking calls apply. For example, the blocking hook will not be called, and Windows Sockets will not yield.

Note The select function has no effect on the persistence of socket events registered with WSAAsyncSelect or WSAEventSelect.

Requirements
Version: Requires Windows Sockets 2.0.
Header: Declared in Winsock2.h.
Library: Use Ws2_32.lib.

See Also
Windows Sockets Programming Considerations Overview, Socket Functions, accept, connect, recv, recvfrom, send, WSAAsyncSelect, WSAEventSelect, TIMEVAL

leecyi 2002-04-29
  • 打赏
  • 举报
回复
能详细一点?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧