Linux EVENTFD ()

Source: Internet
Author: User

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 ()

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.