select function

select function:

  The system provides the select function to implement the multiplexed input/output model. prototype:

  #include <sys/time.h>

  #include <unistd.h>

  select function:

  The system provides the select function to implement the multiplexed input/output model. prototype:

  #include <sys/time.h>

  #include <unistd.h>

  int select(int maxfd,fd_set *rdset,fd_set *wrset,fd_set *exset,struct timeval *timeout);

  The parameter maxfd is the maximum file descriptor value to be monitored + 1; rdset, wrset, and exset correspond to the set of readable file descriptors, the set of writable file descriptors, and the set of abnormal file descriptors to be detected, respectively. The struct timeval structure is used to describe the length of time. If no event occurs in the descriptor to be monitored within this time, the function returns, and the return value is 0.

  FD_ZERO, FD_SET, FD_CLR, FD_ISSET: The parameter maxfd is the maximum file descriptor value to be monitored + 1; rdset, wrset, exset correspond to the set of readable file descriptors to be detected, the set of writable file descriptors and A collection of exception file descriptors. The struct timeval structure is used to describe the length of time. If no event occurs in the descriptor to be monitored within this time, the function returns, and the return value is 0.

  FD_ZERO,FD_SET,FD_CLR,FD_ISSET:

  FD_ZERO(fd_set *fdset); Clear the specified file descriptor set. Before setting the file descriptor set, it must be initialized. If it is not cleared, it is usually not cleared after the system allocates memory space. So the result is unknowable.

  FD_SET(fd_set *fdset); is used to add a new file descriptor to the file descriptor set.

  FD_CLR(fd_set *fdset); is used to delete a file descriptor in the file descriptor set.

  FD_ISSET(int fd, fd_set *fdset); is used to test whether the specified file descriptor is in the set.

  struct timeval structure:

  struct timeval{

  long tv_sec;//second

  long tv_usec;//minisecond

  }

  timeout setting:

  null:select will block until an event occurs on a file descriptor.

  0: Only check the state of the descriptor set, and then return immediately, without waiting for the occurrence of external events.

  Specific time value: If no event occurs within the specified time period, select will timeout and return.

  --

  ('fd_set') is a set of file descriptors (fd). Since the length of the fd_set type varies on different platforms, a standard set of macro definitions should be used to handle such variables as follows:

fd_set set;

FD_ZERO(&set); /* clear set*/ 

FD_SET(fd, &set); /* Add fd to set */ 

FD_CLR(fd, &set); /* clear fd from set */     

 FD_ISSET(fd, &set); /* true if fd is in set */

  In the past, an fd_set can usually only contain less than or equal to 32 file descriptors, because fd_set is actually implemented with a bit vector of int. In most cases, it is the system to check that fd_set can contain any file descriptors. responsibility, but to determine how much your fd_set can fit sometimes you should check/modify the value of macro FD_SETSIZE. *This value is system dependent*, also check the man page for select() on your system. Some systems have problems with support for more than 1024 file descriptors.

  The multiplexing method is a real practical server program, and the non-multiplexing network program can only be used as a learning or accompanying test role. This article talks about the individual

  Multiplexing functions that have been touched: select/poll/epoll/port. I haven't touched the *nix system of kqueue, so I guess I'm familiar with it

  Four, kqueue just needs to be familiar with it.

 

 

select model

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

  The parameter n represents the maximum value + 1 in all the monitored fds.

  The four macros that are closely combined with the select model, the meaning is not explained:

  FD_CLR(int fd, fd_set *set);

  FD_ISSET(int fd, fd_set *set);

  FD_SET(int fd, fd_set *set);

  FD_ZERO(fd_set *set);

  The key to understanding the select model is to understand fd_set. For the convenience of explanation, the length of fd_set is 1 byte, and each bit in fd_set can correspond to a file descriptor fd. Then a 1-byte fd_set can correspond to a maximum of 8 fds.

  (1) Execute fd_set set; FD_ZERO(&set); then set is 0000,0000 in bits.

  (2) If fd=5, execute FD_SET(fd, &set); after the set becomes 0001,0000 (the fifth position is 1)

  (3) If fd=2 and fd=1 are added, the set becomes 0001, 0011

  (4) Execute select(6,&set,0,0,0) to block and wait

  (5) If a readable event occurs on both fd=1 and fd=2, select returns, and set becomes 0000,0011 at this time. Note: fd=5 where no event occurs is cleared.

  Based on the above discussion, the characteristics of the select model can be easily derived

  (1) The number of file descriptors that can be monitored depends on the value of sizeof(fd_set). On my server, sizeof(fd_set)=512, and each bit represents one file descriptor, so the maximum file descriptor supported on my server is 512*8=4096. It is said that it is adjustable, and it is said that although it is adjustable, the upper limit of adjustment is subject to the variable value when compiling the kernel. I am not very interested in adjusting the size of fd_set, which can effectively break through the upper limit of file descriptors that can be monitored by select.

  (2) When adding fd to the select monitoring set, a data structure array should be used to save the fd in the select monitoring set. One is to use the array as the source data and fd_set for FD_ISSET judgment after the select returns. The second is that after the select returns, the previously added fds but no events have occurred will be cleared, and each time before starting the selection, the fds must be re-obtained from the array and added one by one (FD_ZERO is the first), and the maximum fd value maxfd is obtained while scanning the array. The first parameter for select.

  (3) It can be seen that the select model must loop the array before the select (add fd, take maxfd), and loop the array after the select returns (FD_ISSET determines whether there is time to happen).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324483224&siteId=291194637