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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:30:37+00:00 2026-06-11T14:30:37+00:00

Need to call a function every X (let’s say 5) seconds and the below

  • 0

Need to call a function every X (let’s say 5) seconds and the below code does it.

But it is blocking the execution of code. As I want it to work like setitimer(), where I can (for example) call a function every 5 sec and do something else.

   #include <sys/timerfd.h>
   #include <time.h>
   #include <unistd.h>
   #include <stdlib.h>
   #include <stdio.h>
   #include <stdint.h>        /* Definition of uint64_t */

   #define handle_error(msg) \
           do { perror(msg); exit(EXIT_FAILURE); } while (0)

   int
   main(int argc, char *argv[])
   {
       struct itimerspec new_value;
       int max_exp, fd;
       struct timespec now;
       uint64_t exp, tot_exp;
       ssize_t s;

       if (clock_gettime(CLOCK_REALTIME, &now) == -1)
           handle_error("clock_gettime");

       /* Create a CLOCK_REALTIME absolute timer with initial
          expiration and interval as specified in command line */

       new_value.it_value.tv_sec = now.tv_sec + 1; 
       new_value.it_value.tv_nsec = now.tv_nsec;

       new_value.it_interval.tv_sec = 5;
       new_value.it_interval.tv_nsec = 0;
       max_exp = 5; //say 5 times

       fd = timerfd_create(CLOCK_REALTIME, 0);
       if (fd == -1)
           handle_error("timerfd_create");

       if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)
           handle_error("timerfd_settime");

       printf("timer started\n");
       for (tot_exp = 0; tot_exp < max_exp;) {
           s = read(fd, &exp, sizeof(uint64_t));
           if (s != sizeof(uint64_t))
               handle_error("read");

           tot_exp += exp;
           printf("read: %llu; total=%llu\n",
                   (unsigned long long) exp,
                   (unsigned long long) tot_exp);
       }
    //Do something else ?
    //while(1);
       exit(EXIT_SUCCESS);
   }

EDIT
I have one more question.
On changing these lines in above code from

   new_value.it_interval.tv_sec = 5;
   new_value.it_interval.tv_nsec = 0;

to

   new_value.it_interval.tv_sec = 0;
   new_value.it_interval.tv_nsec = 5000000000;

I see that there is no 5 seconds delay. Whats happening here?

  • 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-11T14:30:38+00:00Added an answer on June 11, 2026 at 2:30 pm

    You need to understand how to use multiplexing syscalls like poll(2) (or the older select(2) which tends to become obsolete) and use them to test the readability of the file descriptor obtained by timerfd_create(2) before read(2)-ing it.

    However, be aware that timerfd_create works only when that read call succeeded. So only when the poll says you that the fd is not readable can you do something else. That something else should be quick (last less than 5 seconds).

    You might want to investigate event loop libraries, like e.g. libevent (wrapping poll). If you are coding a graphical application (using Qt or Gtk) it does already have its own event loop. If clever enough, you could do your 5-second period without any timerfd_create, just thru your event loop (by carefully setting the timeout given to poll, etc.).

    Addenda:

    the tv_nsec field should always be non-negative and less than 1000000000 (the number of nanoseconds in a second).

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

Sidebar

Related Questions

I need to call a function every 5 minutes for an 8 hour period.
I've created a stock ticker function and need to call it every 2 minutes.
In my C# code, I need to call a function from a C++ Dll
Let us say I have a void function which just have NSLog(@Call me); in
I need a simple way to call a function every 60 minutes. How can
In PrimeFace's inputText , I need to call function ‍‍‍ f1() when any key
I need to call a function in an unmanaged .dll written in C lang
I need to call a function by clicking a button. The button is on
I need to call a function whose name is available in a string in
I need to call a function in the following fashion: var f = 'fadeOut';

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.