LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-11-2020, 02:06 PM   #1
brownlinux
LQ Newbie
 
Registered: Oct 2020
Posts: 3

Rep: Reputation: Disabled
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/
 
Old 10-11-2020, 02:55 PM   #2
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian Stable and Unstable
Posts: 1,943

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
C is C, pretty much. The OS is immaterial.
 
Old 10-11-2020, 03:56 PM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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
 
Old 10-11-2020, 04:33 PM   #4
brownlinux
LQ Newbie
 
Registered: Oct 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
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?

Last edited by brownlinux; 10-11-2020 at 04:51 PM.
 
Old 10-11-2020, 05:08 PM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by brownlinux View Post
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.

Last edited by berndbausch; 10-11-2020 at 05:34 PM.
 
Old 10-11-2020, 06:35 PM   #6
brownlinux
LQ Newbie
 
Registered: Oct 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
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?
 
Old 10-11-2020, 07:14 PM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: A tiny Arduino Zero clone, and an Arduino for cosplayers LXer Syndicated Linux News 0 09-21-2016 08:38 PM
LXer: New Arduino Srl SBC merges Arduino, WiFi, and Linux LXer Syndicated Linux News 0 05-17-2016 01:04 AM
LXer: 6 Arduino projects to play with on Arduino Day LXer Syndicated Linux News 0 03-27-2015 08:40 AM
Ubuntu 12.10 Attempted Installation of Arduino IDE with arduino-1.0.5-linux32.tar.gz mahnac72 Linux - Newbie 2 06-21-2013 04:26 PM
[SOLVED] Arduino MEGA 2560 REV3, Arduino IDE working software versions jamesf Slackware 1 05-02-2012 12:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration