473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trick to low CPU usage?

Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running
the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's the
trick to that?

I noticed that on certain applications like those used to decode video, you
can specify how much CPU to use (high priority, low priority...), how is
that accomplished? How can I bring my applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%, however
telling it to sleep for a full second is too much. A tenth of a second
would be fine, but sleep only takes ints. I then tried using clock() and
making it pause until a certain amount of time had passed like .005 of a
second, but the cpu sticks at 100% with that method (since it's running in a
while loop testing the amount of time passed....i.e. it's not really
sleeping).

Thanks in advance for the help!
Jul 19 '05 #1
17 21872
"Eternally" <m@r.com> wrote in message
news:ko******************@twister.nyroc.rr.com
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm
running the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with
my application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's
the trick to that?

I noticed that on certain applications like those used to decode
video, you can specify how much CPU to use (high priority, low
priority...), how is that accomplished? How can I bring my
applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%,
however telling it to sleep for a full second is too much. A tenth
of a second would be fine, but sleep only takes ints. I then tried
using clock() and making it pause until a certain amount of time had
passed like .005 of a second, but the cpu sticks at 100% with that
method (since it's running in a while loop testing the amount of time
passed....i.e. it's not really sleeping).

Thanks in advance for the help!

This is platform specific and is better asked in newsgroups for the specific
platforms. I don't know about Linux, but Sleep(n) under Windows puts your
application to sleep for n milliseconds (i.e., n/1000 seconds), not n
seconds.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #2

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.per.paradox.net.au...
"Eternally" <m@r.com> wrote in message
news:ko******************@twister.nyroc.rr.com
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm
running the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with
my application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's
the trick to that?

I noticed that on certain applications like those used to decode
video, you can specify how much CPU to use (high priority, low
priority...), how is that accomplished? How can I bring my
applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%,
however telling it to sleep for a full second is too much. A tenth
of a second would be fine, but sleep only takes ints. I then tried
using clock() and making it pause until a certain amount of time had
passed like .005 of a second, but the cpu sticks at 100% with that
method (since it's running in a while loop testing the amount of time
passed....i.e. it's not really sleeping).

Thanks in advance for the help!

This is platform specific and is better asked in newsgroups for the

specific platforms. I don't know about Linux, but Sleep(n) under Windows puts your
application to sleep for n milliseconds (i.e., n/1000 seconds), not n
seconds.


Well, like I said, the application is cross-platform to an extent. I'm not
looking for a platform specific solution, as then it either won't run in
Windows under Cygwin, or it won't run under Linux.

I'm more or less looking for a C++ solution. Does one not exist?

Oh, and the sleep function only takes an int representing seconds, not
milliseconds, so that doesn't work. Thanks for the suggestion though.
Jul 19 '05 #3
Eternally wrote:
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running
the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's the
trick to that?

I noticed that on certain applications like those used to decode video, you
can specify how much CPU to use (high priority, low priority...), how is
that accomplished? How can I bring my applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%, however
telling it to sleep for a full second is too much. A tenth of a second
would be fine, but sleep only takes ints. I then tried using clock() and
making it pause until a certain amount of time had passed like .005 of a
second, but the cpu sticks at 100% with that method (since it's running in a
while loop testing the amount of time passed....i.e. it's not really
sleeping).

Thanks in advance for the help!

What are you doing that you need 97% of the CPU ?

Is your code event driven ? Can it be event driven ?

Jul 19 '05 #4
"Eternally" <m@r.com> wrote in message
news:ca*******************@twister.nyroc.rr.com
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.per.paradox.net.au...

This is platform specific and is better asked in newsgroups for the
specific platforms. I don't know about Linux, but Sleep(n) under
Windows puts your application to sleep for n milliseconds (i.e.,
n/1000 seconds), not n seconds.
Well, like I said, the application is cross-platform to an extent.
I'm not looking for a platform specific solution, as then it either
won't run in Windows under Cygwin, or it won't run under Linux.

I'm more or less looking for a C++ solution. Does one not exist?


Threads are platform specific. Standard C++ knows nothing of them. You may
be able to find a cross-platform threading library, but it will be an
extension to standard C++.
Oh, and the sleep function only takes an int representing seconds, not
milliseconds, so that doesn't work. Thanks for the suggestion though.


I was referring to Sleep with a capital S. I don't know anything about
sleep, which appears to be neither a standard C++ nor a standard Windows
function.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #5
Eternally wrote:
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm
running the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with
my application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's
the trick to that?
What is your program doing in this time? Idling in a loop or doing
calculations? The former should be changed to something event based. If
the latter, it's not wise to make your program use a fixed maximum
amount of CPU time. If no other process is running that would need the
CPU, your program would only run at 1/10 of its possible maximum speed
and the rest of the CPU time would be spent idling in the OS's idle
process.
I noticed that on certain applications like those used to decode
video, you can specify how much CPU to use (high priority, low
priority...), how is that accomplished?


That's a better approach. If there are other processes with higher
priority, those will be preferred. If not, your program gets the CPU.
So your program doesn't make other things slower, but if there is
nothing else to do (other processes waiting for IO e.g.), it'll still
get the most possible CPU time.
Anyway, all that is highly system specific, since C++ doesn't define
anything about multitasking and process priorities. You will have to
use system specific functions. Since you wrote you're writing for
cygwin and linux, there is a good chance that you find a posix function
that works for both.

Jul 19 '05 #6
Rolf Magnus <ra******@t-online.de> wrote in message news:<bo*************@news.t-online.com>...
Eternally wrote:
I'd like to bring that down to at most 10% of the CPU usage. What's
the trick to that?
There is no mechanism within C++. Even sleep is not part of C++, it
is POSIX. Since you wrote you're writing for
cygwin and linux, there is a good chance that you find a posix function
that works for both.


To reduce priority, use POSIX "nice." To sleep for less than one
second, "nanosleep".
Jul 19 '05 #7
Hi,

Wat might work is using 'select'. However do not select anything just use
the timeout.

select is available on most unixes and win32.

int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout);

Regards, Ron AF Greve.

"Eternally" <m@r.com> wrote in message
news:ko******************@twister.nyroc.rr.com...
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running
the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's the
trick to that?

I noticed that on certain applications like those used to decode video, you can specify how much CPU to use (high priority, low priority...), how is
that accomplished? How can I bring my applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%, however telling it to sleep for a full second is too much. A tenth of a second
would be fine, but sleep only takes ints. I then tried using clock() and
making it pause until a certain amount of time had passed like .005 of a
second, but the cpu sticks at 100% with that method (since it's running in a while loop testing the amount of time passed....i.e. it's not really
sleeping).

Thanks in advance for the help!

Jul 19 '05 #8

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bo*************@news.t-online.com...
Eternally wrote:
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm
running the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with
my application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's
the trick to that?
What is your program doing in this time? Idling in a loop or doing
calculations?


It's a real time packet monitor using OpenGl, so it is doing calculations.
It's the display loop that's running at > 60 fps, but I only really need it
to run at most 10 fps. Since it's real-time, and the graph is constantly
adjusting with time, I don't think there's any even that I could use to
adjust it.
The former should be changed to something event based. If
the latter, it's not wise to make your program use a fixed maximum
amount of CPU time. If no other process is running that would need the
CPU, your program would only run at 1/10 of its possible maximum speed
and the rest of the CPU time would be spent idling in the OS's idle
process.
I noticed that on certain applications like those used to decode
video, you can specify how much CPU to use (high priority, low
priority...), how is that accomplished?


That's a better approach. If there are other processes with higher
priority, those will be preferred. If not, your program gets the CPU.
So your program doesn't make other things slower, but if there is
nothing else to do (other processes waiting for IO e.g.), it'll still
get the most possible CPU time.
Anyway, all that is highly system specific, since C++ doesn't define
anything about multitasking and process priorities. You will have to
use system specific functions. Since you wrote you're writing for
cygwin and linux, there is a good chance that you find a posix function
that works for both.


Ok, thanks. I'll post to a Unix or Linux programming group. I didn't think
cygwin would be capable of that since while the program thinks it's running
in something similar to Linux, Cygwin is still running in Windows. I guess
Cygwin has conversion functions though and calls the appropriate windows
functions.

THanks for the help!
Jul 19 '05 #9

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bo********@dispatch.concentric.net...
Eternally wrote:
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's the
trick to that?

I noticed that on certain applications like those used to decode video, you can specify how much CPU to use (high priority, low priority...), how is
that accomplished? How can I bring my applications CPU usage down to 10%?
Note: I tried running sleep(1), and that brought it down below 10%, however telling it to sleep for a full second is too much. A tenth of a second
would be fine, but sleep only takes ints. I then tried using clock() and making it pause until a certain amount of time had passed like .005 of a
second, but the cpu sticks at 100% with that method (since it's running in a while loop testing the amount of time passed....i.e. it's not really
sleeping).

Thanks in advance for the help!

What are you doing that you need 97% of the CPU ?

Is your code event driven ? Can it be event driven ?


It's a real time packet monitor displayed in a graph using OpenGl. It's the
display loop that's running at > 60 fps, but I only really need it to run at
most 10 fps. Since it's real-time, and the graph is constantly adjusting
with time, I don't think there's any event that I could use to adjust it.
Jul 19 '05 #10
"Eternally" <m@r.com> wrote in message
news:0F*******************@twister.nyroc.rr.com...
It's a real time packet monitor displayed in a graph using OpenGl. It's the display loop that's running at > 60 fps, but I only really need it to run at most 10 fps. Since it's real-time, and the graph is constantly adjusting
with time, I don't think there's any event that I could use to adjust it.


If you sleep for about 80-90ms, then you will get about 10fps with a
real low CPU usage. Actually, you would need 100ms to be at exactly 10fps,
but firstly you cannot sleep for *exactly* 100ms and secondly, you also need
some time for calculations.

<Platform-Specific> On Windows you can use "Sleep" to have your process
sleep for n milliseconds. For other platforms I do not know.
</Platform-Specific>

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #11

"Eternally" <m@r.com> wrote in message news:ko******************@twister.nyroc.rr.com...
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running
the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.


You're going to have to use a system dependent call to give up the CPU.
On UNIXOIDS it's going to be something like setitimer. On windows it's
a bit more involved.
Jul 19 '05 #12
Eternally wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bo********@dispatch.concentric.net...

- now this is off topic for c.l.c++

It's a real time packet monitor displayed in a graph using OpenGl. It's the
display loop that's running at > 60 fps, but I only really need it to run at
most 10 fps. Since it's real-time, and the graph is constantly adjusting
with time, I don't think there's any event that I could use to adjust it.


I thought when using double buffering, the call to switch front and back
buffers would automatically wait for the next frame. It's been a while.

Jul 19 '05 #13
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bo********@dispatch.concentric.net...
Eternally wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bo********@dispatch.concentric.net...

- now this is off topic for c.l.c++

It's a real time packet monitor displayed in a graph using OpenGl. It's the display loop that's running at > 60 fps, but I only really need it to run at most 10 fps. Since it's real-time, and the graph is constantly adjusting with time, I don't think there's any event that I could use to adjust

it.
I thought when using double buffering, the call to switch front and back
buffers would automatically wait for the next frame. It's been a while.

If vsync is on, it will wait for the next refresh of the screen.
Otherwise it will not wait at all.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #14
Jakob Bieling wrote:

<Platform-Specific> On Windows you can use "Sleep" to have your process
sleep for n milliseconds. For other platforms I do not know.
</Platform-Specific>


<more-platform-specific> Sleep() does not guarantee an exact time. If
no other threads are ready to run, the Sleep()ing thread could
conceivably resume immediately. A timer event might a the better way to
go. Does a corresponding thingy exist in Linux?</m-p-s>

--
Mike Smith

Jul 19 '05 #15
"Mike Smith" <mi*******************@acm.DOT.org> wrote in message
news:vr************@news.supernews.com...
Jakob Bieling wrote:

<Platform-Specific> On Windows you can use "Sleep" to have your process sleep for n milliseconds. For other platforms I do not know.
</Platform-Specific>


<more-platform-specific> Sleep() does not guarantee an exact time. If
no other threads are ready to run, the Sleep()ing thread could
conceivably resume immediately


It's right that it is not exact, but when you say 80ms, your thread is
going to sleep for at least 80ms - guaranteed. This is why I suggested
roughly 80-90ms for the timeout; the remaining 10-20ms are for executing
code and for the inaccuracies.

regards
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #16
Mike Smith <mi*******************@acm.DOT.org> writes:
Jakob Bieling wrote:
<Platform-Specific> On Windows you can use "Sleep" to have your
process
sleep for n milliseconds. For other platforms I do not know.
</Platform-Specific>


<more-platform-specific> Sleep() does not guarantee an exact time. If
no other threads are ready to run, the Sleep()ing thread could
conceivably resume immediately. A timer event might a the better way
to go. Does a corresponding thingy exist in Linux?</m-p-s>


man nanosleep
man 3 usleep

HTH & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #17

"Eternally" <m@r.com> wrote in message
news:ko******************@twister.nyroc.rr.com...
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running
the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's the
trick to that?

I noticed that on certain applications like those used to decode video, you can specify how much CPU to use (high priority, low priority...), how is
that accomplished? How can I bring my applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%, however telling it to sleep for a full second is too much. A tenth of a second
would be fine, but sleep only takes ints. I then tried using clock() and
making it pause until a certain amount of time had passed like .005 of a
second, but the cpu sticks at 100% with that method (since it's running in a while loop testing the amount of time passed....i.e. it's not really
sleeping).

Thanks in advance for the help!


Thanks everyone! usleep did the trick!
Jul 19 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: mmf | last post by:
Hi. My problem: How can I make sure that a Python process does not use more that 30% of the CPU at any time. I only want that the process never uses more, but I don't want the process being...
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
7
by: George Gre | last post by:
Hi, I wrote a c# programme that listens to incoming TCP requests and services them. This programme is meant to be running as long as the server its installed on is running. So we assume for...
3
by: Jeff | last post by:
I've been working on an application for a while now that has been giving me some trouble when it comes to working with a picturebox and memory usage. My company deals with digital imaging, so we...
2
by: John | last post by:
I have a 325k VB.Net app that when loaded uses about 25Meg. There's SQL calls, etc. but how can it take up so much space and how can I reduce the memory footprint? John
35
by: Alex Martelli | last post by:
Having fixed a memory leak (not the leak of a Python reference, some other stuff I wasn't properly freeing in certain cases) in a C-coded extension I maintain, I need a way to test that the leak is...
3
by: Tor Erik | last post by:
Hi, This should be possible as Taskmanager tracks CPU usage for every process... Anyone know how this can be done? regards
12
by: Sankar | last post by:
Dear all, I am programming in Linux , wherein I need to know a couple of things. 1) Does an API exist that can copy file onto another file ( an API equivalent of 'cp') ? 2) Is there an API...
0
by: Oli Schacher | last post by:
Hi all I wrote a multithreaded script that polls mails from several pop/imap accounts. To fetch the messages I'm using the getmail classes ( http://pyropus.ca/software/getmail/ ) , those...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.