[Go] Linux new API SIGNALFD, TIMERFD, EVENTFD instructions for use

Source: Internet
Author: User
Tags epoll

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
    1. #include <sys/signalfd.h>
    2. 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
  1. #include <sys/timerfd.h>
  2. int timerfd_create (int clockid, int flags);
  3. int timerfd_settime (int FD, int flags,
  4. const struct itimerspec *new_value,
  5. struct Itimerspec *old_value);
  6. int timerfd_gettime (int fd, struct itimerspec *curr_value);
[CPP]View Plaincopy
    1. Timerfd_create: Create a timerfd; The returned FD can do the following: Read, select (poll, Epoll), close
    2. Timerfd_settime: Set the timer period, and the start interval
    3. Timerfd_gettime: Gets the expiry time.
[CPP]View Plaincopy
  1. The data structures in the function parameters are as follows:
  2. struct TIMESPEC
  3. {
  4. time_t tv_sec; / * Seconds * /
  5. long tv_nsec; / * nanoseconds * /
  6. };
  7. struct ITIMERSPEC
  8. {
  9. struct Timespec it_interval; / * Interval for periodic timer * /
  10. struct Timespec it_value; / * Initial expiration * /
  11. };
EVENTFD involves the API: [CPP]View Plaincopy
    1. #include <sys/eventfd.h>
    2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.