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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:12:57+00:00 2026-05-11T00:12:57+00:00

If you take a look at the clock_gettime() function, which is available in all

  • 0

If you take a look at the clock_gettime() function, which is available in all BSDs and is actually defined as part of the POSIX standard, you see that there is support for at least three types of clocks (many systems support more than these clocks, but actually the POSIX standard only demands one to be present, all others are optional):

  • CLOCK_REALTIME – POSIX demands this to be present. This is the wall time clock.

  • CLOCK_MONOTONIC – No idea what this is (and what SI seconds mean), but I understand that this clock will never jump backwards, it can only monotonically increase in value.

  • CLOCK_UPTIME – I fail to see how this is different to CLOCK_MONOTONIC (uptime also never jumps backwards), but at least I know that this clock starts at zero when the kernel boots (whereas it’s not defined what initial value CLOCK_MONOTONIC will have when the kernel boots)

Let’s ignore the other clocks for a second. CLOCK_REALTIME is not guaranteed to monotonically count upwards, right? This is the actual ‘system time’. I can alter the system time at will. I can set it 3 month into the past or 5 years into the future and each time my system syncs time using a NTP server on the net, the time might jump forward or backward.

Now we have two sleeping functions in a BSD system. sleep() and nanosleep(). I’m not sure, but I would expect sleep() to be implemented on top of nanosleep, after all I can easily emulate sleep() by using nanosleep() and only set the number of seconds in the struct timespec, keeping nanoseconds zero.

I have read at many sources, that these functions actually work by calculating the wake-up time (get current time, add sleep amount to it) and the system will then check in regular interval if the current time is later than the wake-up time and if so, it will wake up the thread again. The fact that this is only checked in intervals is the reason why the man pages say that the current sleep will sleep for at least this amount of time (shorter only if interrupted by a signal), but it may sleep longer (depending on how often the system checks if we are already past the wake-up time and depending on how long it takes before the scheduler allows this thread to run again).

This is all perfectly sane to me… but there is one question that always bugged me:

According to various sources the sleeps (at least the nanosleep) use CLOCK_REALTIME as clock internally. This means, if tell nanosleep() to sleep for 30 seconds, then change my system clock to 1 hour in the future, the thread will wake up almost immediately (1 hour in the future is way ahead of the wake-up time nanosleep() calculated). This is also perfectly okay. However what happens if I say wake up in 30 seconds and then the user finds out that his system clock is one hour ahead and sets his clock backwards by one hour? Then my thread will sleep for 1 hour and 30 seconds? As that would be rather bad.

  • 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. 2026-05-11T00:12:58+00:00Added an answer on May 11, 2026 at 12:12 am

    As far as I know, sleep functions are usually implemented more like a decrementing counter. You say ‘sleep for 10 seconds,’ that translates into ‘sleep for 1000 schedule ticks’ in the scheduler, and then every time the scheduler checks on sleeping processes it decrements the amount of time left.

    In this way, the sleep time will always be an amount of real time to sleep, as opposed to sleeping until some time in the future. The reason for this is as you’ve suspected, if we pick a time in the future we might never get there (or might get there in an unexpected amount of time). This is consistent with what you would want to use sleep for in a program. It’s not meant to do calendar-like calculations.

    You can also make a simple test, make a program sleep for 30 seconds, use the nix ‘time’ command to time how long the function runs, and after it starts change your system clock back 5 minutes and see what happens.

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

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Short Answer: changing your parameter names won't cause a ripple… May 12, 2026 at 12:51 am
  • Editorial Team
    Editorial Team added an answer We've been using LogiXml, which probably has all the reporting… May 12, 2026 at 12:51 am
  • Editorial Team
    Editorial Team added an answer This isn't something you're usually doing in batch, but it's… May 12, 2026 at 12:51 am

Related Questions

When I used to program embedded systems and early 8/16-bit PCs (6502, 68K, 8086)
We've written a plugin to the Xinha text editor to handle footnotes. You can
I started using jQuery as soon as I found out about it, it is
I've had a good look through a load of MS documentation only to find

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.