Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Advanced select Omuri-multiplexing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Select for multiplexing

In the previous socket programming, we used multithreading and multi-process methods to write, and the advantage of writing with them is naturally stable, but very resource-consuming. In the previous advanced Ithumb O blog, we talked about another way, that is, efficient multiplexing, which waits for multiple file descriptors that meet the conditions at a time. Here we first introduce the first multiplexing method, select, and then we will introduce the better epoll and the best multiplexing method epoll.

The multiplexing mode of select is like this: it needs a buf to store the file descriptors that need to be monitored. First, the file descriptors of interest should be put into the corresponding set of read events or write events, and once changes occur in them, they will be reflected by parameters.

Let's take a look at the function of select first.

/ * According to POSIX.1-2001 * / # include / * According to earlier standards * / # include # include # include 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); struct timeval {long tv_sec; / * seconds * / long tv_usec; / * microseconds * /}

Nfds: add one to the number of file descriptors to be monitored. Be sure to add one!

Fd_set * readfds: indicates the set of readfds to be monitored. This fd_set has an upper limit. On my computer, it is 128, so it means that a maximum of 128 / 128 / 1024 can be monitored. Readfds is an input-output parameter, and each bit of it monitors a file descriptor. Select waits for events to occur. When a file descriptor is ready, the corresponding bit is set to 1, and the rest is cleared, so execute emty (later on) and set (later on) every time before select.

Writeds: write event file descriptor set

Exceptfds: error event file descriptor set

Timeout: set the wait event, wait for the event to do other things, such as prompts.

FD_CLR clears the set of file descriptors you are concerned about

FD_ISSET: used to check whether the file descriptor is in the corresponding collection

FD_SET: used to add file descriptors of interest to the corresponding collection

FD_ZERO: set all bit locations in the corresponding file descriptor set to 0

Here is my understanding of the workflow of select

The following is a simple code for TCPserver implemented using select

1 # include 2 # include 3 # include 4 # include 5 # include 6 # include 7 # include 8 # include 9 10 11 int fd [64]; / / select needs to create a buf 12 void usage (char * proc) 13 {14 printf ("% s + [ip] + [port]\ n", proc) for storing socket file descriptors 15} 16 17 int listen_sock (const char*ip,int port) 18 / / the socket blog in front of the listen socket has detailed 19 {20 int sock=socket (AF_INET,SOCK_STREAM,0); 21 if (sock)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report