#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2

    Wink Simple c code for updating values evry 10 min


    Dear all.

    i just wanted know how write simple c code to update data every 10 min.

    assume i have real time clock.
    currently my time is 10:10:10

    current time=previous time=10
    after 10 min
    current time =20
    now i wanna update previous time to20 and current time keep incrementing
    between 10 min we can print hello. when updates i can print welcome
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    7
    Code:
    #include <stdio.h>
    #include <time.h>
    
    const time_t wait_time = 10 * 60; // 10 min * 60 s
    time_t previous_time, current_time;
    previous_time = current_time = time(NULL);
    do
    {
        current_time = time(NULL);
    } while (current_time < previous_time + wait_time);
    puts("Hello");
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,073
    Rep Power
    1802
    Some caution is required. Using the standard library's meagre time services you have to resort to polling the time as in @DRK82's solution. Such "busy-wait" loops will consume as much CPU time as the OS will allow it and may adversely affect other processes and the general responsiveness of your system.

    A better approach is to use timing services provided by the OS so that during the waiting period your processor is free to do other tasks. For example in Windows:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {
        const DWORD wait_time_ms = 10 * 60 * 1000 ;
        Sleep(  wait_time_ms ) ;
        puts("Hello");
        return 0 ;
    }
    And in POSIX API OS's

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
        const unsigned wait_time_sec = 10 * 60 ;
        sleep(  wait_time_sec ) ;
        puts("Hello");
        return 0 ;
    }
    Other more sophisticated techniques are available in most operating systems too.
    Last edited by clifford; February 11th, 2013 at 01:36 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    Originally Posted by clifford
    Some caution is required. Using the standard library's meagre time services you have to resort to polling the time as in @DRK82's solution. Such "busy-wait" loops will consume as much CPU time as the OS will allow it and may adversely affect other processes and the general responsiveness of your system.

    A better approach is to use timing services provided by the OS so that during the waiting period your processor is free to do other tasks. For example in Windows:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {
        const DWORD wait_time_ms = 10 * 60 * 1000 ;
        Sleep(  wait_time_ms ) ;
        puts("Hello");
        return 0 ;
    }
    And in POSIX API OS's

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
        const unsigned wait_time_sec = 10 * 60 ;
        sleep(  wait_time_sec ) ;
        puts("Hello");
        return 0 ;
    }
    Other more sophisticated techniques are available in most operating systems too.
    is there a way to get time from the real time clock on the motherboard? and update using that? (or were the methods you posted already using that ?)
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,073
    Rep Power
    1802
    Originally Posted by zedeneye1
    is there a way to get time from the real time clock on the motherboard? and update using that? (or were the methods you posted already using that ?)
    Firstly no need to quite my entire post!

    To output at 10 minute periods you don't need a clock time just a period measurement. If you want an "alarm clock", you could for example, sleep for 1 second, check the real time using the standard time.h functions, then go back to sleep. That would be less wasteful of CPU time.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    Originally Posted by clifford
    Firstly no need to quite my entire post!

    To output at 10 minute periods you don't need a clock time just a period measurement. If you want an "alarm clock", you could for example, sleep for 1 second, check the real time using the standard time.h functions, then go back to sleep. That would be less wasteful of CPU time.
    sorry I didn't understand. How does the sleep function work?

    I've only known of one way to make a delay which is to use a while loop. the real time clock on the mother board my guess is works on a similar principle, a timer that keeps looping, has a fixed frequency and adds a second to the time after one second worth of counts, power using a cell so it doesn't loose time when u plug ur pc off...
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,073
    Rep Power
    1802
    Originally Posted by zedeneye1
    sorry I didn't understand. How does the sleep function work?
    Sleeping a process uses the operating systems scheduler switch completely stop a process for a period of time. The time can then be used by other processes. If a process "busy-waits" in a loop it will demand CPU cycles to do essentially nothing. Sleep behaviour prevents your process from adversely affecting the performance of the system as a whole.

    Process scheduling is OS specific, read the respectice documentation:

    Sleep()
    sleep()

    Originally Posted by zedeneye1
    the real time clock on the mother board my guess is works on a similar principle, a timer that keeps looping,
    Not really, an RTC is an entirely hardware device using sequential logic - there is no "looping", that is a software concept. The core of such a chip is a crystal oscillator driving a binary counter.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    Originally Posted by clifford
    Sleeping a process uses the operating systems scheduler switch completely stop a process for a period of time. The time can then be used by other processes. If a process "busy-waits" in a loop it will demand CPU cycles to do essentially nothing. Sleep behaviour prevents your process from adversely affecting the performance of the system as a whole.

    Process scheduling is OS specific, read the respectice documentation:

    Sleep()
    sleep()

    Not really, an RTC is an entirely hardware device using sequential logic - there is no "looping", that is a software concept. The core of such a chip is a crystal oscillator driving a binary counter.
    I've got it now...

    so sleep() does use the RTC then?
  16. #9
  17. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,073
    Rep Power
    1802
    Originally Posted by zedeneye1
    I've got it now...

    so sleep() does use the RTC then?
    No, I very much doubt it. It will generally use the system tick interrupt derived from the processor's PLL oscillator. The point is that you don't need to know how it does it, just what it does.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    Originally Posted by clifford
    No, I very much doubt it. It will generally use the system tick interrupt derived from the processor's PLL oscillator. The point is that you don't need to know how it does it, just what it does.
    well I was just curious on how the computer manages to keep time..

IMN logo majestic logo threadwatch logo seochat tools logo