LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-13-2003, 07:06 PM   #1
jimmy416
LQ Newbie
 
Registered: Oct 2003
Posts: 3

Rep: Reputation: 0
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
 
Old 10-13-2003, 07:14 PM   #2
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
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?
 
Old 10-13-2003, 07:18 PM   #3
jimmy416
LQ Newbie
 
Registered: Oct 2003
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 10-13-2003, 07:18 PM   #4
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
well, it works at cmd line
 
Old 10-14-2003, 11:44 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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".
 
Old 10-14-2003, 11:53 AM   #6
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
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
 
Old 10-14-2003, 12:15 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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().
 
Old 10-14-2003, 12:37 PM   #8
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
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
 
Old 10-14-2003, 12:58 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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 01:00 PM.
 
  


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
Searching for Server (BOOTP/DHCP) <sleep> <sleep> .. .. Eileen Linux - Networking 12 10-21-2005 01:14 AM
Linux first timer... (newbie) reborn Linux - Newbie 10 05-18-2005 09:41 AM
Window VS Linux : timer question chanwing Programming 13 04-05-2005 04:27 AM
Custom Timer Interrupt Handler Linux 2.4x ratwings Linux - Software 2 01-13-2005 01:51 AM
I can't compile with <linux/timer.h> Sinner6 Linux - Newbie 1 04-19-2004 10:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 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