By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Trick to low CPU usage?

P: n/a
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
Share this Question
Share on Google+
17 Replies


P: n/a
"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

P: n/a

"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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

P: n/a
"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

P: n/a

"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.