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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:01:00+00:00 2026-05-14T23:01:00+00:00

Update: fixed delta calculations in code, still the issue remains Folks, could you please

  • 0

Update: fixed delta calculations in code, still the issue remains

Folks, could you please explain why I’m getting very strange results from time to time using the following code:

#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>

int main()
{
  struct timeval start, end;
  long mtime1, mtime2, diff;

  while(1)
  {
    gettimeofday(&start, NULL);

    usleep(2000);

    gettimeofday(&end, NULL);

    mtime1 = (start.tv_sec * 1000 + start.tv_usec/1000.0);
    mtime2 = (end.tv_sec * 1000 + end.tv_usec/1000.0);

    diff = mtime2 - mtime1;
    if(diff > 10) 
        printf("WTF: %ld\n", diff);
  }

  return 0;
}

(You can compile and run it with: gcc test.c -o out -lrt && ./out)

What I’m experiencing is sporadic big values of diff variable almost every second or even more often, e.g:

$ gcc test.c -o out -lrt && ./out 
WTF: 14
WTF: 11
WTF: 11
WTF: 11
WTF: 14
WTF: 13
WTF: 13
WTF: 11
WTF: 16

How can this be possible? Is it OS to blame? Does it do too much context switching? But my box is idle(
load average: 0.02, 0.02, 0.3).

Here is my Linux kernel version:

$ uname -a
Linux kurluka 2.6.31-21-generic #59-Ubuntu SMP Wed Mar 24 07:28:56 UTC 2010 i686 GNU/Linux
  • 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-14T23:01:00+00:00Added an answer on May 14, 2026 at 11:01 pm

    The sleep functions only make sure that you sleep AT LEAST a certain amount of time. As Linux is not a real time OS, you CAN NOT be sure that it will sleep ONLY the amount of time you want. It is a problem as you CAN NOT count on that value. As you have pointed out, it happens that the sleep time is really BIG.

    The Linux scheduler can not guarantee that. With a real time OS you can get that.

    Your formula is wrong in a way, but I think that can not be the reason why you have such big sleep time. I check the two formulae with this snippet, and I have the same result :

    #include <unistd.h>
    #include <sys/time.h>
    #include <time.h>
    #include <stdio.h>
    
    int main()
    {
      struct timeval start, end;
      long mtime, mtime2, start_time, end_time, seconds, useconds;
    
      while(1)
      {
        gettimeofday(&start, NULL);
    
        usleep(2000);
    
        gettimeofday(&end, NULL);
    
        seconds  = end.tv_sec  - start.tv_sec;
        useconds = end.tv_usec - start.tv_usec;
    
        mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
    
        start_time = ((start.tv_sec) * 1000 + start.tv_usec/1000.0) + 0.5;
        end_time = ((end.tv_sec) * 1000 + end.tv_usec/1000.0) + 0.5;
    
        mtime2 = end_time - start_time;
    
        if(mtime > 10 || mtime2 > 10)
        {
          printf("WTF: %ld\n", mtime);
          printf("WTF2: %ld\n", mtime2);
        }
      }
    
      return 0;
    }
    

    The results :

    $ gcc test.c -o out -lrt && ./out
    WTF: 11
    WTF2: 12
    WTF: 21
    WTF2: 21
    

    I thought it wrong as the useconds part is cyclic and could lead to a big negative difference. But it will not lead to such big time as you use signed long integers …

    my2cents

    Edit : from man nanosleep :

    The current implementation of
    nanosleep() is based on the normal
    kernel timer mechanism, which has a
    resolution of 1/HZ s (see time(7)).
    Therefore, nanosleep() pauses always
    for at least the specified time,
    however it can take up to 10 ms longer
    than specified until the process
    becomes runnable again. For the same
    reason, the value returned in case of
    a delivered signal in *rem is usually
    rounded to the next larger multiple of
    1/HZ s.

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

Sidebar

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.