Linux New API Eventfd

Source: Internet
Author: User

Transfer from http://www.cppblog.com/peija/archive/2010/10/07/128941.html

EVENTFD is available in kernel version, 2.6.22 later. Viewing the kernel version can be uname-r with the command.

1 #include <sys/eventfd.h>2int int int flags);

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

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

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

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

Once you have created this object, you can do the following.

Write adds a 8-byte integer value written to the buffer to the kernel counter.

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

Poll Select Epoll

Close can be called close when no eventfd is required, and the kernel frees resources when all the handles of the object are closed. Why not close directly release if call fork creates
The process will copy the handle to the new process and inherit all the state.


Here is an example

1#include <sys/eventfd.h>2#include <unistd.h>3#include <stdio.h>4#include <stdint.h>5#include <stdlib.h>6#include <errno.h>7 8 #defineHandle_error (msg)9      Do{perror (msg); exit (1); } while(0)Ten  One intMainintargcChar**argv) A { - uint64_t u; - ssize_t S; the     intJ; -     if(ARGC <2 ) { -fprintf (stderr,"input <num> in command argument"); -Exit1); +     } -  +     intEFD; A     if(EFD = EVENTFD (0, efd_nonblock)) = =-1 ) atHandle_error ("Eventfd failed"); -  -  -     Switch(fork ()) { -          Case 0: -              for(j =1; J < argc; J + + ) { inprintf"Child writing%s to efd\n", Argv[j]); -              toU = Strtoull (Argv[j], NULL,0);/*analogesly atoi*/ +s = Write (EFD, &u,sizeof(uint64_t));/*Append u to counter*/  -                 if(s! =sizeof(uint64_t)) theHandle_error ("Write EFD failed"); *  $             }Panax Notoginsengprintf"Child completed write loop\n"); -  theExit0); +         default: ASleep (2); the              +printf"parent About to read\n"); -s = Read (EFD, &u,sizeof(uint64_t)); $             if(s! =sizeof(uint64_t)) { $                 if(errno =Eagain) { -printf"Parent Read Value%d\n", s); -                     return 1; the                 } -Handle_error ("Parent Read failed");Wuyi             } theprintf"Parent Read%d,%llu (0X%LLX) from efd\n",  -S, (unsignedLong Long) u, (unsignedLong Long) u); WuExit0); -  About          Case-1: $Handle_error ("Fork"); -     } -     return 0; -}

This API is also useful when you want to write and hairstyle the server, AVENTFD can perfectly replace the pipe to notify (wake) Other processes (threads). Like the classic asynchronous IO Reactor/selector
Application scenario, to wake up the select call. His buffer handling is very convenient, with only 8 bytes specified.

Linux New API Eventfd (RPM)

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.