{"id":3976,"date":"2022-12-20T17:28:34","date_gmt":"2022-12-20T20:28:34","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/epoll-man7\/"},"modified":"2022-12-20T17:28:34","modified_gmt":"2022-12-20T20:28:34","slug":"epoll-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/epoll-man7\/","title":{"rendered":"EPOLL (man7)"},"content":{"rendered":"<h1 align=\"center\">EPOLL<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#VERSIONS\">VERSIONS<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">epoll \u2212 I\/O event notification facility<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <sys\/epoll.h><\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>epoll<\/b> API performs a similar task to <b>poll<\/b>(2): monitoring multiple file descriptors to see if I\/O is possible on any of them. The <b>epoll<\/b> API can be used either as an edge-triggered or a level-triggered interface and scales well to large numbers of watched file descriptors.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The central concept of the <b>epoll<\/b> API is the <b>epoll<\/b> <i>instance<\/i>, an in-kernel data structure which, from a user-space perspective, can be considered as a container for two lists:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"86%\">\n<p>The <i>interest<\/i> list (sometimes also called the <b>epoll<\/b> set): the set of file descriptors that the process has registered an interest in monitoring.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"86%\">\n<p>The <i>ready<\/i> list: the set of file descriptors that are &#8220;ready&#8221; for I\/O. The ready list is a subset of (or, more precisely, a set of references to) the file descriptors in the interest list. The ready list is dynamically populated by the kernel as a result of I\/O activity on those file descriptors.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The following system calls are provided to create and manage an <b>epoll<\/b> instance:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"86%\">\n<p style=\"margin-top: 1em\"><b>epoll_create<\/b>(2) creates a new <b>epoll<\/b> instance and returns a file descriptor referring to that instance. (The more recent <b>epoll_create1<\/b>(2) extends the functionality of <b>epoll_create<\/b>(2).)<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"86%\">\n<p>Interest in particular file descriptors is then registered via <b>epoll_ctl<\/b>(2), which adds items to the interest list of the <b>epoll<\/b> instance.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"86%\">\n<p><b>epoll_wait<\/b>(2) waits for I\/O events, blocking the calling thread if no events are currently available. (This system call can be thought of as fetching items from the ready list of the <b>epoll<\/b> instance.)<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Level-triggered and edge-triggered<\/b> <br \/> The <b>epoll<\/b> event distribution interface is able to behave both as edge-triggered (ET) and as level-triggered (LT). The difference between the two mechanisms can be described as follows. Suppose that this scenario happens:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">1.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p style=\"margin-top: 1em\">The file descriptor that represents the read side of a pipe (<i>rfd<\/i>) is registered on the <b>epoll<\/b> instance.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>2.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>A pipe writer writes 2\u00a0kB of data on the write side of the pipe.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>3.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>A call to <b>epoll_wait<\/b>(2) is done that will return <i>rfd<\/i> as a ready file descriptor.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>4.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>The pipe reader reads 1\u00a0kB of data from <i>rfd<\/i>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>5.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>A call to <b>epoll_wait<\/b>(2) is done.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">If the <i>rfd<\/i> file descriptor has been added to the <b>epoll<\/b> interface using the <b>EPOLLET<\/b> (edge-triggered) flag, the call to <b>epoll_wait<\/b>(2) done in step <b>5<\/b> will probably hang despite the available data still present in the file input buffer; meanwhile the remote peer might be expecting a response based on the data it already sent. The reason for this is that edge-triggered mode delivers events only when changes occur on the monitored file descriptor. So, in step <b>5<\/b> the caller might end up waiting for some data that is already present inside the input buffer. In the above example, an event on <i>rfd<\/i> will be generated because of the write done in <b>2<\/b> and the event is consumed in <b>3<\/b>. Since the read operation done in <b>4<\/b> does not consume the whole buffer data, the call to <b>epoll_wait<\/b>(2) done in step <b>5<\/b> might block indefinitely.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">An application that employs the <b>EPOLLET<\/b> flag should use nonblocking file descriptors to avoid having a blocking read or write starve a task that is handling multiple file descriptors. The suggested way to use <b>epoll<\/b> as an edge-triggered (<b>EPOLLET<\/b>) interface is as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">a)<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p style=\"margin-top: 1em\">with nonblocking file descriptors; and<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>b)<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>by waiting for an event only after <b>read<\/b>(2) or <b>write<\/b>(2) return <b>EAGAIN<\/b>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">By contrast, when used as a level-triggered interface (the default, when <b>EPOLLET<\/b> is not specified), <b>epoll<\/b> is simply a faster <b>poll<\/b>(2), and can be used wherever the latter is used since it shares the same semantics.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Since even with edge-triggered <b>epoll<\/b>, multiple events can be generated upon receipt of multiple chunks of data, the caller has the option to specify the <b>EPOLLONESHOT<\/b> flag, to tell <b>epoll<\/b> to disable the associated file descriptor after the receipt of an event with <b>epoll_wait<\/b>(2). When the <b>EPOLLONESHOT<\/b> flag is specified, it is the caller\u2019s responsibility to rearm the file descriptor using <b>epoll_ctl<\/b>(2) with <b>EPOLL_CTL_MOD<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If multiple threads (or processes, if child processes have inherited the <b>epoll<\/b> file descriptor across <b>fork<\/b>(2)) are blocked in <b>epoll_wait<\/b>(2) waiting on the same epoll file descriptor and a file descriptor in the interest list that is marked for edge-triggered (<b>EPOLLET<\/b>) notification becomes ready, just one of the threads (or processes) is awoken from <b>epoll_wait<\/b>(2). This provides a useful optimization for avoiding &#8220;thundering herd&#8221; wake-ups in some scenarios.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Interaction with autosleep<\/b> <br \/> If the system is in <b>autosleep<\/b> mode via <i>\/sys\/power\/autosleep<\/i> and an event happens which wakes the device from sleep, the device driver will keep the device awake only until that event is queued. To keep the device awake until the event has been processed, it is necessary to use the <b>epoll_ctl<\/b>(2) <b>EPOLLWAKEUP<\/b> flag.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When the <b>EPOLLWAKEUP<\/b> flag is set in the <b>events<\/b> field for a <i>struct epoll_event<\/i>, the system will be kept awake from the moment the event is queued, through the <b>epoll_wait<\/b>(2) call which returns the event until the subsequent <b>epoll_wait<\/b>(2) call. If the event should keep the system awake beyond that time, then a separate <i>wake_lock<\/i> should be taken before the second <b>epoll_wait<\/b>(2) call.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>\/proc interfaces<\/b> <br \/> The following interfaces can be used to limit the amount of kernel memory consumed by epoll: <i><br \/> \/proc\/sys\/fs\/epoll\/max_user_watches<\/i> (since Linux 2.6.28)<\/p>\n<p style=\"margin-left:22%;\">This specifies a limit on the total number of file descriptors that a user can register across all epoll instances on the system. The limit is per real user ID. Each registered file descriptor costs roughly 90 bytes on a 32-bit kernel, and roughly 160 bytes on a 64-bit kernel. Currently, the default value for <i>max_user_watches<\/i> is 1\/25 (4%) of the available low memory, divided by the registration cost in bytes.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Example for suggested usage<\/b> <br \/> While the usage of <b>epoll<\/b> when employed as a level-triggered interface does have the same semantics as <b>poll<\/b>(2), the edge-triggered usage requires more clarification to avoid stalls in the application event loop. In this example, listener is a nonblocking socket on which <b>listen<\/b>(2) has been called. The function <i>do_use_fd()<\/i> uses the new ready file descriptor until <b>EAGAIN<\/b> is returned by either <b>read<\/b>(2) or <b>write<\/b>(2). An event-driven state machine application should, after having received <b>EAGAIN<\/b>, record its current state so that at the next call to <i>do_use_fd()<\/i> it will continue to <b>read<\/b>(2) or <b>write<\/b>(2) from where it stopped before.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#define MAX_EVENTS 10 <br \/> struct epoll_event ev, events[MAX_EVENTS]; <br \/> int listen_sock, conn_sock, nfds, epollfd;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">\/* Code to set up listening socket, &#8216;listen_sock&#8217;, <br \/> (socket(), bind(), listen()) omitted *\/<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">epollfd = epoll_create1(0); <br \/> if (epollfd == \u22121) { <br \/> perror(&#8220;epoll_create1&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">ev.events = EPOLLIN; <br \/> ev.data.fd = listen_sock; <br \/> if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &#038;ev) == \u22121) { <br \/> perror(&#8220;epoll_ctl: listen_sock&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">for (;;) { <br \/> nfds = epoll_wait(epollfd, events, MAX_EVENTS, \u22121); <br \/> if (nfds == \u22121) { <br \/> perror(&#8220;epoll_wait&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">for (n = 0; n < nfds; ++n) { <br \/> if (events[n].data.fd == listen_sock) { <br \/> conn_sock = accept(listen_sock, <br \/> (struct sockaddr *) &#038;addr, &#038;addrlen); <br \/> if (conn_sock == \u22121) { <br \/> perror(&#8220;accept&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> } <br \/> setnonblocking(conn_sock); <br \/> ev.events = EPOLLIN | EPOLLET; <br \/> ev.data.fd = conn_sock; <br \/> if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, <br \/> &#038;ev) == \u22121) { <br \/> perror(&#8220;epoll_ctl: conn_sock&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> } <br \/> } else { <br \/> do_use_fd(events[n].data.fd); <br \/> } <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When used as an edge-triggered interface, for performance reasons, it is possible to add the file descriptor inside the <b>epoll<\/b> interface (<b>EPOLL_CTL_ADD<\/b>) once by specifying (<b>EPOLLIN<\/b>|<b>EPOLLOUT<\/b>). This allows you to avoid continuously switching between <b>EPOLLIN<\/b> and <b>EPOLLOUT<\/b> calling <b>epoll_ctl<\/b>(2) with <b>EPOLL_CTL_MOD<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Questions and answers<\/b><\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>0.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p>What is the key used to distinguish the file descriptors registered in an interest list?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">The key is the combination of the file descriptor number and the open file description (also known as an &#8220;open file handle&#8221;, the kernel\u2019s internal representation of an open file).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">1.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">What happens if you register the same file descriptor on an <b>epoll<\/b> instance twice?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">You will probably get <b>EEXIST<\/b>. However, it is possible to add a duplicate (<b>dup<\/b>(2), <b>dup2<\/b>(2), <b>fcntl<\/b>(2) <b>F_DUPFD<\/b>) file descriptor to the same <b>epoll<\/b> instance. This can be a useful technique for filtering events, if the duplicate file descriptors are registered with different <i>events<\/i> masks.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">2.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Can two <b>epoll<\/b> instances wait for the same file descriptor? If so, are events reported to both <b>epoll<\/b> file descriptors?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">Yes, and events would be reported to both. However, careful programming may be needed to do this correctly.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">3.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Is the <b>epoll<\/b> file descriptor itself poll\/epoll\/selectable?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">Yes. If an <b>epoll<\/b> file descriptor has events waiting, then it will indicate as being readable.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">4.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">What happens if one attempts to put an <b>epoll<\/b> file descriptor into its own file descriptor set?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">The <b>epoll_ctl<\/b>(2) call fails (<b>EINVAL<\/b>). However, you can add an <b>epoll<\/b> file descriptor inside another <b>epoll<\/b> file descriptor set.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">5.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Can I send an <b>epoll<\/b> file descriptor over a UNIX domain socket to another process?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">Yes, but it does not make sense to do this, since the receiving process would not have copies of the file descriptors in the interest list.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">6.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Will closing a file descriptor cause it to be removed from all <b>epoll<\/b> interest lists?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">Yes, but be aware of the following point. A file descriptor is a reference to an open file description (see <b>open<\/b>(2)). Whenever a file descriptor is duplicated via <b>dup<\/b>(2), <b>dup2<\/b>(2), <b>fcntl<\/b>(2) <b>F_DUPFD<\/b>, or <b>fork<\/b>(2), a new file descriptor referring to the same open file description is created. An open file description continues to exist until all file descriptors referring to it have been closed.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">A file descriptor is removed from an interest list only after all the file descriptors referring to the underlying open file description have been closed. This means that even after a file descriptor that is part of an interest list has been closed, events may be reported for that file descriptor if other file descriptors referring to the same underlying file description remain open. To prevent this happening, the file descriptor must be explicitly removed from the interest list (using <b>epoll_ctl<\/b>(2) <b>EPOLL_CTL_DEL<\/b>) before it is duplicated. Alternatively, the application must ensure that all file descriptors are closed (which may be difficult if file descriptors were duplicated behind the scenes by library functions that used <b>dup<\/b>(2) or <b>fork<\/b>(2)).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">7.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">If more than one event occurs between <b>epoll_wait<\/b>(2) calls, are they combined or reported separately?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">They will be combined.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">8.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Does an operation on a file descriptor affect the already collected but not yet reported events?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">You can do two operations on an existing file descriptor. Remove would be meaningless for this case. Modify will reread available I\/O.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\">9.<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Do I need to continuously read\/write a file descriptor until <b>EAGAIN<\/b> when using the <b>EPOLLET<\/b> flag (edge-triggered behavior)?<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">Receiving an event from <b>epoll_wait<\/b>(2) should suggest to you that such file descriptor is ready for the requested I\/O operation. You must consider it ready until the next (nonblocking) read\/write yields <b>EAGAIN<\/b>. When and how you will use the file descriptor is entirely up to you.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">For packet\/token-oriented files (e.g., datagram socket, terminal in canonical mode), the only way to detect the end of the read\/write I\/O space is to continue to read\/write until <b>EAGAIN<\/b>.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">For stream-oriented files (e.g., pipe, FIFO, stream socket), the condition that the read\/write I\/O space is exhausted can also be detected by checking the amount of data read from \/ written to the target file descriptor. For example, if you call <b>read<\/b>(2) by asking to read a certain amount of data and <b>read<\/b>(2) returns a lower number of bytes, you can be sure of having exhausted the read I\/O space for the file descriptor. The same is true when writing using <b>write<\/b>(2). (Avoid this latter technique if you cannot guarantee that the monitored file descriptor always refers to a stream-oriented file.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Possible pitfalls and ways to avoid them <br \/> o Starvation (edge-triggered)<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If there is a large amount of I\/O space, it is possible that by trying to drain it the other files will not get processed causing starvation. (This problem is not specific to <b>epoll<\/b>.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The solution is to maintain a ready list and mark the file descriptor as ready in its associated data structure, thereby allowing the application to remember which files need to be processed but still round robin amongst all the ready files. This also supports ignoring subsequent events you receive for file descriptors that are already ready. <b><br \/> o If using an event cache&#8230;<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you use an event cache or store all the file descriptors returned from <b>epoll_wait<\/b>(2), then make sure to provide a way to mark its closure dynamically (i.e., caused by a previous event\u2019s processing). Suppose you receive 100 events from <b>epoll_wait<\/b>(2), and in event #47 a condition causes event #13 to be closed. If you remove the structure and <b>close<\/b>(2) the file descriptor for event #13, then your event cache might still say there are events waiting for that file descriptor causing confusion.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">One solution for this is to call, during the processing of event 47, <b>epoll_ctl<\/b>(<b>EPOLL_CTL_DEL<\/b>) to delete file descriptor 13 and <b>close<\/b>(2), then mark its associated data structure as removed and link it to a cleanup list. If you find another event for file descriptor 13 in your batch processing, you will discover the file descriptor had been previously removed and there will be no confusion.<\/p>\n<h2>VERSIONS <a name=\"VERSIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>epoll<\/b> API was introduced in Linux kernel 2.5.44. Support was added to glibc in version 2.3.2.<\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>epoll<\/b> API is Linux-specific. Some other systems provide similar mechanisms, for example, FreeBSD has <i>kqueue<\/i>, and Solaris has <i>\/dev\/poll<\/i>.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The set of file descriptors that is being monitored via an epoll file descriptor can be viewed via the entry for the epoll file descriptor in the process\u2019s <i>\/proc\/[pid]\/fdinfo<\/i> directory. See <b>proc<\/b>(5) for further details.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>kcmp<\/b>(2) <b>KCMP_EPOLL_TFD<\/b> operation can be used to test whether a file descriptor is present in an epoll instance.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>epoll_create<\/b>(2), <b>epoll_create1<\/b>(2), <b>epoll_ctl<\/b>(2), <b>epoll_wait<\/b>(2), <b>poll<\/b>(2), <b>select<\/b>(2)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  epoll \u2212 I\/O event notification facility <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,1065,972],"class_list":["post-3976","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-epoll","tag-man7"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=3976"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3976\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=3976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=3976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=3976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}