LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-15-2007, 04:43 PM   #1
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Rep: Reputation: 19
[C/C++] pause and cpu usage


Hello all.

When doing some programming I wanted check/alter some variable every 3 seconds. The thing is that CPU usage is always at 100%.

How can I avoid/bypass this CPU consumption without using the command system() to call sleep XXs?

Thanks.
 
Old 02-15-2007, 07:39 PM   #2
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Rep: Reputation: 30
you can use ulimit if you want. That limits the resources that the process can access.
 
Old 02-15-2007, 08:30 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
sleep() is also a system call. Don't count on it if you need it to come back in exactly 3 seconds.
 
Old 02-16-2007, 07:47 AM   #4
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Original Poster
Rep: Reputation: 19
vargadanis: The idea was the program to itself standby for a certain amount time without the need to use some other program.

tuxdev: The time to come back dosent need to be so precise, arround 3 secs is fine.

So a c/c++ program always works at 100% CPU usage? Isn't this an efficiency problem?

I cant make some program to keep it running in background because it takes all CPU power.
 
Old 02-16-2007, 08:16 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
There's a sleep call defined in unistd.h - calling the sleep program with system() is not a good method. How do you think that program implements the sleeping anyhow?

There's also usleep and nanosleep for more precise sleep duration requests. As tuxdev said, don't rely on it to be very accurate (e.g. timing the movement of the fuel rods into and out of the reactor).

Last edited by matthewg42; 02-16-2007 at 08:17 AM.
 
Old 02-17-2007, 07:29 AM   #6
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Original Poster
Rep: Reputation: 19
Thanks matthewg42 that is what I wanted.

Thanks all who have replied.
 
  


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
about CPU USAGE Pravab Linux - General 6 07-05-2006 07:32 AM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 08:43 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM
Controlling CPU usage & memory usage Saravana babu Linux - Software 0 02-18-2004 05:55 AM
cpu usage Aldebaran Linux - Newbie 3 10-11-2003 11:57 AM

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

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