Results 1 to 7 of 7

Thread: Fractional Secs in C

  1. #1
    Member
    Join Date
    Sep 2007
    Posts
    51

    Question Fractional Secs in C

    Hey Guys,

    i've created the following program, which outputs the string 'output' once a second.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(){
    	time_t cal_tm;
    	time_t lst_tm;
    
    	cal_tm=time(NULL);
    	lst_tm=cal_tm;
    
    	while(1){
    		cal_tm=time(NULL);
    
    		if(cal_tm>lst_tm){
    			printf("output\n");
    			lst_tm=cal_tm;
    		}
    	}
    }
    my question is, i was wondering how one would time intervals of less than 1 second in C and C++? for example, if i wanted to output the string 'output' every 0.5 of a second or 0.25 of a second etc.


    thanks in advance,

    - user0182

  2. #2
    Junior Member
    Join Date
    Apr 2008
    Posts
    1
    Hey,

    I think that the easiest way for you to accomplish what you want to do would be with the Sleep() function:


    #include <stdio.h>
    #include <windows.h>

    int main(int argc, char **argv)
    {

    while(1) {
    printf("output\n");
    Sleep(500); //sleep 500 miliseconds
    }

    return 1;
    }

    Hope this helps.

    larch

  3. #3
    Member
    Join Date
    Sep 2007
    Posts
    51
    Hey larch,

    thanks, but i don't think this works. as i understand it, the sleep() function causes a program to sleep for a given number of seconds (not milliseconds).

    although, is Sleep() (i.e. with a capital 'S') an intentional difference?


    thanks,

    - user0182

  4. #4
    Member
    Join Date
    Sep 2007
    Posts
    51
    Hey,

    i think that i've found a solution to my problem, i can use the nanosleep() function. although, if anyone has any alternatives to this solution, please post a reply.

    also, this problem has gotten me thinking, about how computers 'keep time', because hypothetically if a program knew the speed of the microprocessor and there was only one process executing on the processor, then the processor could count its own cycles and thus calculate time. but, obviously, on a modern computer there are a lot more processes, than one, scheduled on the processor at a given time.

    therefore, could someone explain how do computers 'keep time' accurately?


    thanks in advance,

    - user0182

  5. #5
    Senior Member nihil's Avatar
    Join Date
    Jul 2003
    Location
    United Kingdom: Bridlington
    Posts
    17,188
    Probably stupid, but I suspect that you might still face historical constraints?

    They may well make assumptions about time delineations?

    Try looking at working in milliseconds?


  6. #6
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    There's also the usleep() function, but I think it's *nix only. If you want to make your app portable you can use libraries like pjlib that abstract OS specific stuff, so instead of usleep() etc you have pj_thread_sleep(). It also has a timer implementation where you can schedule tasks to occur, although I'm not too sure on how that works.

    If you're interested see http://www.pjsip.org/ and have a look at the documentation section, but I suspect it's probably way more than you need.

    ac

  7. #7
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    You should research the time/timer functions and libraries available to you for your os. More specifically I'd look into any timers written for games for your setup. The stuff provided by default generally isn't all that good and other people have already solved your problem for their real time applications/games.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

Similar Threads

  1. Tcp/ip
    By gore in forum Newbie Security Questions
    Replies: 11
    Last Post: December 29th, 2003, 08:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •