LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 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: 23
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: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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

Original Poster
Blog Entries: 2

Rep: Reputation: 23
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
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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

Original Poster
Blog Entries: 2

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

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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

Original Poster
Blog Entries: 2

Rep: Reputation: 23
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: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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



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

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

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