LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Any similar function as Arduino's delay() in Linux C program? (https://www.linuxquestions.org/questions/linux-software-2/any-similar-function-as-arduinos-delay-in-linux-c-program-4175683502/)

brownlinux 10-11-2020 02:06 PM

Any similar function as Arduino's delay() in Linux C program?
 
Hi, I am writing C program under Linux. Is there a builtin function that allows me to use something like the delay() function in Arduino?

https://www.arduino.cc/reference/en/...ns/time/delay/

sgosnell 10-11-2020 02:55 PM

C is C, pretty much. The OS is immaterial.

berndbausch 10-11-2020 03:56 PM

There is usleep.

Since your program is not alone on a Linux system, you need to be aware that the sleep time is not that accurate:
Quote:

The sleep may be lengthened slightly by any system activity

brownlinux 10-11-2020 04:33 PM

Quote:

Originally Posted by berndbausch (Post 6174548)
There is usleep.

Since your program is not alone on a Linux system, you need to be aware that the sleep time is not that accurate:

In general, how much off will that be? Less than a second, 500ms, 10ms, 1ms? Are there ways to achieve more accurate results? For example, getting some kind of stopwatch...

Will keep pulling the time (e.g. date command on linux. not sure about C's function to access it) and checking the difference to see if x msec has passed work better?

berndbausch 10-11-2020 05:08 PM

Quote:

Originally Posted by brownlinux (Post 6174553)
In general, how much off will that be? Less than a second, 500ms, 10ms, 1ms? Are there ways to achieve more accurate results? For example, getting some kind of stopwatch...

This depends on factors like CPU speed, number of CPUs, system load etc. I don't think a general statement can be made. See also https://www.linuxquestions.org/quest...ep-4175682641/.
Quote:

Will keep pulling the time (e.g. date command on linux. not sure about C's function to access it) and checking the difference to see if x msec has passed work better?
You could use gettimeofday as in the above link. It's worth a try, but you should also explore realtime priorities to mitigate the above factors.

brownlinux 10-11-2020 06:35 PM

Quote:

Originally Posted by berndbausch (Post 6174568)
This depends on factors like CPU speed, number of CPUs, system load etc. I don't think a general statement can be made. See also https://www.linuxquestions.org/quest...ep-4175682641/.

You could use gettimeofday as in the above link. It's worth a try, but you should also explore realtime priorities to mitigate the above factors.

Thank you. What are realtime priorities and how to explore them?

berndbausch 10-11-2020 07:14 PM

Realtime priorities allow a process to gain access to the CPU as soon as it wants it, rather than waiting in a run queue.

I can't tell you how to use them, but there should be plenty of information about them on the internet.


All times are GMT -5. The time now is 09:17 AM.