Reference: http://www.man7.org/linux/man-pages/man2/eventfd.2.html
First, Introduction
Simply put, the function is to create a file descriptor for the event notification. It is similar to a pipe, but does not require two descriptors like a pipe, it requires only a description to enable interprocess communication.
For a detailed introduction, see Resources.
Second, use
Example:
#include <sys/eventfd.h>#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<stdint.h>/*Definition of uint64_t*/#defineHandle_error (msg) Do{perror (msg); exit (exit_failure);} while(0)intMainintargcChar**argv) { intEFD, J; uint64_t u; ssize_t s; if(ARGC <2) {fprintf (stderr,"Usage:%s <num>...\n", argv[0]); Exit (Exit_failure); } EFD= EVENTFD (0,0); if(EFD = =-1) {Handle_error ("EVENTFD"); } Switch(fork ()) { Case 0: for(j =1; J < argc; J + +) {printf ("Child writing%s to efd\n", Argv[j]); U= Strtoull (Argv[j], NULL,0); S= Write (EFD, &u,sizeof(uint64_t)); if(s! =sizeof(uint64_t)) {Handle_error ("Write"); }} printf ("Child completed write loop\n"); Exit (exit_success); Case-1: Handle_error ("Fork"); default: Sleep (2); printf ("Parent About to read\n"); S= Read (EFD, &u,sizeof(uint64_t)); if(s! =sizeof(uint64_t)) {Handle_error ("Read"); } printf ("Parent Read%llu (0X%LLX) from efd\n", (unsignedLong Long) u, (unsignedLong Long) u); Exit (exit_success); }}
Execution Result:
Third, the question
Why write the number of this file descriptor, read to incredibly is their and?
See, take a closer look at the read, write operation on the file descriptor, read reads an integer of type uini64_t from the file descriptor, and write adds the number to be written to an existing integer.
Linux EVENTFD ()