一、I/O复用概述
解决进程或线程阻塞到某个 I/O 系统调用而出现的技术,使进程不阻塞于某个特定的 I/O 系统调
I/O复用使用的场合:
1.当客户处理多个描述符(通常是交互式输入、网络套接字)时,必须使用I/O复用。
2.tcp服务器既要处理监听套接字,又要处理已连接套接字,一般要使用I/O复用。
3.如果一个服务器既要处理tcp又要处理udp,一般要使用I/O复用。
4.如果一个服务器要处理多个服务或多个服务时,一般要使用I/O复用。
I/O复用常用函数:
select、poll
二、select()函数
select函数介绍:
int select(int maxfd, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout);
功能:轮询扫描多个描述符中的任一描述符是否发生响应,或经过一段时间后唤醒
参数:
参数名称说明maxfd指定要检测的描述符的范围所检测描述符最大值+1
readset
可读描述符集
监测该集合中的任意描述符是否有数据可读
writeset
可写描述符集
监测该集合中的任意描述符是否有数据可写
exceptset
异常描述符集
监测该集合中的任意描述符是否发生异常
timeout
超时时间
超过规定时间后唤醒
返回值:
0:超时
-1:出错
>0:准备好的文件描述符数量
头文件:
- #include <sys/select.h>
- #include <sys/time.h>
超时时间:
-
- struct timeval{
- long tv_sec;
- long tv_usec;
- };
-
-
- struct timeval timeout;
- timeoout.tv_sec = 10;
- timeoout.tv_usec = 200000;
-
-
描述符集合的操作:
select()函数能对多个文件描述符进行监测,如果一个参数对应一个描述符,那么select函数的4个参数最多能监测4个文件描述,那他如何实现对多个文件描述符的监测的呢?
大家想一想文件描述符基本具有3种特性(读、写、异常),如果我们统一将监测可读的描述符放入可读集合(readset),监测可写的描述符放入可写集合(writeset),监测异常的描述符放入异常集合(exceptset)。然后将这3个集合传给select函数,是不是就可监测多个描述符呢.
如何将某个描述符加入到特定的集合中呢?这时就需要了解下面的集合操作函数
- /初始化描述符集
- void FD_ZERO(fd_set *fdset);
-
-
- void FD_SET(int fd, fd_set *fdset);
-
-
- void FD_CLR(int fd, fd_set *fdset);
-
-
- int FD_ISSET(int fd, fd_set *fdset);
select()函数整体使用框架:
例子:检测 0、4、5 描述符是否准备好读
- while(1)
- {
- fd_set rset;
- FD_ZERO(&rset);
- FD_SET(0, &rset);
- FD_SET(4, &rset);
- FD_SET(5, &rset);
-
- if(select(5+1, &rset, NULL, NULL, NULL) > 0)
- {
- if(FD_ISSET(0, &rset))
- {
-
- }
-
- if(FD_ISSET(4, &rset))
- {
-
- }
- if(FD_ISSET(5, &rset))
- {
-
- }
- }
- }
三、select函数的应用对比
我们通过udp同时收发的例子来说明select的妙处。
对于udp同时收发立马想到的是一个线程收、另一个线程发,下面的代码就是通过多线程来实现
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/select.h>
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <pthread.h>
-
-
- void *recv_thread(void* arg)
- {
- int udpfd = (int)arg;
- struct sockaddr_in addr;
- socklen_t addrlen = sizeof(addr);
-
- bzero(&addr,sizeof(addr));
- while(1)
- {
- char buf[200] = "";
- char ipbuf[16] = "";
- recvfrom(udpfd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &addrlen);
- printf("\r\033[31m[%s]:\033[32m%s\n",inet_ntop(AF_INET,&addr.sin_addr,ipbuf,sizeof(ipbuf)),buf);
- write(1,"UdpQQ:",6);
- }
- return NULL;
- }
-
- int main(int argc,char *argv[])
- {
- char buf[100] = "";
- int udpfd = 0;
- pthread_t tid;
- struct sockaddr_in addr;
- struct sockaddr_in cliaddr;
-
-
- bzero(&addr,sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(8000);
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
-
- bzero(&cliaddr,sizeof(cliaddr));
- cliaddr.sin_family = AF_INET;
- cliaddr.sin_port = htons(8000);
-
-
- if( (udpfd = socket(AF_INET,SOCK_DGRAM, 0)) < 0)
- {
- perror("socket error");
- exit(-1);
- }
-
-
- if(bind(udpfd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
- {
- perror("bind error");
- close(udpfd);
- exit(-1);
- }
-
- printf("input: \"sayto 192.168.221.X\" to sendmsg to somebody\n");
-
- pthread_create(&tid, NULL, recv_thread, (void*)udpfd);
- printf("\033[32m");
- fflush(stdout);
-
- while(1)
- {
-
- write(1,"UdpQQ:",6);
- fgets(buf, sizeof(buf), stdin);
- buf[strlen(buf) - 1] = '\0';
- if(strncmp(buf, "sayto", 5) == 0)
- {
- char ipbuf[INET_ADDRSTRLEN] = "";
- inet_pton(AF_INET, buf+6, &cliaddr.sin_addr);
- printf("\rconnect %s successful!\n",inet_ntop(AF_INET,&cliaddr.sin_addr,ipbuf,sizeof(ipbuf)));
- continue;
- }
- else if(strncmp(buf, "exit",4) == 0)
- {
- close(udpfd);
- exit(0);
- }
-
- sendto(udpfd, buf, strlen(buf),0,(struct sockaddr*)&cliaddr, sizeof(cliaddr));
- }
-
- return 0;
- }
运行结果:
用select来完成上述同样的功能:
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/select.h>
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
-
- int main(int argc,char *argv[])
- {
- int udpfd = 0;
- struct sockaddr_in saddr;
- struct sockaddr_in caddr;
-
- bzero(&saddr,sizeof(saddr));
- saddr.sin_family = AF_INET;
- saddr.sin_port = htons(8000);
- saddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
- bzero(&caddr,sizeof(caddr));
- caddr.sin_family = AF_INET;
- caddr.sin_port = htons(8000);
-
-
- if( (udpfd = socket(AF_INET,SOCK_DGRAM, 0)) < 0)
- {
- perror("socket error");
- exit(-1);
- }
-
-
- if(bind(udpfd, (struct sockaddr*)&saddr, sizeof(saddr)) != 0)
- {
- perror("bind error");
- close(udpfd);
- exit(-1);
- }
-
- printf("input: \"sayto 192.168.220.X\" to sendmsg to somebody\033[32m\n");
- while(1)
- {
- char buf[100]="";
- fd_set rset;
- FD_ZERO(&rset);
- FD_SET(0, &rset);
- FD_SET(udpfd, &rset);
- write(1,"UdpQQ:",6);
-
- if(select(udpfd + 1, &rset, NULL, NULL, NULL) > 0)
- {
- if(FD_ISSET(0, &rset))
- {
- fgets(buf, sizeof(buf), stdin);
- buf[strlen(buf) - 1] = '\0';
- if(strncmp(buf, "sayto", 5) == 0)
- {
- char ipbuf[16] = "";
- inet_pton(AF_INET, buf+6, &caddr.sin_addr);
- printf("\rsay to %s\n",inet_ntop(AF_INET,&caddr.sin_addr,ipbuf,sizeof(ipbuf)));
- continue;
- }
- else if(strcmp(buf, "exit")==0)
- {
- close(udpfd);
- exit(0);
- }
- sendto(udpfd, buf, strlen(buf),0,(struct sockaddr*)&caddr, sizeof(caddr));
- }
- if(FD_ISSET(udpfd, &rset))
- {
- struct sockaddr_in addr;
- char ipbuf[INET_ADDRSTRLEN] = "";
- socklen_t addrlen = sizeof(addr);
-
- bzero(&addr,sizeof(addr));
-
- recvfrom(udpfd, buf, 100, 0, (struct sockaddr*)&addr, &addrlen);
- printf("\r\033[31m[%s]:\033[32m%s\n",inet_ntop(AF_INET,&addr.sin_addr,ipbuf,sizeof(ipbuf)),buf);
- }
- }
- }
-
- return 0;
- }
运行结果:
0 0