How to use Linux new Api--eventfd __linux

Source: Internet
Author: User
The EVENTFD is in the kernel version and is 2.6.22 effective later. To view the kernel version you can use the command uname-r.
[CPP]View Plain copy print? #include <sys/eventfd.h> int eventfd (unsigned int initval,int flags);

This function creates an event object (Eventfd object) to implement the Wait/notify (wait/notify) mechanism between processes (threads). The kernel maintains a 64-bit counter (uint64_t) for this object.
And initializes the counter with the first argument (Initval). Calling this function returns a new file descriptor (event object). The 2.6.27 version begins with a bitwise setting of the second parameter (flags).
Some of the following macros can be used:

Efd_nonblock, function with open (2) O_nonblock, set the object is non-blocking state, if not set this state, read (2) reads Eventfd, and the counter value of 0 has been blocked in the read call, if set this flag, A eagain error (errno = Eagain) is returned. The effect is also the same as an extra call to select (2).

Efd_cloexec my understanding is that if this identity is set, calling exec automatically closes the file descriptor to prevent leaks.

If it is a 2.6.26 or previous version of the kernel, the flags must be set to 0.

After you create this object, you can do the following.

Write adds the 8-byte reshaping value that the buffer writes to the kernel counter.

Read reads a 8-byte value and resets the counter to 0. If you call read when the counter is 0, if the EVENTFD is blocked, read blocks here, otherwise you get a eagain error.
If the buffer length is less than 8 then read will fail with the error code set to Einval.

Poll Select Epoll

Close closes when no eventfd is needed, and the kernel releases resources when all the handles to the object are closed. Why not close and just release it if you call fork to create

The process copies the handle to the new process and inherits all the states.

(PS: That is, after write no read, but write new data, then read the two 8 bytes of the and, write after read, you can complete the interaction between read and write)

An example:

    #include <stdio.h> #include <unistd.h> #include <sys/time.h> #include <stdint.h>

    #include <pthread.h> #include <sys/eventfd.h> #include <sys/epoll.h> int efd =-1;
        void *read_thread (void *dummy) {int ret = 0;
        uint64_t count = 0;
        int ep_fd =-1;

        struct Epoll_event events[10];
            if (EFD < 0) {printf ("EFD not inited.\n");
        Goto fail;
        } EP_FD = Epoll_create (1024);
            if (EP_FD < 0) {perror ("Epoll_create fail:");
        Goto fail;

            } {struct epoll_event read_event; read_event.events = Epollhup | Epollerr |
            Epollin;

            READ_EVENT.DATA.FD = EFD;
            ret = Epoll_ctl (EP_FD, Epoll_ctl_add, EFD, &read_event);
                if (Ret < 0) {perror ("Epoll ctl failed:");
     Goto fail;       } while (1) {ret = epoll_wait (EP_FD, &events[0], 10, 5000);
                if (Ret > 0) {int i = 0;
                        for (; i < ret; i++) {if (Events[i].events & Epollhup) {
                        printf ("Epoll eventfd has epoll hup.\n");
                    Goto fail; else if (events[i].events & Epollerr) {printf ("Epol
                        L EVENTFD has epoll error.\n ");
                    Goto fail; else if (events[i].events & epollin) {int event_fd
                        = EVENTS[I].DATA.FD;
                        ret = Read (EVENT_FD, &count, sizeof (count));
                     if (Ret < 0) {perror ("read fail:");       Goto fail;

                            else {struct Timeval TV;
                            Gettimeofday (&AMP;TV, NULL); printf ("Success read from EFD, read%d bytes (%llu) at%lds%ldus\n", ret, Count, tv.tv_
                        SEC, tv.tv_usec); else if (ret = 0) {/* Ti
                Me Out */printf ("Epoll wait timed out.\n");
            Break
                else {perror ("Epoll wait Error:");
            Goto fail;
            } fail:if (ep_fd >= 0) {close (EP_FD);
        EP_FD =-1;
    return NULL;
        int main (int argc, char *argv[]) {pthread_t pid = 0;
        uint64_t count = 0;
 int ret = 0;       int i = 0;
        EFD = eventfd (0, 0);
            if (EFD < 0) {perror ("Eventfd failed.");
        Goto fail;
        ret = pthread_create (&pid, NULL, read_thread, NULL);
            if (Ret < 0) {perror ("Pthread Create:");
        Goto fail;
            for (i = 0; i < 5; i++) {count = 4;
            ret = Write (EFD, &count, sizeof (count));
                if (Ret < 0) {perror ("write event FD fail:");
            Goto fail;

                else {struct Timeval TV;
                Gettimeofday (&AMP;TV, NULL); printf ("Success write to efd, write%d bytes (%llu) at%lds%ldus\n", ret, Count, Tv.tv_sec, Tv.tv_u
            SEC);
        Sleep (1);
            } fail:if (0!= pid) {pthread_join (PID, NULL);
        PID = 0;

  }      if (EFD >= 0) {close (EFD);
        EFD =-1;
    return ret; }

    Success write to EFD, write 8 bytes (4) at 1328805612s 21939us
    success read from EFD, read 8 bytes (4) at 1328805612s 21 997us
    Success Write to EFD, write 8 bytes (4) at 1328805613s 22247us
    success read from EFD, read 8 bytes (4) at 1328 805613s 22287us
    Success Write to EFD, write 8 bytes (4) at 1328805614s 22462us
    success read from EFD, read 8 bytes ( 4 at 1328805614s 22503us
    Success Write-efd, write 8 bytes (4) at 1328805615s 22688us
    success read from EFD, Rea D 8 bytes (4) at 1328805615s 22726us
    success Write to EFD, write 8 bytes (4) at 1328805616s 22973us
    success Read fro M efd, read 8 bytes (4) at the 1328805616s 23007us
    epoll wait timed out.

At the same time he is also supporting interprocess communication, and the process is similar to this. But the interprocess communication here is not a process in two main functions, but a process generated by a process by invoking the fork function

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.