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 5847143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:40:37+00:00 2026-05-22T12:40:37+00:00

I have created a timer which can expire, in 5 seconds using timerfd_create ,

  • 0

I have created a timer which can expire, in 5 seconds using timerfd_create, but i can see that it is waiting indefinitely.

Can someone help me?

Thanks in advance.

Here is my code:

enter code here
#include <sys/timerfd.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <time.h>

int main()
{
  struct itimerspec its;
  struct epoll_event event, revent;

  int timer_fd, efd;

  /* Setting timer interval */
  its.it_interval.tv_sec=1;
  its.it_interval.tv_nsec=0;

  /* Setting timer expiration */
  its.it_value.tv_sec=5;
  its.it_value.tv_nsec=0;

  efd=epoll_create(2);
  event.data.fd=timer_fd;

  event.events=EPOLLIN;
  epoll_ctl(efd, EPOLL_CTL_ADD, timer_fd, &event);

  timer_fd=timerfd_create(CLOCK_REALTIME, 0);

  if(timer_fd==-1)
  {
    perror("timerfd:");
  }

  if(timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL)==-1)
  {
   perror("timerfd_settime error:");
  }

  printf("Starting the timer...");

  while(1) {
     epoll_wait(efd, &revent, 1, -1);
  }
}
  • 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-05-22T12:40:38+00:00Added an answer on May 22, 2026 at 12:40 pm

    Reverse the order of calls to epoll_ctl and timerfd_create. Right now you are adding some random integer value to the event set.

    Edit 0:

    Several points:

    • timerfd_create(2) produces a file descriptor, just like open(2) or socket(2). You have to assign the return value to the timer_fd variable before giving it to the epoll_ctl(2), otherwise it’s just a random integer value from the stack.
    • Don’t use TFD_TIMER_ABSTIME – you are asking the kernel to start a timer that expires one second after the Epoch (which is not that big of a deal – it’ll just expire immediately).
    • When the timer expires epoll_wait(2) returns the number of ready file descriptors, 1 in your example, and you are expected to handle that. You, on the other hand, just ignore that return value and spin around in a tight loop, so you don’t even know the timer is expiring.
    • You need to read from timer file descriptor to consume the expiration event. Otherwise all subsequent calls to epoll_wait(2) will just return immediately since the descriptor remains in the “signaled” state.
    • Always check the return values of the system calls and handle error conditions based on the value of errno(3) – manual page for each call gives you possible error values.

    Edit 1:

    You do want a loop around the epoll_wait(2) (or select(2). or poll(2)), but you need:

    1. handle the IO events being signaled (that’s the whole point of these multiplexing APIs – being able to wait on multiple descriptors and dispatch the events), and
    2. be able to break out of that loop (on a signal, on input from a dedicated file descriptor ala self-pipe trick, or on some application event).

    Hope this helps.

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

Sidebar

Related Questions

Our users have created MS-Excel spreadsheets which over time have evolved into fairly complex
I have been learning C++ for three months now and in that time created
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a PHP-script to update a web server that is live inside
I have created a form in C#.net which ask for key and check validity
Hey guys. I have a model, Item , which is set to expire in
Well, I have created a python script, which checks the number of uncompleted tasks
I am creating timed online test in C# asp.net. I have created it using
I have created an application which requires the reloading of an image several times
I have created a derived button class which contains a couple ImageSource members for

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.