#include <sys/time.h>/* for portability */#include <sys/select.h>int select (int Nfds, fd_set *readfds, fd_s Et *writefds, fd_set *exceptfds, struct timeval *timeout); Returns number of Ready file descriptors, 0 on timeout, or–1 on Error#include <sys/select.h>void Fd_zero (fd_set *fd Set), void Fd_set (int fd, fd_set *fdset), void fd_clr (int fd, fd_set *fdset), int fd_isset (int fd, fd_set *fdset); Returns true (1) if FD is in Fdset, or False (0) Otherwisefd_zero () initializes the set pointed to by Fdset to be empty. Fd_set () adds the file descriptor FD to the SET pointed to by Fdset. FD_CLR () removes the file descriptor FD from the set pointed to by Fdset. Fd_isset () returns True if the file descriptor FD is a member of the set pointed Toby Fdset.struct timeval {time_t tv_ Sec /* Seconds */suseconds_t tv_usec;/* microseconds (long int) */};typedef struct{/*xpg4.2requiresthismembername.othe Rwiseavoidthename fromtheglobalnamespace.*/#ifdef__USE_XOPEN __fd_maskfds_bits[__fd_setsize/__nfdbits]; #define__FDS_BITS (set) (set)->fds_bits #else __fd_mask__fds_bits[__fd_setsize/__nfdbits]; #define__FDS_BITS (set) (set)->__fds_bits #endif}fd_set; /* Linux for 1024/8 = Bytes */------------------#include <sys/epoll.h>int epoll_create (int size); Returns file descriptor on success, or–1 on errorstarting with kernel 2.6.27, Linux supports a new system call, Epoll_cre Ate1 (). This system call performs the same task as epoll_create (), but drops the obsoletesize argument and adds a flags argument t Hat can is used to modify the Behaviorof the system call. One flag is currently supported:epoll_cloexec, which causesthe kernel to enable the CLOSE-ON-EXEC flag (fd_cloexec) for T He new file descriptor. This flag was useful for the same reasons as the open () o_cloexec flag described insection 4.3.1#include <sys/epoll.h> ; int epoll_ctl (int epfd, int op, int fd, struct epoll_event *ev); Returns 0 on success, or–1 onErrorthe FD argument identifies which of the file descriptors in the interest list are to Haveits settings modified. This argument can is a file descriptor for a ' pipe, fifo,socket, POSIX message queue, inotify instance, terminal, device, Or even another epolldescriptor ' (i.e., we can build a kind of hierarchy of monitored descriptors). HOWEVER,FD can ' t is a file descriptor for a regular file or a directory (the error eperm results). The OP argument specifies the operation to being performed, and has one of the thefollowing Values:epoll_ctl_addepoll_ctl_modepol L_ctl_del Total 12 Bytes: struct epoll_event {uint32_t events; /* Epoll events (bit mask) */epoll_data_t data; /* User data */}; The data field of the epoll_event structure is typed as Follows:typedef Union epoll_data {void *ptr; Pointer to user-defined data int fd; File Descriptor */uint32_t u32; 32-bit integer */uint64_t u64; 64-bit integer */} epoll_data_t; #include <sys/epoll.h>int epoll_wait (int epfd, struct epoll_event *evlist, int maxevents, int timeout); Returns number of Ready file descriptors, 0 on timeout, or–1 on errortable 63-8: Bit-mask values for the Epoll events fie LD Bitepollin data other than high-priority data can is READEPOLLPRI high-priority data can be read Epollrdhup Shutdown on Peer sockets (since Linux 2.6.17) epollout Normal data can be Writtenepollet Employ edge-triggered event Notificationepolloneshot Disable monitoring after event Notificationepollerr An error has occurredepollhup A Hangup have occurredinput to returned Byepoll_ctl ()? Epoll_wait ()?
GCC epoll_input_fifo.c lib/error_functions.c lib/get_num.c-ilib-o epoll_input_fifo.out#include <sys/epoll.h > #include <fcntl.h> #include "tlpi_hdr.h" #define MAX_BUF 1000#define max_events 5int Main (int argc, char *argv[] ) {int EPFD, ready, FD, S, J, Numopenfds; struct epoll_event ev; struct Epoll_event evlist[max_events]; Char Buf[max_buf]; printf ("epollin:0x%x\n", Epollin); printf ("epollhup:0x%x\n", epollhup); printf ("epollerr:0x%x\n", Epollerr); if (argc < 2 | | strcmp (argv[1], "--help") = = 0) usageerr ("%s file...\n", argv[0]); EPFD = Epoll_create (argc-1); if (EPFD = =-1) errexit ("Epoll_create"); for (j = 1; j < argc; J + +) {fd = open (Argv[j], o_rdonly); if (fd = =-1) {Errexit ("open"); } printf ("Opened '%s ' on FD%d\n", Argv[j], FD); Ev.events = Epollin; Only interested in input events EV.DATA.FD = FD; if (Epoll_ctl (EPFD, Epoll_ctl_add, FD, &ev) = =-1) Errexit ("Epoll_ctl"); } Numopenfds = argc-1; while (Numopenfds > 0) {printf ("About-epoll_wait () \ n"); Ready = Epoll_wait (EPFD, Evlist, Max_events,-1); if (ready = =-1) {if (errno = = eintr) continue; else Errexit ("epoll_wait"); } printf ("Ready:%d\n", ready); for (j=0;j<ready;j++) {printf ("fd=%d; Events:%s%s%s\n ", EVLIST[J].DATA.FD, (Evlist[j].events & Epollin)? "Epollin": "", (Evlist[j].events & epollhup)? " Epollhup ":" ", (Evlist[j].events & Epollerr)?" Epollerr ":"); if (Evlist[j].events & Epollin) {s = Read (EVLIST[J].DATA.FD, buf, max_buf); if (s = =-1) errexit ("read"); printf ("read%d bytes:%.*s\n", s,s,buf); } else if (Evlist[j].events & (Epollhup | Epollerr)) { printf ("Closing fd%d\n", EVLIST[J].DATA.FD); if (Close (EVLIST[J].DATA.FD) ==-1) errexit ("Close"); numopenfds--; }}} printf ("All file descriptors closed. Bye\n "); Exit (exit_success);}
From: "The Linux programming Interface" P1355
Linux Epoll Learning