LinuxQuestions.org
Review your favorite Linux distribution.
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 03-31-2004, 08:26 AM   #1
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Rep: Reputation: 30
C microsecond sleep


i need a function in my program that works just like sleep() only for microseconds rather than seconds.

mss_sleep(int time)
{
????????
}

i'm sure this problem has been tackled before.
any ideas?
thanks
 
Old 03-31-2004, 08:44 AM   #2
skel
Member
 
Registered: Jan 2004
Distribution: Slackware 10 ; Gentoo ; OpenBSD 3.6
Posts: 101

Rep: Reputation: 15
usleep()

look at man 3 usleep
 
Old 03-31-2004, 08:51 AM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
be careful in that usleep() and nanosleep() may always sleep for at least 1 or 10 milliseconds regardless of how long you ask to sleep. If you need less than this, the nanosleep manpage says some stuff about using sched_setscheduler to make it so you will spin instead of sleep.
 
Old 03-31-2004, 08:52 AM   #4
skel
Member
 
Registered: Jan 2004
Distribution: Slackware 10 ; Gentoo ; OpenBSD 3.6
Posts: 101

Rep: Reputation: 15
Quote:
Originally posted by aluser
be careful in that usleep() and nanosleep() may always sleep for at least 1 or 10 milliseconds regardless of how long you ask to sleep. If you need less than this, the nanosleep manpage says some stuff about using sched_setscheduler to make it so you will spin instead of sleep.
aluser:
thanks for clearing that up, i didn't know that
 
Old 03-31-2004, 10:24 AM   #5
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
i knew it had to be in a header somewhere.
thanks alot
 
Old 03-31-2004, 11:51 AM   #6
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
what about using select, is that subject to the same 1or10?
 
Old 03-31-2004, 12:02 PM   #7
skel
Member
 
Registered: Jan 2004
Distribution: Slackware 10 ; Gentoo ; OpenBSD 3.6
Posts: 101

Rep: Reputation: 15
wouldn't you have to do a bit of a workaround to use select as a sleep timer ?

i think you would need a file descriptor that is never ready for i/o and then just let select block until it times out.

but i suspect that select is prone to the same issue with the 1 to 10 millisecond sleep, but i don't know for sure
 
Old 03-31-2004, 02:38 PM   #8
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
I am 99% sure select will suffer the same problem with regards to accuracy. If the process yields processor time it may sleep for as long as 1/HZ seconds and there's not much to be done about it. The only way to wait for shorter periods is to spin.

HZ is 100 on x86 with 2.4.x and before, and 1000 on x86 with 2.6.x

To use select as a sleep, just select(1, NULL, NULL, NULL, &timeout) -- no need to create any file descriptor.
 
  


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
Sleep Samoth Linux - General 1 05-01-2005 06:20 PM
sleep TroelsSmit Linux - Newbie 10 05-22-2004 01:17 AM
C++ Microsecond Accuracy timing CamelofCamelot Programming 2 12-22-2003 01:59 PM
to sleep or not hejduk Linux - General 3 03-28-2002 03:11 PM

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

All times are GMT -5. The time now is 09:24 PM.

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