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--
Multiplexing of IO: a process can monitor multiple descriptors, and once a descriptor is read or written, it can notify the process program to read and write accordingly.
Use the scene:
1. When a customer handles multiple descriptors (network sockets) or a customer handles multiple sockets at the same time
The 2.TCP server has to deal with both listening sockets and connected sockets
3. A server that handles multiple services or protocols also needs to use Icano reuse.
Compared with multi-process and multi-thread, the biggest advantage of Icano multiplexing is less system overhead, and the system does not need to create processes or threads, so there is no need to maintain these processes and threads.
Select, poll, epoll are all synchronous IO in nature, so they are all responsible for reading and writing after the read-write event is ready.
1.select
Basic principle: the file descriptor monitored by this function reads the descriptor set, the write descriptor set, and the exception descriptor set. After calling this function, select will block waiting until there is a descriptor ready (at least one), or wait for a timeout.
Advantages: good cross-platform
Disadvantages:
1. There is a limit on the number of file descriptors that can be opened by a single process. Default is 2048 on 32-bit platform and 2048 on 64-bit platform.
two。 Every time the sock set is linearly scanned (polled), it is expensive to switch from the user mode to the kernel state every time.
3. A data structure (array) for storing a large number of descriptors needs to be maintained, and the transfer and replication overhead between the kernel and user space is also high.
4. Initialize timeout before each call to the select function, and initialize the set of descriptors in which the file descriptor it cares about resides
Timeout structure in select:
1. If NULL is passed, the select is set to blocking state. Be sure to wait until the state of one or more descriptors changes.
two。 If it is set to 0 seconds and 0 milliseconds, it becomes a non-blocking function and returns immediately regardless of whether the state of the descriptor changes.
3. If set to greater than 0, the function will block within the timeout time, and return if an event arrives within the timeout period.
2.poll
Basic principle: copy the array passed by the user to the kernel area, poll to check the event status corresponding to each descriptor, join the waiting queue to continue traversing if the event is ready, and suspend the process if there is no event ready at the end of the travers. until a ready event arrives or a timeout is woken up, and then polling
Poll contains a structure that contains events to be monitored and events that occur, instead of using the parameter values passed in select.
Advantages: there is no limit on the maximum number of connections because it is stored based on linked lists
Triggered horizontally, the socket is detected again each time the function is called to see if the data in the socket buffer has been read
Disadvantages:
1. After each call to this function, the traversal descriptor is polled to get the ready socket
two。 When a large number of clients are connected at the same time, and only a few are ready at some point, its efficiency decreases as the number of monitored descriptors increases.
Applicable scenario: the number of connections is small and each connection is very active
3.epoll: use one descriptor to manage multiple descriptors, and store the corresponding events of the descriptor of interest to the kernel in the event table, which only needs to be copied once
Use three functions:
Epoll_create creates an epollfd
Epoll_ctl registers a socket descriptor
Epoll_wait waits for ready event
Basic principle: support horizontal trigger and edge trigger, generally default horizontal trigger, when using edge trigger, only tell the process which descriptors have become ready and notify only once. The file descriptor and the events it cares about will be registered first, and the kernel will use the corresponding callback mechanism to activate the descriptor, using epoll_wait to return only the events that are ready
Advantages:
1. There is no limit to the maximum concurrent connection, and the way that there is no need for polling traverses the descriptor set every time, and does not decrease with the increase of the number of descriptors.
two。 Using mmap (memory mapping technology) to accelerate message delivery to the kernel, reducing the number of copies from the user to memory
Different from select and poll, it is realized by sharing a piece of memory between the kernel and user space
Two modes of operation:
LT: when calling epoo_wait detects a descriptor event and notifies the application, the application should handle it immediately. If not, the upper application will be informed again next time.
ET: calling epoll_wait detects the occurrence of a descriptor event and notifies the upper layer application only once. Only non-blocking socket is supported (avoiding blocking waiting for subsequent multiple file descriptors due to blocking reading or writing of one handle), which greatly reduces the number of times that time is triggered repeatedly.
Applicable scenario: simultaneously handle a large number of clients and request to connect to the server at the same time
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.