In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
The system provides the select function to implement the multiplexing Icano model, and the select system call is used to allow our program to monitor the state changes of multiple file handles. The program stops here in select and waits until one or more of the monitored file handles have a state change.
Select API:
1) the nfds parameter specifies the total number of file descriptors being monitored. It is usually set to add 1 to the maximum value of all file descriptors that select listens to, because file descriptors are counted from 0.
2) the readfds, writefds, and exceptfds parameters are input-output parameters that point to the set of file descriptors corresponding to readable, writable, and exception events, respectively. As an input parameter, when an application calls the select parameter, it passes in the file descriptor of interest through these three parameters. As output parameters, when the select call returns, the kernel modifies them to inform the application which file descriptors are ready.
The fd_set structure contains only an integer array, and each bit of each element of the array is marked with a file descriptor. The number of file descriptors that fd_set can hold is specified by FD_SETSIZE, which limits the number of file descriptors that select can process at the same time.
The system provides a series of macros to access bits in the fd_set structure:
3) the timeout parameter is used to set the timeout of the select function. It is a pointer of the timeval structure type, and the pointer parameter is set to the input-output parameter, and the kernel modifies it to tell the application how long the select waited.
Select provides us with a microsecond timing method. If both tv_sec and tv_usec of the timeout variable are passed 0, select will return immediately. If you pass NULL to timeout, select will block until a file descriptor is ready.
Select returns the total number of ready (readable, writable, and exception) file descriptors when successful.
If no file descriptor is ready within the timeout, select returns 0.
Returns-1 when select fails and sets errno. If the program receives a signal while select is waiting, select immediately returns-1 and sets errno to EINTER.
Understand select
The key to the select model is to understand fd_set. If fd_set is taken as one 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 fd.
(1) execution
Fd_set set;FD_ZERO & set)
Then the set is 0000 0000 in bits.
(2) if fd=5, execute
FD_SET (fd, & set)
After that, the set becomes 0001 0000 (bit 5 is set to 1).
(3) if fd = 2 is added, the set becomes 0001 0011.
(4) execution
Select (6, & set, NULL, NULL, NULL)
Blocking waiting.
(5) if readable events occur on both fd = 1 and fd = 2, select returns, and the set becomes 0000 0011.
Note: fd = 5 with no event is emptied.
The characteristics of select model:
(1) the number of file descriptors that can be monitored depends on the value of sizeof (fd_set), which is generally 512 or 1024. If the value of sizeof (fd_set) on the server is 512 and each bit represents a file descriptor, the maximum file descriptor supported on the server is 512 * 8 = 4096. If you want to resize fd_set, you can compile the kernel or other methods.
(2) when adding fd to select monitoring, we should also use an array Array to save the fd placed in the select monitoring set. First, after the select is returned, the Array is used as the original data and fd_set to make FD_ISSET judgment. Second, when select returns, it will empty the previously added fd but no event occurs, so before starting select, you have to re-obtain fd from Array to join one by one (FD_ZERO is the first). Scan Array and get the maximum value of fd max_fd, which is used for the first parameter of select.
(3) the select model must traverse Array before select (add fd, take max_fd), and traverse Array after select returns (FD_ISSET to determine whether an event has occurred).
Advantages of select:
The performance of select is much higher than that of multi-process and multi-thread.
Disadvantages of select:
(1) the number of file descriptors monitored by select is limited.
(2) every time you call select, you need to copy the fd collection from the user mode to the kernel state. This overhead will be very high in many cases of fd, resulting in server performance degradation.
(3) each call has to traverse all the fd passed in by the kernel, which is also very expensive in many cases of fd.
TCP server using select
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.