LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 09-29-2012, 11:19 PM   #1
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192
Blog Entries: 2

Rep: Reputation: 22
Question 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.
 
Old 09-29-2012, 11:55 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,774

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Code:
/* wait 0 sec and 10^8 nanosec */

struct timespec ts = {0, 100000000L };

nanosleep (&ts, NULL};
 
Old 09-29-2012, 11:58 PM   #3
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192
Blog Entries: 2

Original Poster
Rep: Reputation: 22
but, do i actually need "struct"? I thought I can specify the nanoseconds in the nanosleep();...
 
Old 09-30-2012, 02:23 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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.
Old 09-30-2012, 05:05 AM   #5
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192
Blog Entries: 2

Original Poster
Rep: Reputation: 22
ok, then, give me the simplest example of usleep, plz?
 
Old 09-30-2012, 05:53 AM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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.
Old 09-30-2012, 06:26 AM   #7
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192
Blog Entries: 2

Original Poster
Rep: Reputation: 22
Quote:
Originally Posted by H_TeXMeX_H View Post
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
 
Old 09-30-2012, 12:20 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,774

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Off: Please don't worry, suttiwit, I won't take offence if you say thank you for answering your question.
 
  


Reply

Tags
clang, comment, date, time, usage


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Looking for a script/program/util that can give me stats of CPU usage for a user MaRiOsGR Linux - Server 9 10-20-2011 11:43 AM
Script to give the disk usage in GB for each user. Tekken Linux - Server 2 09-08-2009 07:00 AM
nanosleep() command deathonator Programming 1 04-14-2008 11:41 AM
who can give me an simplest example about ipc? iclinux Programming 1 01-16-2005 11:23 PM
about nanosleep() dummyagain Programming 4 10-01-2003 05:53 PM


All times are GMT -5. The time now is 03:01 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration