search
HomeOperation and MaintenanceSafetyWhat does select mean in I/O multiplexing?

select是用于监视多个文件描述符状态的变化的。即用来监视文件描述符读/写/异常状态是否就绪。

函数原型:int select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout);

select的几大缺点:

(1)每次调用select,都需要把fd集合从用户态拷贝到内核态,这个开销在fd很多时会很大

(2)同时每次调用select都需要在内核遍历传递进来的所有fd,这个开销在fd很多时也很大

(3)select支持的文件描述符数量太小了

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<unistd.h>
  5 #include<sys/select.h>
  6 
  7 int main()
  8 {
  9     int std_in = 0;
 10 //  int std_out = 1;
 11     fd_set reads;
 12 //  fd_set writes;
 13     //int max_nums = std_out;
 14     int max_nums = std_in;
 15     FD_ZERO(&reads);
 16 //  FD_ZERO(&writes);
 17     FD_SET(std_in,&reads);
 18 //  FD_SET(std_out,&writes);
 19     struct timeval _timeout = {5,0};
 20     int done = 0;
 21     while(!done)
 22     {
 23         _timeout.tv_sec = 5;
  24         _timeout.tv_usec = 0;
 25         //switch(select(max_nums+1,&reads,&writes,NULL,&_timeout))
 26         switch(select(max_nums+1,&reads,NULL,NULL,&_timeout))
 27         {
 28             case -1:
 29                 perror("select");
 30                 break;
 31             case 0:
 32                 printf("timeout...\n");
 33                 break;
 34             default://success
 35                 {
 36                     if(FD_ISSET(std_in,&reads))
 37                     {//read
 38                         char buf[1024];
 39                         memset(buf,&#39;\0&#39;,sizeof(buf));
 40                         ssize_t size = read(std_in,buf,sizeof(buf)-1);
 41                         if(size<0)
 42                         {
 43                             perror("read");
 44                             exit(1);
 45                         }
 46                         if(strncmp(buf,"quit",4)==0)
  47                         {
 48                             done =1;
 49                             continue;
 50                         }
 51                         printf("echo: %s",buf);
 52 
 53                     }
 54                 //  if(FD_ISSET(std_out,&writes))
 55                 //  {//writes
 56                 //      char buf[1024];
 57                 //      while(1)
 58                 //      {
 59                 //          memset(buf,&#39;\0&#39;,sizeof(buf));
 60                 //          strcpy(buf,"hello world");
 61                             //write(1,buf,sizeof(buf)-1);
 62                 //          printf("%s\n",buf);
 63                 //      }
 64                 //  }
 65                     break;
 66                 }
 67         }
 68     }
 69     return 0;
 70 }
 [fbl@localhost select]$ ./select 
hello
echo: hello
hi
echo: hi
nihao
echo: nihao
ahhau
echo: ahhau
quit
[fbl@localhost select]$
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<unistd.h>
  5 #include<sys/socket.h>
  6 #include<sys/select.h>
  7 #include<sys/types.h>
  8 #include<netinet/in.h>
  9 #include<arpa/inet.h>
 10 #include<assert.h>
 11 
 12 #define _BACKLOG_ 5
 13 int fd[64];
 14 void usage(char *_port)
 15 {
 16     printf("%s,[ip],[port]\n",_port);
 17 }
 18 int startup(char *ip,int port)
 19 {
 20     assert(ip);
 21     int sock = socket(AF_INET,SOCK_STREAM,0);
 22     if(sock<0)
 23     {
 24         perror("socket");
 25         exit(1);
 26     }
 27     struct sockaddr_in local;
 28     local.sin_family = AF_INET;
 29     local.sin_port = htons(port);
 30     local.sin_addr.s_addr = inet_addr(ip);
 31     if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
 32     {
 33         perror("bind");
 34         exit(2);
 35     }
 36     if(listen(sock,_BACKLOG_)<0)
 37     {
 38         perror("listen");
 39         exit(3);
 40     }
 41     return sock;
 42 
 43 
 44 }
 45 int main(int argc,char *argv[])
 46 {
 47     if(argc!=3)
 48     {
 49         usage(argv[0]);
 50         return 1;
 51     }
 52     int port = atoi(argv[2]);
 53     char *ip = argv[1];
 54     int listen_sock = startup(ip,port);
 55     int new_sock = -1;
 56     struct sockaddr_in client;
 57     socklen_t len = sizeof(client);
 58     fd_set reads;
 59     fd_set writes;
 60     int max_nums;
 61     struct timeval _timeout = {5,0};
 62     int done = 0;
 63     int i =0;
 64     int fd_nums = sizeof(fd)/sizeof(fd[0]);
 65     for(;i<fd_nums;++i)
 66     {
 67         fd[i]=-1;
 68     }
 69     fd[0] = listen_sock;
  70     while(!done)
 71     {
 72         _timeout.tv_sec = 5;
 73         _timeout.tv_usec = 0;
 74         FD_ZERO(&reads);
 75         FD_ZERO(&writes);
 76         for(i=0;i<fd_nums;++i)
 77         {
 78             if(fd[i]>0)
 79             {
 80                 FD_SET(fd[i],&reads);
 81                 if(fd[i]>max_nums)
 82                 {
 83                     max_nums = fd[i];
 84                 }
 85 
 86             }
 87         }
 88         switch(select(max_nums+1,&reads,&writes,NULL,&_timeout))
 89         {
 90             case -1:
 91                 perror("select");
 92                 break;
 93             case 0:
 94                 printf("timeout...\n");
 95                 break;
 96             default:
 97                 {
 98 
 99                     for(i=0;i<fd_nums;++i)
100                     {
101                         if(fd[i]==listen_sock && FD_ISSET(fd[i],&reads))
102                         {
103                             new_sock = accept(listen_sock,(struct sockaddr*)    &client,&len);
104                             if(new_sock<0)
105                             {
106                                 perror("accept");
107                                 continue;
108                             }
109                             for(i=0;i<fd_nums;++i)
110                             {
111                                 if(fd[i]==-1)
112                                 {
113                                     fd[i]=new_sock;
114                                 }
115                             }
116 
117                         }
118                         else if(fd[i]>0 && FD_ISSET(fd[i],&reads))
119                         {
120                             char buf[1024];
121                             memset(buf,&#39;\0&#39;,sizeof(buf));
122                             ssize_t size = read(new_sock,buf,sizeof(buf)-1);
123                             if(size<0)
124                             {
125                                 perror("read");
126                                 exit(4);
127                             }
128                             else if(size==0)
129                             {
130                                 printf("client close...\n");
131                                 close(fd[i]);
132                                 fd[i]=-1;
133                             }
134                             else
135                             {
136                                 buf[size]=&#39;\0&#39;;
137                             }
138                             printf("client:%s\n",buf);
139                         }
140                         else
141                         {}
142                     }
143 
144                     break;
145                 }
146 
147         }
148     }
149     return 0;
150 }

1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<unistd.h>
  5 #include<sys/socket.h>
  6 #include<sys/select.h>
  7 #include<sys/types.h>
  8 #include<netinet/in.h>
  9 #include<arpa/inet.h>
 10 void usage(char *_port)
 11 {
 12     printf("%s,[ip],[port]\n",_port);
 13 }
 14 int main(int argc,char *argv[])
 15 {
 16     if(argc!=3)
 17     {
 18         usage(argv[0]);
 19         return 1;
 20     }
 21     int port = atoi(argv[2]);
 22     char *ip = argv[1];
 23     int sock = socket(AF_INET,SOCK_STREAM,0);
 24     if(sock<0)
 25     {
 26         perror("socket");
 27         exit(1);
 28     }
 29     struct sockaddr_in remote;
 30     remote.sin_family = AF_INET;
 31     remote.sin_port = htons(port);
 32     remote.sin_addr.s_addr = inet_addr(ip);
 33     int ret = connect(sock,(struct sockaddr*)&remote,sizeof(remote));
 34     if(ret<0)
 35     {
 36         perror("connect");
 37         exit(1);
 38     }
 39     char buf[1024];
 40     while(1)
 41     {
 42 
 43         memset(buf,&#39;\0&#39;,sizeof(buf));
 44         printf("please say:");
 45         scanf("%s",buf);
 46         ssize_t size = write(sock,buf,sizeof(buf)-1);
  47         if(size<0)
 48         {
 49             perror("write");
 50             exit(2);
 51         }
 52         else if(size ==0)
 53         {}
 54         else
 55         {
 56             printf("client : %s\n",buf);
 57         }
 58     }
 59     return 0;
 60 }
[fbl@localhost select_socket]$ ./server 192.168.1.106 8080
timeout...
client:hello
client:hi
client:huowo
client close...
client:
read: Bad file descriptor
[fbl@localhost select_socket]$ 
[fbl@localhost select_socket]$ ./client 192.168.1.106 8080
please say:hello
client : hello
please say:hi
client : hi
please say:huowo
client : huowo
please say:^C
[fbl@localhost select_socket]$

The above is the detailed content of What does select mean in I/O multiplexing?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
使用golang进行Select Channels Go并发式编程的异步处理方法使用golang进行Select Channels Go并发式编程的异步处理方法Sep 28, 2023 pm 05:27 PM

使用golang进行SelectChannelsGo并发式编程的异步处理方法引言:并发式编程是现代软件开发中的一个重要领域,它可以有效地提高应用程序的性能和响应能力。在Go语言中,使用Channels和Select语句可以简单而高效地实现并发编程。本文将介绍如何使用golang进行SelectChannelsGo并发式编程的异步处理方法,并提供具体的

jquery如何隐藏select元素jquery如何隐藏select元素Aug 15, 2023 pm 01:56 PM

jquery隐藏select元素的方法:1、hide()方法,在HTML页面中引入jQuery库,可以使用不同选择器来隐藏select元素,ID选择器将selectId替换为你实际使用的select元素的ID;2、css()方法,使用ID选择器选择需要隐藏的select元素,使用css()方法将display属性设置为none,并将selectId替换为select元素的ID。

jQuery中如何实现select元素的改变事件绑定jQuery中如何实现select元素的改变事件绑定Feb 23, 2024 pm 01:12 PM

jQuery是一个流行的JavaScript库,可以用来简化DOM操作、事件处理、动画效果等。在web开发中,经常会遇到需要对select元素进行改变事件绑定的情况。本文将介绍如何使用jQuery实现对select元素改变事件的绑定,并提供具体的代码示例。首先,我们需要使用标签来创建一个包含选项的下拉菜单:

linux要用select的原因是什么linux要用select的原因是什么May 19, 2023 pm 03:07 PM

因为select可以使开发者在同时等待多个文件缓冲区,可减少IO等待的时间,能够提高进程的IO效率。select()函数是IO多路复用的函数,允许程序监视多个文件描述符,等待所监视的一个或者多个文件描述符变为“准备好”的状态;所谓的”准备好“状态是指:文件描述符不再是阻塞状态,可以用于某类IO操作了,包括可读,可写,发生异常三种。select是一个计算机函数,位于头文件#include。该函数用于监视文件描述符的变化情况——读写或是异常。1.select函数介绍select函数是IO多路复用的函

mysql的select语法怎么使用mysql的select语法怎么使用Jun 01, 2023 pm 07:37 PM

1、SQL语句中的关键词对大小写不敏感,SELECT等效于SELECT,FROM等效于from。2、从users表中选择所有列的,可以用符号*代替列的名称。语法--这是注释--从FEOM指定的[表中],查询出[所有的]数据.*表示[所有列]SELECT*FROM--通过从FROM从指定的[表中],查询出指定列名称(字段)的数据SELECT列名称FROM表名称实例--注意:多个列之间,使用英文的逗号来分隔selectusername,passwordfrom

通过golang实现Select Channels Go并发式编程的性能优化通过golang实现Select Channels Go并发式编程的性能优化Sep 27, 2023 pm 01:09 PM

通过golang实现SelectChannelsGo并发式编程的性能优化在Go语言中,使用goroutine和channel实现并发编程是非常常见的。而在处理多个channel的情况下,我们通常会使用select语句来进行多路复用。但是,在大规模并发的情况下,使用select语句可能会导致性能下降。在本文中,我们将介绍一些通过golang实现select

使用golang实现可靠性和鲁棒性的Select Channels Go并发式编程使用golang实现可靠性和鲁棒性的Select Channels Go并发式编程Sep 28, 2023 pm 05:37 PM

使用Golang实现可靠性和鲁棒性的SelectChannelsGo并发式编程引言:在现代软件开发中,并发性已经成为了一个非常重要的主题。使用并发编程可以使得程序更具有响应性、更高效地利用计算资源,并且能够更好地处理大规模的并行计算任务。Golang是一种非常强大的并发编程语言,它通过go协程和channel机制,提供了一种简单而有效的方式来实现并发编程

MySQL的select语句如何使用MySQL的select语句如何使用May 27, 2023 pm 01:18 PM

select语句可以用回车分隔$sql="select*fromarticlewhereid=1"和$sql="select*fromarticlewhereid=1"都可以得到正确的结果,但有时分开写或许能更明了一点,特别是当sql语句比较长时。批量查询数据可以用in来实现$sql="select*fromarticlewhereid;in(1,3,5)"使用concat连接查询的结果$sql="selectconcat(i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),