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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:30:33+00:00 2026-06-06T19:30:33+00:00

Possible Duplicate: Linux C/C++ Timer signal handler in userspace How do i got about

  • 0

Possible Duplicate:
Linux C/C++ Timer signal handler in userspace

How do i got about creating a timer in C? I want to perform certain action once every 5 seconds and another one every 2 seconds?

How do i go about achieving this? Could anyone point out some good links or code ?

  • 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-06T19:30:34+00:00Added an answer on June 6, 2026 at 7:30 pm

    The problem is not in setting a periodic timer, but to have two timers with a different period.

    Unfortunately 2 does not divide 5, but we can have a 1s resolution timer and a counter, when the counter is a multiple of 2, or a multiple of 5, you do what you want. You register the signal handler as usual with a timer, but this handler should look like this:

    static int cnt = 0;
    
    int handler(int) {
        cnt++;
    
        if (cnt % 2 == 0) {
            action_a();
        } 
    
        if (cnt % 5 == 0) {
            action_b();
        }
    }
    

    In order to get rid of overflow issues, we can reset the counter to 0 when we reach a multiple of 2 and 5, for example by doing:

    int handler(int) {
        cnt++;
    
        if (cnt == 10)
        {
            cnt = 0;
        }
    
        if (cnt % 2 == 0) {
            action_a();
        } 
    
        if (cnt % (2*5) == 0) {
            action_b();
        }
    }
    

    Note that this technique generalizes well to more than 2 periods, but if you are out of luck, the smallest common denominator may be a very small period, so that you may get a high timer signal rate for very little use. For instance if you have a 999ms period, and a 1000ms period, you end up having a signal every ms, which is not really ideal in terms of accuracy and in terms of overhead, but for 2s and 5s this should really be acceptable to have a signal every second.

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

Sidebar

Related Questions

Possible Duplicate: High resolution timer with C++ and Linux? double hires_time_in_seconds(); I'm looking for
Possible Duplicate: Threads vs Processes in Linux To implement multi-tasks programs we could use
Possible Duplicate: Starting iPhone app development in Linux? Is there a way to use
Duplicate: Run .NET exe in linux Hello, Is it possible to make my existing
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: sem_open() error: “undefined reference to sem_open()” on linux (Ubuntu 10.10) Having issues
Possible Duplicate: In Linux, how to prevent a background process from being stopped after
Possible Duplicate: How to cross compile from Mac OS X to Linux x86? I
Possible Duplicate: C++ Timer function to provide time in nano seconds High resolution timer
Possible Duplicate: How can I create directory tree in C++/Linux? Why mkdir fails to

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.