http://blog.csdn.net/gdutliuyun827/article/details/8460417
Three new FD added to the Linux kernel version:
signalfd:2.6.22
timerfd:2.6.25
eventfd:2.6.22
Three kinds of FD meanings:
SIGNALFD: The traditional way of processing signals is to register the signal processing function, because the signal is asynchronous, to solve the data of concurrent access, can re-enter the problem. SIGNALFD can abstract the signal into a file descriptor, which can be read when a signal occurs, so that the signal can be monitored in Select, poll, Epoll and other listening queues.
TIMERFD: Can implement the function of the timer, the timer is abstracted into a file descriptor, when the timer expires can be read to it, which can also be placed in the main loop of the listening queue.
EVENTFD: The way to implement the event notification between threads, EVENTFD buffer size is sizeof (uint64_t), to its write can increment this counter, read operation can read, and to clear 0; Eventfd can also be placed in the listening queue, When the counter is not 0 o'clock, a readable event occurs and can be read.
Three new fd can be monitored and a readable event occurs when an event is triggered.
SIGNALFD involves the API:
[CPP]View Plaincopy
- #include <sys/signalfd.h>
- int SIGNALFD (int fd, const sigset_t *mask, int flags);
Parameter FD: If 1 means to create a new one, if it is an existing one, then modify the signal associated with the SIGNALFD;
Parameter mask: signal set;
Parameter flag: Kernel version 2.6.27 support Sfd_nonblock, sfd_cloexec;
Successful return of the file descriptor, the returned FD supports the following operations: Read, select (poll, Epoll), close
TIMERFD the API involved
[CPP]View Plaincopy
- #include <sys/timerfd.h>
- int timerfd_create (int clockid, int flags);
- int timerfd_settime (int FD, int flags,
- const struct itimerspec *new_value,
- struct Itimerspec *old_value);
- int timerfd_gettime (int fd, struct itimerspec *curr_value);
[CPP]View Plaincopy
- Timerfd_create: Create a timerfd; The returned FD can do the following: Read, select (poll, Epoll), close
- Timerfd_settime: Set the timer period, and the start interval
- Timerfd_gettime: Gets the expiry time.
[CPP]View Plaincopy
- The data structures in the function parameters are as follows:
- struct TIMESPEC
- {
- time_t tv_sec; / * Seconds * /
- long tv_nsec; / * nanoseconds * /
- };
- struct ITIMERSPEC
- {
- struct Timespec it_interval; / * Interval for periodic timer * /
- struct Timespec it_value; / * Initial expiration * /
- };
EVENTFD involves the API:
[CPP]View Plaincopy
- #include <sys/eventfd.h>
- int eventfd (unsigned int initval, int flags);
Create a eventfd, which is a counter-related FD, the counter is not zero is there is a readable event occurred, read after the counter zeroing, write increment counter; The returned FD can do the following: Read, write, select (poll, Epoll), Close
This is the basic concept of three new FD, and the next few will illustrate the use of FD using an example program.
[GO] Linux new API SIGNALFD, TIMERFD, EVENTFD instructions for use