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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-29-2012, 11:19 PM
|
#1
|
|
Member
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 190
Rep:
|
Can someone give me the example of the simplest usage of nanosleep in C?
Can someone give me the example of the simplest usage of nanosleep in C?
Please explain in the comment '/* */'.
I would want the command-line to sleep for 0.1 second.
Any answers welcome.
|
|
|
|
09-29-2012, 11:55 PM
|
#2
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,047
|
Code:
/* wait 0 sec and 10^8 nanosec */
struct timespec ts = {0, 100000000L };
nanosleep (&ts, NULL};
|
|
|
|
09-29-2012, 11:58 PM
|
#3
|
|
Member
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 190
Original Poster
Rep:
|
but, do i actually need "struct"? I thought I can specify the nanoseconds in the nanosleep();...
|
|
|
|
09-30-2012, 02:23 AM
|
#4
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,623
|
Well, if it doesn't need to be very precise, you could use usleep instead, and then you don't need to use the struct. I don't see a problem with using the struct tho.
http://linux.die.net/man/2/nanosleep
|
|
|
1 members found this post helpful.
|
09-30-2012, 05:05 AM
|
#5
|
|
Member
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 190
Original Poster
Rep:
|
ok, then, give me the simplest example of usleep, plz?
|
|
|
|
09-30-2012, 05:53 AM
|
#6
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,623
|
Code:
#include <unistd.h>
int main (void)
{
usleep(100000);
return 0;
}
it is in microseconds so 100000 = 0.1 sec.
|
|
|
1 members found this post helpful.
|
09-30-2012, 06:26 AM
|
#7
|
|
Member
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 190
Original Poster
Rep:
|
Quote:
Originally Posted by H_TeXMeX_H
Code:
#include <unistd.h>
int main (void)
{
usleep(100000);
return 0;
}
it is in microseconds so 100000 = 0.1 sec.
|
Ha ha! Thanks a lot, you've just saved my life! 
From wikipedia: 10−6 s µs microsecond
|
|
|
|
09-30-2012, 12:20 PM
|
#8
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,047
|
Off: Please don't worry, suttiwit, I won't take offence if you say thank you for answering your question.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:33 PM.
|
|
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
|
|