0

I want to implement a real time clock and timer , that prints on screen current time like this. : " HOURS:MINUTES:SECONDS "

Is it safe to use :

      While(1){
      .....Do sth
      sleep(1);
      .....Do sth
      }

and then

     seconds+=1;

For measure of one second pass?

singa1994
  • 747
  • 1
  • 7
  • 18
  • This might be a duplicate of [this](http://stackoverflow.com/questions/3673226/how-to-print-time-in-format-2009-08-10-181754-811) – Hedron Aug 09 '16 at 19:50
  • It is . But i dont know if this format will be working in RTOS like QNX . Because i dont have experince in the past with embedded systems. – singa1994 Aug 09 '16 at 20:13
  • *"Is it safe to use ..."* -- Clarify what you mean by *"safe"*. – sawdust Aug 09 '16 at 21:25
  • @sawdust I want to implement this Timer/Clock in C in an EMPTY Virtual Machine ,running a simple RTOS.Would this give me a good an accurate result? – singa1994 Aug 09 '16 at 21:35
  • No. A delay of unknown accuracy and the precision no better than the time unit you're tracking in a scheduled thread (of unknown priority) is not able to maintain any decent level of accuracy. – sawdust Aug 09 '16 at 22:09
  • Can you recommend me something so that i can have a normally accurate result? – singa1994 Aug 09 '16 at 22:21
  • See [periodic tasks in Linux](http://www.2net.co.uk/tutorial/periodic_threads) – sawdust Aug 10 '16 at 05:06
  • And build your Linux kernel with high-resolution timer support and kernel preemption. – sawdust Aug 10 '16 at 05:11

1 Answers1

1

You will have to check whether in your particular embedded system, sleep(1) will sleep the system for 1 second. In many of the embedded boards I have used, sleep takes the argument in milliseconds. So for 1 second sleep you would have to use sleep(1000).

If you are not too worried about accuracy then yes you can use this method. however, this will not be as accurate as you using a timer or an RTC. so for example if you want your system to do something when seconds reaches 30, a better way might be to setup a timer or an RTC alarm (based on what your embedded platform has) to more accurately measure out that time.

user3397008
  • 100
  • 2
  • 8
  • 1
    The Linux man page for sleep states the argument is for number of seconds. And the question is tagged `embedded-linux`. So your first paragraph (or half of your answer) raises an irrelevant point. – sawdust Aug 09 '16 at 21:29