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

  • Home
  • SEARCH
  • 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 8632545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:22:04+00:00 2026-06-12T09:22:04+00:00

I was using a periodic timer and taking times between when two SIGALRM signals

  • 0

I was using a periodic timer and taking times between when two SIGALRM signals are received. what I observed was that itimer might expires a little before or little after the time I set. e.g. if I set it for 1m sec , it might expires at 0.9998msec or 1.0023msec.

Shouldn’t the timer expiration would always be greater than what is set? less time taken is what I dont understand.

here’s my code:

enter code here     
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include <time.h>

#define INTERVAL 1000


struct timespec ti[100];
int s=0;


void ex(int i)
{int d=0;
struct timespec t[100],s1,s2;
   for(d=0;d<99;d++)
    {
    s1= ti[d];
    s2= ti[d+1];
   printf("%u:%u\t%u:%u\t", s1.tv_sec, s1.tv_nsec, s2.tv_sec, s2.tv_nsec);

    if ((s2.tv_nsec- s1.tv_nsec)<0) {
        t[d].tv_sec = s2.tv_sec-s1.tv_sec-1;
        t[d].tv_nsec = 1000000000 +s2.tv_nsec -s1.tv_nsec;
    } else {
        t[d].tv_sec = s2.tv_sec-s1.tv_sec;
        t[d].tv_nsec = s2.tv_nsec-s1.tv_nsec;
    }

    printf("%u:%u\n",t[d].tv_sec,t[d].tv_nsec);  

    } 
   exit(0);
}

void alarm_wakeup (int i)
{  
   clock_gettime(CLOCK_MONOTONIC, &ti[s]); 
    s++;
    if(s==100)
    { ex(0);
    }
}


void main ()
{
  struct itimerval tout_val;
  tout_val.it_interval.tv_sec = 0;
  tout_val.it_interval.tv_usec = INTERVAL;
  tout_val.it_value.tv_sec = 0; 
  tout_val.it_value.tv_usec = INTERVAL;


  setitimer(ITIMER_REAL, &tout_val,0);

  signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */
 signal(SIGINT,ex);

  while (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-06-12T09:22:05+00:00Added an answer on June 12, 2026 at 9:22 am

    When the timer expires, the signal is raised and the timer is rescheduled.

    However, there can be a delay between the signal being raised and the signal being handled – if the process isn’t running already, it has to be rescheduled. This means that there is a potentially variable delay between the actual expiration of the timer and when the clock_gettime() call in your signal handler runs.

    If this delay before the clock_gettime() call is higher one iteration than the next, then the time between the clock_gettime() calls will be slightly less than 1ms even though there was a 1ms gap between the subsequent timer expiries.

    In diagrammatic form:

    time:                0ms...............1ms...............2ms...............3ms
    timer expiry:        X                 X                 X                 X
    signal handler runs:  S                      S            S                  S
    

    You can see that the longer delay before the second signal handler ran made the third signal appear to be “early”, even though the underlying timer was not.

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

Sidebar

Related Questions

I am forced to execute a periodic task using a timer that is invoked
I have a periodic task that needs to execute once a minute (using delayed_job).
I'm using java.util.Timer to schedule a periodic task. At one point, I'd like to
I am using the ScheduledThreadPoolExecutor to execute periodic tasks. It is essential that the
Hey, I'm using Rails 2.3.5 and using rufus scheduler to send periodic emails. This
A periodic sequence is a sequence that repeats itself after n terms, for example,
I am using Sling's scheduler to schedule periodic jobs and I'm wondering if I'm
I am using .NET remoting to retrieve periodic status updates from a Windows service
I'm investigating the differences between using log4net and System.Diagnostics.Trace for logging, and I'm curious
I am using tinymce editor to provide rich text formatting and along with that

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.