2

I want to create a timer to run on an embedded system with as little impact as possible. I can choose to implement the timer in C, C++, bash or python. The error in the timer can be +/- a few seconds.

I'm tempted just to create a loop in a bash script using the 'sleep()' command and the '&' to start it in the background. However I'm worried/don't quite understand if this will be keeping the system active/awake if implemented this way.

If it is keeping the system active/awake, is it possible to create a timer that doesn't do that using C, C++, python or maybe another way with a bash script?

icedTea
  • 495
  • 2
  • 7
  • 12
  • What kind of resolution do you need? – jedwards Jul 03 '12 at 19:18
  • 2
    "I take most uses of the compound C/C++ as an indication of ignorance. " ~ Bjarne Stroustrup - See also http://www.research.att.com/~bs/bs_faq.html#C-slash – Griwes Jul 03 '12 at 19:18
  • If your embedded system runs python, surely the OS has timer functionality built-in? – Alex Wilson Jul 03 '12 at 19:23
  • 3
    @Griwes you dont shout to everyone how ignorant they are even if you did find some FAQ website, right? – Ulterior Jul 03 '12 at 19:35
  • @Ulterior, questions marked both [tag:c] and [tag:c++] are plague here, on SO... – Griwes Jul 03 '12 at 19:37
  • 1
    It seems that your embedded system runs linux (or similar POSIX-compatible system). Does it run a cron task? – Robᵩ Jul 03 '12 at 19:41
  • 1
    [`fcron`](http://fcron.free.fr/) should do what you want. – jxh Jul 03 '12 at 19:50
  • 1
    What is the goal of the timer? – sfstewman Jul 03 '12 at 21:04
  • I want the timer to allow me to read some data from a file on a consistent interval. This file will contain data on average current and CPU states. I want the timer to allow me to do this while affecting this data as minimally as possible. Unfortunately, cron is not available. C/C++ debate isn't relevant to the question, it's corrected now. – icedTea Jul 04 '12 at 13:30

2 Answers2

1

You can do timers in a number of different ways.

The simplest is just something like

date "+%s"

Which prints out the timestamp of the system. If the system clock is adequate, then getting two timestamps, one at the start and the second at the end, you can find the elapsed time by finding the difference in these two timestamps.

There are equivalent ways to do this on every language, C, C++, Python etc.

If you're timing a process that ends, you could do something like

TIC=$(date "+%s")
./some-script
TOC=$(date "+%s")
DELTA=$((TOC - TIC))
echo $DELTA

Or alternatively in Python.

In any case, I don't think your sleep idea is the best approach.

Community
  • 1
  • 1
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • I think you misunderstand his question. He doesn't want to *measure* time, he wants to have a task run periodically. – Robᵩ Jul 03 '12 at 19:41
  • If that is the case, its not very clear and then my answer wouldn't apply. – jedwards Jul 03 '12 at 22:04
  • Thanks, this gave me some ideas but Rob is correct. I don't want to measure time but to have a task run periodically. – icedTea Jul 04 '12 at 13:32
1

If you are worried about system resource or power consumption issue in an embedded system, you should definitely use RTC to implement timer.

Please refer to http://linux.die.net/man/4/rtc

And, check the user manual of the operating system you are using. Maybe there are some functions existing to call.

It is not necessary to reinvent the timer related functions.

Frank He
  • 536
  • 3
  • 9
  • RTC is limited to 8192Hz, and anything above 60Hz requires running as root. I've found that using [setitimer](http://pubs.opengroup.org/onlinepubs/009695399/functions/getitimer.html) to generate alarm signals at regular intervals is more portable, offers the possibility of microsecond granularity (depending on the implementation), and doesn't require any special privileges. Then you can simply call the pause() system call in a loop to sleep between alarm signals. Python even includes support for this method using the [signal module](http://docs.python.org/library/signal.html). – James O'Doherty Aug 12 '12 at 07:48