【socket编程】select manual page翻译

原文: select manual page

依赖的头文件

/* According to POSIX.1-2001, POSIX.1-2008 */
#include <sys/select.h>

/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
   

方法定义

int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);
void FD_CLR(int fd, fd_set *set);
int  FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_ZERO(fd_set *set);

int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask);

特征

select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform a corresponding I/O operation (e.g., read(2) without blocking, or a sufficiently small write(2)).

select和pselect方法可以监听多个fd,直到其中的一个或多个准备好可以进行读取或写入操作。

select() can monitor only file descriptors numbers that are less than FD_SETSIZE; poll(2) does not have this limitation.

select只可以同时监听最多FD_SETSIZE个文件描述法,而poll则没有最大限制。

The operation of select() and pselect() is identical, other than these three differences:

(i) select() uses a timeout that is a struct timeval (with seconds and microseconds), while pselect() uses a struct timespec (with seconds and nanoseconds).

(ii) select() may update the timeout argument to indicate how much time was left. pselect() does not change this argument.

(iii) select() has no sigmask argument, and behaves as pselect called with NULL sigmask.

select和pselect的区别主要有三点:

1)select使用了timeval类型的timeout(单位为秒和毫秒),pselect使用的是timespec(单位是秒和纳秒)

2)select可以更新timeout的值而pselect不可以

3)select没有sigmask参数,与pselect使用值为NULL的sigmask的参数时效果相同

参数解释

Three independent sets of file descriptors are watched. The file

descriptors listed in readfds will be watched to see if characters

become available for reading (more precisely, to see if a read will

not block; in particular, a file descriptor is also ready on end-of-

file). The file descriptors in writefds will be watched to see if

space is available for write (though a large write may still block).

The file descriptors in exceptfds will be watched for exceptional

conditions.

三个独立的文件描述符集合被监听,其中readfds中的fd被监听是否可读,writefds被监听是否可写,exceptfds被监听是否有异常状态。

On exit, each of the file descriptor sets is modified in place to

indicate which file descriptors actually changed status. (Thus, if

using select() within a loop, the sets must be reinitialized before

each call.)

当函数退出时,每个fd集合都可能被修改,因此当在循环中使用select函数时,要记得每次重新初始化fd 集合。

Four macros are provided to manipulate the sets. FD_ZERO() clears a

set. FD_SET() and FD_CLR() respectively add and remove a given file

descriptor from a set. FD_ISSET() tests to see if a file descriptor

is part of the set; this is useful after select() returns.

四个宏命令可以用来操作这些fd集合:

FD_ZERO()用来清空fd集合;

FD_SET()FD_CLR()分别用来向fd集合添加和删除一个fd;

FD_ISSET()用来查看一个fd是否属于某一个fd集合,该方法通常在select返回后使用。

nfds should be set to the highest-numbered file descriptor in any of

the three sets, plus 1. The indicated file descriptors in each set

are checked, up to this limit

nfds参数应该被设置成三个fd集合的数量最大值加一,这样每个fd集合中的fd都可以被check一次。我理解是FD_ISSET宏会根据这个值去遍历每个fd集合。

The timeout argument specifies the interval that select() should

block waiting for a file descriptor to become ready. The call will

block until either:

  • a file descriptor becomes ready;
  • the call is interrupted by a signal handler; or
  • the timeout expires.

If both fields of the timeval structure are zero, then select()

returns immediately. (This is useful for polling.) If timeout

is NULL (no timeout), select() can block indefinitely.

timeout参数用来设置select阻塞的时间,直到其中一个fd可以进行读写,或者中断,或者超时。如果timeout设为0则select会立刻返回(轮询时很有用),设为NULL则select函数会一直阻塞下去。

sigmask is a pointer to a signal mask (see sigprocmask(2)); if it is

not NULL, then pselect() first replaces the current signal mask by

the one pointed to by sigmask, then does the "select" function, and

then restores the original signal mask.

错误类型

EBADF An invalid file descriptor was given in one of the sets.

(Perhaps a file descriptor that was already closed, or one on

which an error has occurred.) However, see BUGS.

EINTR A signal was caught; see signal(7).

EINVAL nfds is negative or exceeds the RLIMIT_NOFILE resource limit

(see getrlimit(2)).

EINVAL The value contained within timeout is invalid.

ENOMEM Unable to allocate memory for internal tables.

返回值

On success, select() and pselect() return the number of file

descriptors contained in the three returned descriptor sets (that is,

the total number of bits that are set in readfds, writefds,

exceptfds) which may be zero if the timeout expires before anything

interesting happens. On error, -1 is returned, and errno is set to

indicate the error; the file descriptor sets are unmodified, and

timeout becomes undefined.

select执行成功时,会返回三个fd集合的fd总数量。超时时返回值为0。发生错误时返回-1,可以通过errno的值查看报错信息。

代码示例

   #include <stdio.h>
   #include <stdlib.h>
   #include <sys/time.h>
   #include <sys/types.h>
   #include <unistd.h>

   int
   main(void)
   {
       fd_set rfds;
       struct timeval tv;
       int retval;

       /* Watch stdin (fd 0) to see when it has input. */

       FD_ZERO(&rfds);
       FD_SET(0, &rfds);

       /* Wait up to five seconds. */

       tv.tv_sec = 5;
       tv.tv_usec = 0;

       retval = select(1, &rfds, NULL, NULL, &tv);
       /* Don‘t rely on the value of tv now! */

       if (retval == -1)
           perror("select()");
       else if (retval)
           printf("Data is available now.\n");
           /* FD_ISSET(0, &rfds) will be true. */
       else
           printf("No data within five seconds.\n");

       exit(EXIT_SUCCESS);
   }

原文地址:https://www.cnblogs.com/puyangsky/p/8424505.html

时间: 2024-08-25 18:21:00

【socket编程】select manual page翻译的相关文章

windows socket编程select模型使用

int select( int nfds,            //忽略 fd_ser* readfds,    //指向一个套接字集合,用来检测其可读性 fd_set* writefds,   // 指向一个套接字结合,用来检测其可写性 fd_ser* exceptfds, //指向一个套接字集合,用来检测错误 const struct timeval * timeout   //指定此函数等待的最长时间,如果为NULL,则最长时间为无限大. ); 参数说明: (1)   nfds  win

Socket编程实践(10) --select的限制与poll的使用

select的限制 用select实现的并发服务器,能达到的并发数一般受两方面限制: 1)一个进程能打开的最大文件描述符限制.这可以通过调整内核参数.可以通过ulimit -n(number)来调整或者使用setrlimit函数设置,但一个系统所能打开的最大数也是有限的,跟内存大小有关,可以通过cat /proc/sys/fs/file-max 查看 /**示例: getrlimit/setrlimit获取/设置进程打开文件数目**/ int main() { struct rlimit rl;

[转] 3个学习Socket编程的简单例子:TCP Server/Client, Select

以前都是采用ACE的编写网络应用,最近由于工作需要,需要直接只用socket接口编写CS的代码,重新学习这方面的知识,给出自己所用到的3个简单例子,都是拷贝别人的程序.如果你能完全理解这3个例子,估计socket编程就已经基本入门了. 建议:1) 多多查查所用到的网络接口; 2) 最好有一本书,如UNIX环境高级编程,UNIX网络编程,可查询:3) 可以直接使用书上的例子更好. http://blog.csdn.net/zhenjing/article/details/4770490 TCP C

socket编程的select模型

在掌握了socket相关的一些函数后,套接字编程还是比较简单的,日常工作中碰到很多的问题就是客户端/服务器模型中,如何让服务端在同一时间高效的处理多个客户端的连接,我们的处理办法可能会是在服务端不停的监听客户端的请求,有新的请求到达时,开辟一个新的线程去和该客户端进行后续处理,但是这样针对每一个客户端都需要去开辟一个新的线程,效率必定底下. 其实,socket编程提供了很多的模型来处理这种情形,我们只要按照模型去实现我们的代码就可以解决这个问题.主要有select模型和重叠I/o模型,以及完成端

socket编程以及select、epoll、poll示例详解

socket编程socket这个词可以表示很多概念,在TCP/IP协议中“IP地址 + TCP或UDP端口号”唯一标识网络通讯中的一个进程,“IP + 端口号”就称为socket.在TCP协议中,建立连接的两个进程各自有一个socket来标识,那么两个socket组成的socket pair就唯一标识一个连接. 预备知识 网络字节序:内存中多字节数据相对于内存地址有大端小端之分,磁盘文件中的多字节数据相对于文件中的偏移地址也有大端小端之分.网络数据流同样有大端小端之分,所以发送主机通常将发送缓冲

c++中Socket编程(入门)

在学习Socket编程时,在其他地方看到了不错Socket入门文档,通俗易懂: 介绍 Socket编程让你沮丧吗?从man pages中很难得到有用的信息吗?你想跟上时代去编Internet相关的程序,但是为你在调用 connect() 前的bind() 的结构而不知所措?等等- 好在我已经将这些事完成了,我将和所有人共享我的知识了.如果你了解 C 语言并想穿过网络编程的沼泽,那么你来对地方了. 读者对象 这个文档是一个指南,而不是参考书.如果你刚开始 socket 编程并想找一本入门书,那么你

PHP SOCKET编程

1. 预备知识 一直以来很少看到有多少人使用PHP的socket模块来做一些事情,大概大家都把它定位在脚本语言的范畴内吧,但是其实php的socket模块可以做很多事情,包括做ftplist,http post提交,smtp提交,组包并进行特殊报文的交互(如smpp协议),whois查询.这些都是比较常见的查询. 特别是php的socket扩展库可以做的事情简直不会比c差多少.php的socket连接函数1.集成于内核的socket这个系列的函数仅仅只能做主动连接无法实现端口监听相关的功能.而且

C语言SOCKET编程指南

1.介绍 Socket 编程让你沮丧吗?从man pages中很难得到有用的信息吗?你想跟上时代去编Internet相关的程序,但是为你在调用 connect() 前的bind() 的结构而不知所措?等等… 好在我已经将这些事完成了,我将和所有人共享我的知识了.如果你了解 C语言并想穿过网络编程的沼泽,那么你来对地方了. 2.读者对象 这个文档是一个指南,而不是参考书.如果你刚开始 socket 编程并想找一本入门书,那么你是我的读者.但这不是一本完全的 socket 编程书. 3.平台和编译器

[转]C++ Socket编程

C++ Socket 编程 介绍 Socket编程让你沮丧吗?从man pages中很难得到有用的信息吗?你想跟上时代去编Internet相关的程序,但是为你在调用 connect() 前的bind() 的结构而不知所措?等等… 好在我已经将这些事完成了,我将和所有人共享我的知识了.如果你了解 C 语言并想穿过网络编程的沼泽,那么你来对地方了. 读者对象 这个文档是一个指南,而不是参考书.如果你刚开始 socket 编程并想找一本入门书,那么你是我的读者.但这不是一本完全的 socket 编程书