In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The main content of this article is to explain the case analysis of Linux I Pot O Multiplexing. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Linux I Band O multiplexing case analysis" it!
Icano multiplexing monitors multiple descriptors through a mechanism, and once a descriptor is ready (usually read-ready or write-ready), it can inform the program to read and write accordingly. Icano multiplexing technology is a technology to solve the problem that a process or thread blocks to a certain Icano system call, so that the process does not block a specific Icano system call.
Ipaw O Multiplexing select
This function allows the process to instruct the kernel to wait for any one of the multiple events to be sent and to wake up only after one or more events have occurred or experienced a specified period of time.
The select function 1.1 requires the header file # include # include1.2 declaration and return value
\ 1. Statement
Int select (int nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval * timeout)
\ 2. Return value
Success: the number of ready descriptors, timeout returns 0.
Error:-1.
1.3 function
Monitor and wait for property changes (readable, writable, or error exceptions) of multiple file descriptors. The file descriptors monitored by the select () function fall into three categories: writefds, readfds, and exceptfds. When called, the select () function blocks until the descriptor is ready (data is readable, writable, or has an error exception), or times out (timeout specifies the wait time). When the select () function returns, you can find the ready descriptor by traversing the fdset.
1.4 parameters
\ 1. Nfds: the range of file descriptors to be monitored. Generally, take + 1 as the maximum number of monitored descriptors, such as 10 here. In this case, the descriptor 0 ~ ~ 1, 2... 9 is monitored, and the maximum value on Linux is generally 1024.
2. Readfd: a collection of monitored readable descriptors that are stored here as long as a file descriptor is about to be read.
\ 3. Writefds: a collection of writable descriptors for monitoring.
\ 4. Exceptfds: a collection of monitored error exception descriptors.
\ 5. Timeout tells the kernel how long it can take to wait for any of the specified descriptions to be ready. Its timeval structure is used to specify the number of seconds and microseconds during this period.
Struct timeval {long tv_sec; / / secondslong tv_usec; / / microseconds}
The values that timeout can set:
1. Set this parameter to null pointer NULL. Means to wait forever and return only when a description word is ready for Iripple O.
2. Set this parameter to a value that specifies the number of seconds and microseconds in the timeval structure. Indicates that the timeout time is specified for waiting, and returns directly when no description word is ready for Icord O after the timeout.
3. Set this parameter to specify the number of seconds and microseconds in the timeval structure, and both seconds and microseconds are 0. Indicates that the description word is not checked to see if it is ready for Icano and returns as soon as it is ready, which is called polling.
1.5 fd_set
Fd_set can be understood as a collection in which file descriptors are stored and can be set by the following four macros:
1. Void FD_ZERO (fd_set * fdset); / / clear the collection 2. Void FD_SET (int fd, fd_set * fdset); / / add a given file descriptor to the collection 3. Void FD_CLR (int fd, fd_set * fdset); / / remove a given file descriptor from the collection 4. Int FD_ISSET (int fd, fd_set * fdset) / / check whether the file descriptor specified in the collection can read and write the advantages and disadvantages of select
Select () is currently supported on almost all platforms, and its good cross-platform support is also an advantage.
2.2 shortcomings
1. Every time you call select (), you need to copy the fd collection from the user state to the kernel state, which is very expensive in many cases of fd. At the same time, every time you call select (), you need to traverse all the fd passed in the kernel, and this overhead is also high in many cases of fd.
2. There is a maximum limit on the number of file descriptors that a single process can monitor, which is generally 1024 on Linux. This limit can be raised by modifying macro definitions or even recompiling the kernel, but this will also reduce efficiency.
Ipaw O Multiplexing poll
The essence of select () is the same as that of poll () system calls, the former introduced in BSD UNIX and the latter introduced in System V. The mechanism of poll () is similar to that of select (), which is essentially not much different from select (). Managing multiple descriptors is also polled and processed according to the state of the descriptors, but poll () has no limit on the maximum number of file descriptors (but performance will degrade if the number is too large). Poll () and select () also have the disadvantage that an array containing a large number of file descriptors is copied between the user state and the kernel address space as a whole, and its overhead increases linearly with the number of file descriptors, regardless of whether these file descriptors are ready or not.
Poll function
1.1 header file required
# include1.2 declaration and return value
\ 1. Statement
Int poll (struct pollfd * fds, nfds_t nfds, int timeout)
\ 2. Return value
On success, poll () returns the number of file descriptors in the structure whose revents field is not 0; if no event occurs before the timeout, poll () returns 0
On failure, poll () returns-1 and sets errno to one of the following values:
EBADF: the file descriptor specified in one or more structures is invalid.
The address pointed to by the EFAULT:fds pointer exceeds the address space of the process.
EINTR: a signal is generated before the requested event, and the call can be reinitiated.
The EINVAL:nfds parameter exceeds the PLIMIT_NOFILE value.
ENOMEM: not enough memory is available to complete the request.
1.3 function
Monitor and wait for the properties of multiple file descriptors to change.
1.4 parameters
\ 1. Unlike select (), which uses three bitmaps to represent three fdset, poll () is implemented using a pointer to a pollfd. An array of pollfd structures, which contains the file descriptors and events you want to test. The events are determined by the event field events in the structure, and the actual occurrence time after the call will be filled in the revents field of the structure.
Struct pollfd {int fd; / / File descriptor short events; / / events waiting short revents; / / events actually occurring}
Fd each pollfd structure specifies a monitored file descriptor, which can pass multiple structures, instructing poll () to monitor multiple file descriptors.
Events: the events domain of each structure is the event mask that monitors the file descriptor and is set by the user. The mask values for events wait events are as follows:
Process input:
POLLIN normal or priority with readable data
POLLRDNORM ordinary data is readable
POLLRDBAND priority with readable data
POLLPRI high priority data readable
Processing output:
POLLOUT normal or priority with writable data
POLLWRNORM ordinary data is writable
POLLWRBAND priority with writable data
Handling errors:
An error occurred in POLLERR
POLLHUP occurs and hangs
The POLLVAL description word is not an open file
Poll () handles three levels of data, normal normal, priority with priority band, and high priority high priority, all of which are implemented out of the stream.
POLLIN | POLLPRI is equivalent to the read event of select ().
POLLOUT | POLLWRBAND is equivalent to the write event of select ().
POLLIN is equivalent to POLLRDNORM | POLLRDBAND.
POLLOUT is equivalent to POLLWRNORM.
For example, to monitor whether a file descriptor is readable and writable at the same time, we can set events to POLLIN | POLLOUT.
The revents field is the event mask for the result of the operation of the file descriptor, which is set by the kernel when the call returns. Any event requested in the events domain may be returned in the revents domain. The events field for each structure is set by the user to tell the kernel what we are focused on, while the revents field is set by the kernel when it returns to indicate what event occurred to the descriptor.
\ 2. Nfds is used to specify the number of elements in the first parameter array.
3. Timeout: specify the number of milliseconds to wait.
If timeout is set to the number of milliseconds to wait, poll () will return regardless of whether the iUnip O is ready or not.
If timeout is set to 0, the poll () function returns immediately.
If timeout is set to-1, poll () blocks until a specified event occurs.
Ipaw O Multiplexing epoll
Epoll is proposed in the 2.6 kernel and is an enhanced version of the previous select () and poll (). Compared to select () and poll (), epoll is more flexible and has no descriptor restrictions. Epoll uses a file descriptor to manage multiple descriptors, storing the events of the file descriptor of user relations in a kernel event table, so that copy is only needed once in user space and kernel space.
Need header file # include declaration int epoll_create (int size); int epoll_ctl (int epfd, int op, int fd, struct epoll_event * event); int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout); epoll_create function
Int epoll_create (int size); 3.1 function
This function generates a file descriptor specific to epoll (creates a handle to epoll). 3.2 parameters
Size is used to tell the kernel the total number of listeners. The parameter size does not limit the maximum number of descriptors that epoll can listen on, but is just a suggestion for the kernel to initially allocate internal data structures.
Since linux 2.6.8, the size parameter has been ignored, which means you can fill in any value that is only greater than 0. It should be noted that when the epoll handle is created, it will occupy an FD value. If you look at the / proc/ process id/fd/, under linux, you can see this fd, so after using epoll, you must call close () to close, otherwise it may cause fd to be exhausted.
3.3 return value
Success: file descriptor specific to epoll
Failed:-1
Epoll_ctl function int epoll_ctl (int epfd, int op, int fd, struct epoll_event * event)
4.1 Featur
Epoll's event registration function, which, unlike select (), tells the kernel what type of event to listen for when listening for events, but registers the type of event to listen for first.
4.2 parameters
1. Epfd epoll-specific file descriptor, the return value of epoll_create ()
2. Op represents an action, which is represented by three macros:
EPOLL_CTL_ADD: register a new fd into the epfd; EPOLL_CTL_MOD: modify the listening event of a registered fd; EPOLL_CTL_DEL: delete a fd from the epfd
3. File descriptors that fd needs to listen to
4. Event tells the kernel what events to listen for. The structure of struct epoll_event is as follows:
/ / Save the data related to a file descriptor that triggers the event (depending on how it is used) typedef union epoll_data {void * ptr;int fd;__uint32_t u32 / triggering event struct epoll_event {_ _ uint32_t events; / * Epoll events * / epoll_data_t data; / * User data variable * /}
An events can be a collection of the following macros:
EPOLLIN: indicates that the corresponding file descriptor can be read (including the peer SOCKET is normally closed)
EPOLLOUT: indicates that the corresponding file descriptor can be written
EPOLLPRI: indicates that the corresponding file descriptor has urgent data to read (here it should indicate that out-of-band data is coming)
EPOLLERR: indicates that an error occurred in the corresponding file descriptor
EPOLLHUP: indicates that the corresponding file descriptor is hung up
EPOLLET: set EPOLL to Edge Triggered mode, as opposed to horizontal trigger (Level Triggered).
EPOLLONESHOT: only listen for one event. After listening to this event, if you need to continue listening to the socket, you need to add the socket to the EPOLL queue again.
4.3 return value
Success: 0
Failed:-1
Epoll_wait function
Int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout)
5.1 function
Waiting for events to be generated, collecting events that have been sent in the events monitored by epoll, similar to the call to select ().
5.2 parameters
1. Epfd epoll-specific file descriptor, the return value of epoll_create ()
2. The epoll_event structure array assigned by events. Epoll will assign events to the events array (events cannot be a null pointer. The kernel is only responsible for copying data to this events array, not to help us allocate memory in user mode).
3. Maxevents maxevents tells the kernel how big the events is.
4. Timeout timeout.
If timeout is set to the number of milliseconds to wait, it will be returned regardless of whether or not the Ibind O is ready.
If timeout is set to 0, the function returns immediately.
If timeout is set to-1, it blocks until a specified event occurs.
5.3 return value
Success: returns the number of events that need to be processed. A return of 0 indicates a timeout.
Failed:-1
LT mode and ET mode
Epoll operates on file descriptors in two modes: LT (level trigger) and ET (edge trigger). LT mode is the default mode.
6.1 LT mode
When epoll_wait detects that a descriptor event occurs and notifies the application of the event, the application can not process the event immediately. The next time epoll_wait is called, the application responds again and this event is notified.
6.2 ET model
When epoll_wait detects that a descriptor event occurs and notifies the application of the event, the application must process the event immediately. If not, the next time epoll_wait is called, the application will not respond again and this event will not be notified.
6.3 comparison between LT mode and ET mode
ET mode greatly reduces the number of times epoll events are triggered repeatedly, so it is more efficient than LT mode. When epoll works in ET mode, it must use a non-blocking socket to avoid starving the task of processing multiple file descriptors due to a blocking read / write operation of one file handle.
Advantages of epoll
1. In select/poll, the kernel scans all monitored file descriptors only after the process calls a certain method, and epoll () registers a file descriptor through epoll_ctl () in advance. Once a file descriptor is ready based on a file descriptor, the kernel uses a callback mechanism similar to callback (software interrupt) to activate the file descriptor quickly and is notified when the process calls epoll_wait ().
2. The number of monitoring descriptors is unlimited, and the upper limit of FD it supports is the maximum number of files that can be opened. This number is generally much greater than 2048. For example, on a machine with 1GB memory, it is about 100000, and the specific number can be viewed by cat / proc/sys/fs/file-max. Generally speaking, this number has a lot to do with system memory. The biggest disadvantage of select () is that there is a limit to the number of fd opened by the process. This is simply not enough for servers with a large number of connections. Although you can choose a multi-process solution (Apache is implemented in this way), although the cost of creating a process on Linux is relatively small, it can not be ignored, and inter-process data synchronization is not as efficient as inter-thread synchronization, so it is not a perfect solution.
3. The efficiency of fd O does not decrease with the increase of the number of monitors. The select (), poll () implementation requires itself to poll all the fd collections until the device is ready, possibly alternating sleep and wake up multiple times. In fact, epoll also needs to call epoll_wait () to constantly poll the ready list, and it may alternate between sleep and wake up many times, but it calls the callback function when the device is ready, puts the ready fd in the ready list, and wakes up the process that goes to sleep in epoll_wait (). Although both sleep and alternate, select () and poll () traverse the entire fd collection when they are awake, while epoll simply determines whether the ready list is empty when it is awake, which saves a lot of CPU time. This is the performance improvement brought about by the callback mechanism.
4. Select (), poll () copy the fd collection from the user state to the kernel state once for each call, while epoll can save a lot of overhead as long as it is copied once.
At this point, I believe that you have a deeper understanding of the "Linux I Pot O Multiplexing case Analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.