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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:26:08+00:00 2026-06-15T22:26:08+00:00

Could somebody please explain how to make a countdown timer using clock_gettime, under Linux.

  • 0

Could somebody please explain how to make a countdown timer using clock_gettime, under Linux. I know you can use the clock() function to get cpu time, and multiply it by CLOCKS_PER_SEC to get actual time, but I’m told the clock() function is not well suited for this.

So far I have attempted this (a billion is to pause for one second)

#include <stdio.h>
#include <time.h>

#define BILLION 1000000000

int main()
{
 struct timespec rawtime;
 clock_gettime(CLOCK_MONOTONIC_RAW, &rawtime);
 unsigned long int current =  ( rawtime.tv_sec + rawtime.tv_nsec );
 unsigned long int end =  (( rawtime.tv_sec + rawtime.tv_nsec ) + BILLION );
 while ( current < end )
 {
  clock_gettime(CLOCK_MONOTONIC_RAW, &rawtime);
  current =  ( rawtime.tv_sec + rawtime.tv_nsec );
 }

 return 0;
}

I know this wouldn’t be very useful on its own, but once I’ve found out how to time correctly I can use this in my projects. I know that sleep() can be used for this purpose, but I want to code the timer myself so that I can better integrate it in my projects – such as the possibility of it returning the time left, as opposed to pausing the whole program.

  • 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-15T22:26:10+00:00Added an answer on June 15, 2026 at 10:26 pm

    Please, do not do that. You’re burning CPU power for nothing in a busy loop.

    Why not use the nanosleep() function instead? It’s perfectly suited to the use case you outlined. Or, if you want an easier interface, perhaps something like

    #define _POSIX_C_SOURCE 200809L
    #include <time.h>
    #include <errno.h>
    
    /* Sleep for the specified number of seconds,
     * and return the time left over.
    */
    double dsleep(const double seconds)
    {
        struct timespec  req, rem;
    
        /* No sleep? */
        if (seconds <= 0.0)
            return 0.0;
    
        /* Convert to seconds and nanoseconds. */
        req.tv_sec = (time_t)seconds;
        req.tv_nsec = (long)((seconds - (double)req.tv_sec) * 1000000000.0);
    
        /* Take care of any rounding errors. */
        if (req.tv_nsec < 0L)
            req.tv_nsec = 0L;
        else
        if (req.tv_nsec > 999999999L)
            req.tv_nsec = 999999999L;
    
        /* Do the nanosleep. */
        if (nanosleep(&req, &rem) != -1)
            return 0.0;
    
        /* Error? */
        if (errno != EINTR)
            return 0.0;
    
        /* Return remainder. */
        return (double)rem.tv_sec + (double)rem.tv_nsec / 1000000000.0;
    }
    

    The difference is that using this one the CPU is free to do something else, rather than spin like a crazed squirrel on speed.

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

Sidebar

Related Questions

Can somebody please explain me clearly by using some nice examples how c++ class
Could somebody please explain what this notation is in javascript? What is function(d) doing?
Could somebody please explain or link to a resource that will tell me why
Could somebody please explain the meaning of this error? INFO: Scanning for root resource
Could somebody please explain why the variable named foo remains true in the code
Could somebody please explain why I'm getting a compile error here - error C2558:
Could somebody please explain to me what happens here? I am creating a binding
Could somebody please explain this piece of Ruby code: def add_spec_path_to(args) # :nodoc: args
Could somebody please explain to me why does this code only print 42 instead
Could somebody please explain to me why I am receiving the below error, I

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.