Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-13-2003, 08:06 PM
|
#1
|
LQ Newbie
Registered: Oct 2003
Posts: 3
Rep:
|
Linux C : A better way to sleep/timer?
currently i am using system("sleep 1"); to cause a delay in my loop
however, a 1 second delay is too long. I need to get down to millisecond delaying...anyone know how?
jstic04@cp.centennialcollege.ca
|
|
|
10-13-2003, 08:14 PM
|
#2
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Rep:
|
i havent tried it, but manpage for sleep says that the number could be a floating point. can u give a sleep(0.001) for a 1ms delay?
|
|
|
10-13-2003, 08:18 PM
|
#3
|
LQ Newbie
Registered: Oct 2003
Posts: 3
Original Poster
Rep:
|
h/w : ill give that a try, but strace of sleep 1 showed me that a function called nanosleep() exists, and its in the time.h api.
im readin the man page for nanosleep() now. ill let you know the details when i get back.
|
|
|
10-13-2003, 08:18 PM
|
#4
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Rep:
|
well, it works at cmd line
|
|
|
10-14-2003, 12:44 PM
|
#5
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep:
|
Re: Linux C : A better way to sleep/timer?
Quote:
Originally posted by jimmy416
currently i am using system("sleep 1"); to cause a delay in my loop
|
Better use the sleep() function.
See "man 3 sleep".
Quote:
Originally posted by jimmy416
however, a 1 second delay is too long. I need to get down to millisecond delaying...anyone know how?
|
Use the usleep() function for microseconds. See "man 3 usleep".
|
|
|
10-14-2003, 12:53 PM
|
#6
|
Member
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600
Rep:
|
what's even better than sleep and usleep??
select() of course. Selecting on no file descriptors with NULLs as sets, you can set a timer up to delay as many microseconds as you'd like. In fact, if you man select you'll find that it suggests using that as a nice delay. I don't know of any portability issues with it, but ymmv.
Aaron
|
|
|
10-14-2003, 01:15 PM
|
#7
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep:
|
Quote:
Originally posted by orgcandman
what's even better than sleep and usleep??
select() of course.
|
When you only need a delay, why use select()?
Select() is really a overly complex way to just usleep().
|
|
|
10-14-2003, 01:37 PM
|
#8
|
Member
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600
Rep:
|
yeah, but hasn't usleep been deprecated because it has unspecified interactions with sigalarm and friends? Also, usleep has some portability issues with BSD/SysV/glibc etc... whereas select() doesn't
Also, exposure to select() isn't [edit] a bad thing [/edit] in case that helps him with his project (IE: He was sleeping because some other process was writing to a file that he wanted to read from so he guessed that it was done writing after X seconds, etc...)
Aaron
|
|
|
10-14-2003, 01:58 PM
|
#9
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep:
|
Quote:
Originally posted by orgcandman
yeah, but hasn't usleep been deprecated because it has unspecified interactions with sigalarm and friends? Also, usleep has some portability issues with BSD/SysV/glibc etc... whereas select() doesn't
|
I didn't know that. And your right. The man page says:
"This function is obsolete. Use nanosleep(2) or setitimer(2) instead."
(note however that it does not recommend select() instead of usleep() )
Quote:
Originally posted by orgcandman
Also, exposure to select() isn't a bad thing in case that helps him with his project
|
True. One could learn much about programming finding out how select() works, as it is quite complex.
Quote:
Originally posted by orgcandman
(IE: He was sleeping because some other process was writing to a file that he wanted to read from so he guessed that it was done writing after X seconds, etc...)
|
Well, yes, but that is either just your guess, or you know more about his specific project than what's in this thread.
Last edited by Hko; 10-14-2003 at 02:00 PM.
|
|
|
All times are GMT -5. The time now is 03:14 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|