select()的机制中提供一fd_set的数据结构,实际上是一long类型的数组, 每一个数组元素都能与一打开的文件句柄(不管是Socket句柄,还是其他 文件或命名管道或设备句柄)建立联系,建立联系的工作由程序员完成, 当调用select()时,由内核根据IO状态修改fd_set的内容,由此来通知执 行了select()的进程哪一Socket或文件可读,下面具体解释:#include int select(nfds, readfds, writefds, exceptfds, timeout)
int nfds;fd_set *readfds, *writefds, *exceptfds;struct timeval *timeout;ndfs:select监视的文件句柄数,视进程中打开的文件数而定,一般设为呢要监视各文件 中的最大文件号加一。
readfds:select监视的可读文件句柄集合。
writefds: select监视的可写文件句柄集合。
exceptfds:select监视的异常文件句柄集合。
timeout:本次select()的超时结束时间。(见/usr/sys/select.h, 可精确至百万分之一秒!)
当readfds或writefds中映象的文件可读或可写或超时,本次select() 就结束返回。程序员利用一组系统提供的宏在select()结束时便可判 断哪一文件可读或可写。对Socket编程特别有用的就是readfds. 几只相关的宏解释如下:FD_ZERO(fd_set *fdset):清空fdset与所有文件句柄的联系。
FD_SET(int fd, fd_set *fdset):建立文件句柄fd与fdset的联系。
FD_CLR(int fd, fd_set *fdset):清除文件句柄fd与fdset的联系。
FD_ISSET(int fd, fdset *fdset):检查fdset联系的文件句柄fd是否可读写,>0表示可读写。
(关于fd_set及相关宏的定义见/usr/include/sys/types.h)
这样,你的socket只需在有东东读的时候才读入,大致如下:……
int sockfd;fd_set fdR;struct timeval timeout = ……;……
for(;;) { FD_ZERO(&fdR);FD_SET(sockfd, &fdR);switch (select(sockfd + 1, &fdR, NULL, &timeout)) { case -1:error handled by u;case 0:timeout hanled by u;default:if (FD_ISSET(sockfd)) { now u read or recv something;/* if sockfd is father and server socket, u
can now accept() */ }所以一个FD_ISSET(sockfd)就相当通知了sockfd可读。
至于struct timeval在此的功能,请man select.不同的timeval设置 使使select()表现出超时结束、无超时阻塞和轮询三种特性。由于timeval可精确至百万分之一秒,所以Windows的SetTimer()根本不算 什么。你可以用select()做一个超级时钟。
[lingyun@localhost select]$ cat select.c
/********************************************************************************** Copyright: (C) 2013 fulinux<fulinux@sina.com>
* All rights reserved.
*
* Filename: select.c
* Description: This file
*
* Version: 1.0.0(07/31/2013~)
* Author: fulinux <fulinux@sina.com>
* ChangeLog: 1, Release initial version on "07/31/2013 02:18:45 PM"
*
********************************************************************************/
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int fds[2];
char buf[7];
int i,rc,maxfd;
fd_set inset1,inset2;
struct timeval tv;
if((fds[0] = open("hello1",O_RDWR|O_CREAT,0666)) < 0)
perror("open hello1");
if((fds[1] = open("hello2",O_RDWR|O_CREAT,0666)) < 0)
perror("open hello2");
if((rc = write(fds[0],"Hello!\n",7)))
printf("rc = %d\n",rc);
lseek(fds[0],0,SEEK_SET);
maxfd = fds[0] > fds[1] ? fds[0] : fds[1];
FD_ZERO(&inset1);
FD_SET(fds[0],&inset1);
FD_ZERO(&inset2);
FD_SET(fds[1],&inset2);
tv.tv_sec = 2;
tv.tv_usec = 0;
while(FD_ISSET(fds[0],&inset1)||FD_ISSET(fds[1],&inset2))
{
if(select(maxfd+1,&inset1,&inset2,NULL,&tv) < 0)
perror("select");
else
{
if(FD_ISSET(fds[0],&inset1))
{
rc = read(fds[0],buf,7);
if(rc > 0)
{
buf[rc] = '\0';
printf("read: %s\n", buf);
}
else
perror("read");
}
}
if(FD_ISSET(fds[1],&inset2))
{
rc = write(fds[1],buf,7);
if(rc > 0)
{
buf[rc] = '\0';
printf("rc = %d, write: %s\n", rc,buf);
}
else
perror("write");
sleep(3);
}
}
exit(0);
}
[lingyun@localhost select]$
[lingyun@localhost select]$ ls
select.c
[lingyun@localhost select]$ gcc select.c
[lingyun@localhost select]$ ./a.out
rc = 7
read: Hello!
rc = 7, write: Hello!
read: Success
rc = 7, write: Hello!
read: Success
rc = 7, write: Hello!
read: Success
rc = 7, write: Hello!
read: Success
rc = 7, write: Hello!
^C
[lingyun@localhost select]$ cat hello1
Hello!
[lingyun@localhost select]$ cat hello2
Hello!
Hello!
Hello!
Hello!
Hello!
[lingyun@localhost select]$