Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8921389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:31:52+00:00 2026-06-15T06:31:52+00:00

I want to use eventfd as a way to signal simple events between kernelspace

  • 0

I want to use eventfd as a way to signal simple events between kernelspace and userspace. eventfd will be used as a way to signal and the actual data will be transferred using ioctl.
Before going ahead with implementing this, I wrote a simple program to see how eventfd behaves with select(). It seems that if you use select to wait on an eventfd, it wont return when u write to it in a separate thread. In the code I wrote, the writing thread waits for 5 seconds beginning from program start before writing to the eventfd twice. I would expect the select() to return in the reading thread immediately following this write but this does not happen. The select() returns only after the timeout of 10 seconds and returns zero. Regardless of this return zero, when I try to read the eventfd after 10 seconds, I get the correct value.

I use Ubuntu 12.04.1 (3.2.0-29-generic-pae) i386

Any idea why this is so? It seems to me that select() is not working as it should.

PS: This question is similar to linux – Can't get eventfd to work with epoll together

Is anyone else facing similar issues?

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>     //Definition of uint64_t

#include <pthread.h>    //One thread writes to fd, other waits on it and then reads it
#include <time.h>       //Writing thread uses delay before writing
#include <sys/eventfd.h>

int efd; //Event file descriptor

void * writing_thread_func() {
    uint64_t eftd_ctr = 34;
    ssize_t s;

    printf("\n%s: now running...",__func__);

    printf("\n%s: now sleeping for 5 seconds...",__func__);
    fflush(stdout); //must call fflush before sleeping to ensure previous printf() is executed
    sleep(5);

    printf("\n%s: Writing %lld to eventfd...",__func__,eftd_ctr);
    s = write(efd, &eftd_ctr, sizeof(uint64_t));
    if (s != sizeof(uint64_t)) {
        printf("\n%s: eventfd writing error. Exiting...",__func__);
        exit(EXIT_FAILURE);
    }

    eftd_ctr = 99;

    printf("\n%s: Writing %lld to eventfd...",__func__,eftd_ctr);
    s = write(efd, &eftd_ctr, sizeof(uint64_t));
    if (s != sizeof(uint64_t)) {
        printf("\n%s: eventfd writing error. Exiting...",__func__);
        exit(EXIT_FAILURE);
    }

    printf("\n%s: thread exiting...",__func__);
    pthread_exit(0);
}

void * reading_thread_func() {
    ssize_t s;
    uint64_t eftd_ctr;

    int retval;         //for select()
    fd_set rfds;        //for select()
    struct timeval tv;  //for select()

    printf("\n%s: now running...",__func__);

    printf("\n%s: now waiting on select()...",__func__);
    //Watch efd
    FD_ZERO(&rfds);
    FD_SET(efd, &rfds);

    //Wait up to 10 seconds
    tv.tv_sec = 10;
    tv.tv_usec = 0;

    retval = select(1, &rfds, NULL, NULL, &tv);

    if (retval == -1){
        printf("\n%s: select() error. Exiting...",__func__);
        exit(EXIT_FAILURE);
    } else if (retval > 0) {
        printf("\n%s: select() says data is available now. Exiting...",__func__);
        printf("\n%s: returned from select(), now executing read()...",__func__);
        s = read(efd, &eftd_ctr, sizeof(uint64_t));
        if (s != sizeof(uint64_t)){
            printf("\n%s: eventfd read error. Exiting...",__func__);
            exit(EXIT_FAILURE);
        }
        printf("\n%s: Returned from read(), value read = %lld",__func__, eftd_ctr);
    } else if (retval == 0) {
        printf("\n%s: select() says that no data was available even after 10 seconds...",__func__);
        printf("\n%s: but lets try reading efd count anyway...",__func__);
        s = read(efd, &eftd_ctr, sizeof(uint64_t));
        if (s != sizeof(uint64_t)){
            printf("\n%s: eventfd read error. Exiting...",__func__);
            exit(EXIT_FAILURE);
        }
        printf("\n%s: Returned from read(), value read = %lld",__func__, eftd_ctr);
        exit(EXIT_FAILURE);
    }

    printf("\n%s: thread exiting...",__func__);
    pthread_exit(0);
}

int main() {
    pthread_t writing_thread_var, reading_thread_var;

    //Create eventfd
    efd = eventfd(0,0);
    if (efd == -1){
        printf("\n%s: Unable to create eventfd! Exiting...",__func__);
        exit(EXIT_FAILURE);
    }

    printf("\n%s: eventfd created. value = %d. Spawning threads...",__func__,efd);

    //Create threads
    pthread_create(&writing_thread_var, NULL, writing_thread_func, NULL);
    pthread_create(&reading_thread_var, NULL, reading_thread_func, NULL);

    //Wait for threads to terminate
    pthread_join(writing_thread_var, NULL);
    pthread_join(reading_thread_var, NULL);

    printf("\n%s: closing eventfd. Exiting...",__func__);
    close(efd);
    exit(EXIT_SUCCESS);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:31:54+00:00Added an answer on June 15, 2026 at 6:31 am

    So it was a silly mistake:

    I changed:

    retval = select(1, &rfds, NULL, NULL, &tv);
    

    to:

    retval = select(efd+1, &rfds, NULL, NULL, &tv);
    

    and it worked.

    Thanks again @Steve-o

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't want use ajax to load data in my grid. Theres a way
i want use some data from a website with web service. i have a
I want use php curl with oauth to get the JSON data from twitter
I want use regex to find something in string (or QString) that is between
I use google-java-client-library v3. Know I want to create events by code. This is
I want use this 1 for using Bar code or QR code scanner. I
I want use BYTE_ORDER macro in my Xcode project but i can't because i
I want use javascript setInterval function to achieve a box rotate animate effect, I
I want use a single php file to handle all of my voting requests.
I want use groovy findAll with my param to filtering closure filterClosure = {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.