epoll使用

来源:互联网 发布:尚学堂java架构师视频 编辑:程序博客网 时间:2024/05/21 22:39
/*************************************/
bionic/libc/include/sys/epoll.h


#define EPOLLIN          0x00000001
#define EPOLLPRI         0x00000002
#define EPOLLOUT         0x00000004
#define EPOLLERR         0x00000008
#define EPOLLHUP         0x00000010


#define EPOLL_CTL_ADD    1
#define EPOLL_CTL_DEL    2
#define EPOLL_CTL_MOD    3


typedef union epoll_data {
  void* ptr;
  int fd;
  uint32_t u32;
  uint64_t u64;
} epoll_data_t;


struct epoll_event {
  uint32_t events;
  epoll_data_t data;
}


int epoll_create(int);
int epoll_create1(int);
int epoll_ctl(int, int, int, struct epoll_event*);
int epoll_wait(int, struct epoll_event*, int, int);
int epoll_pwait(int, struct epoll_event*, int, int, const sigset_t*);


/*************************************/
NAME
epoll - I/O event notification facility


SYNOPSIS
       #include <sys/epoll.h>


DESCRIPTION
The  epoll  API  performs a similar task to poll(2): monitoring multiple file descriptors to see if I/O is possible on any of them.  
The epoll API can be used either as an edge-triggered or a level-triggered interface and scales well to large numbers of 
watched file descriptors.  
The following system calls are provided to create and manage an epoll instance:


*  epoll_create(2) creates an epoll instance and returns a file descriptor referring to that instance.
*  Interest in particular file descriptors is then registered via epoll_ctl(2).  
   The set of file descriptors currently registered on an epoll instance is sometimes called an epoll set.
*  epoll_wait(2) waits for I/O events, blocking the calling thread if no events are currently available.


/*************************************/
NAME
epoll_create, epoll_create1 - open an epoll file descriptor


SYNOPSIS
       #include <sys/epoll.h>
       int epoll_create(int size);
       int epoll_create1(int flags);


DESCRIPTION
epoll_create() creates an epoll(7) instance.  Since Linux 2.6.8, the size argument is ignored, but must be greater than zero;
epoll_create()  returns a file descriptor referring to the new epoll instance.  
This file descriptor is used for all the subsequent calls to the epoll interface.  When no longer required, the
file descriptor returned by epoll_create() should be closed by using close(2).  
When all file descriptors referring to an epoll instance have been closed, the kernel destroys the instance and
releases the associated resources for reuse.


RETURN VALUE
On success, these system calls return a nonnegative file descriptor.  
On error, -1 is returned, and errno is set to indicate the error.




epoll_ctl - control interface for an epoll descriptor
SYNOPSIS
       #include <sys/epoll.h>
       int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
DESCRIPTION
This  system call performs control operations on the epoll(7) instance referred to by the file descriptor epfd.  
It requests that the operation op be performed for the target file descriptor,fd.


Valid values for the op argument are:
EPOLL_CTL_ADD
Register the target file descriptor fd on the epoll instance referred to by the file descriptor epfd and 
associate the event event with the internal file linked to fd.


EPOLL_CTL_DEL
Remove (deregister) the target file descriptor fd from the epoll instance referred to by epfd.  
The event is ignored and can be NULL (but see BUGS below).


The event argument describes the object linked to the file descriptor fd.  
The struct epoll_event is defined as:
           typedef union epoll_data {
               void        *ptr;
               int          fd;
               uint32_t     u32;
               uint64_t     u64;
           } epoll_data_t;


           struct epoll_event {
               uint32_t     events;      /* Epoll events */
               epoll_data_t data;        /* User data variable */
           };
The events member is a bit mask composed using the following available event types:
EPOLLIN
The associated file is available for read(2) operations.
EPOLLOUT
The associated file is available for write(2) operations.


epoll_wait:

The  epoll_wait()  system  call  waits  for events on the epoll(7) instance referred to by the file descriptor epfd.  The memory area pointed to by events will contain the events that will be available for the caller.  Up to maxevents are returned by epoll_wait().  The maxevents argument must be greater than zero.
The timeout argument specifies the number of milliseconds that epoll_wait() will block.  The call will block until either:
 *  a file descriptor delivers an event;
 *  the call is interrupted by a signal handler; or
 *  the timeout expires.


使用案例
epoll_fd = epoll_create(EPOLL_SIZE);


struct epoll_event eventItem;
memset(&eventItem, 0, sizeof(eventItem));
eventItem.events = EPOLLIN;
eventItem.data.fd = fd;
printf("add  device %s: fd %d to epoll\n", device, fd);
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &eventItem);


pendingCount = epoll_wait(epoll_fd, pendingEvent, EPOLL_MAX_EVENTS, -1);
0 0