Changing process priority

J

John den Haan

Hey!

This question may be basic for you guys, but I haven't found anything
related to it in the FAQ. How do I make sure a simple while(1){} loop
(for example) doesn't consume half my CPU?

Thanks

--

Cheers,

John den Haan
joDhn[dot]haEan[at]chLello[dot]nl

Remove capital 'DEL' from above addy to obtain e-mail address
 
I

Ian Collins

John said:
Hey!

This question may be basic for you guys, but I haven't found anything
related to it in the FAQ. How do I make sure a simple while(1){} loop
(for example) doesn't consume half my CPU?
That isn't a C issue, but an OS one. So you'll have to ask on a group
dedicated to your OS.
 
W

Walter Roberson

This question may be basic for you guys, but I haven't found anything
related to it in the FAQ. How do I make sure a simple while(1){} loop
(for example) doesn't consume half my CPU?

If you restrict your choices to those available within the C
standard, then the answer is "Don't write such a loop,
and don't let anyone else write such a loop either."

If you are willing to go outside the facilities provided by standard C,
then you are dealing with OS-specific resource
allocation policies that C doesn't know anything about. In order
to find out about those policies and how to manipulate them,
you would need to ask in a forum that deals with your specific OS.


I think you will find that it is difficult to do what you want,
even with common OS extensions. OS extensions that control
process priority -usually- are set up to change -relative-
priorities, and if there is only one task available to run,
-usually- even if you lower your process priority as far as possible,
the process will consume as much CPU as is not being used for
anything else. Though sometimes there is a "idle process" that runs
an infinite loop consuming all leftover CPU time, and if you manage to
lower your process priority to match that of the idle process,
then your process and the idle process might each average half of
the available CPU time -- but keep in mind that in such a scheme,
the whole CPU time is still being used by -something-.

In order to get a hard guarantee that your process is not going to
use half (or more) of the CPU even when nothing else is waiting to run,
you would probably need to use an OS that provided "real time"
processes and processor quanta. Common operating systems don't
provide those kind of facilities (though I don't know if Linux has
them.)
 
J

John den Haan

Walter Roberson schreef:
In order to get a hard guarantee that your process is not going to
use half (or more) of the CPU even when nothing else is waiting to run,
you would probably need to use an OS that provided "real time"
processes and processor quanta. Common operating systems don't
provide those kind of facilities (though I don't know if Linux has
them.)

What I am looking for is something like the 'doevents()' call in VB
which allows one to keep the OS responsive while running my app (a
game). I mean, how do all games do it? They have to loop somewhere in
order to receive input... Or do they use some form of event-driven code?

--

Cheers,

John den Haan
joDhn[dot]haEan[at]chLello[dot]nl

Remove capital 'DEL' from above addy to obtain e-mail address
 
W

Walter Roberson

John den Haan said:
What I am looking for is something like the 'doevents()' call in VB
which allows one to keep the OS responsive while running my app (a
game).

As I indicated earlier, there is no standard C way to do anything like
this. To find out what can be done on your OS, you will need to
ask in a forum that knows specifics about your OS. Specific answers
here are more likely to be wrong than in your OS newsgroup -- for
one thing, you didn't even indicate which OS you are using.
(Referring to VB is not an indication that you are using Windows,
only an indication that you want functionality -like- what you see
there. And I really don't know what doevents() does, as I never
use VB and doevents() is not part of the C standard.)

I mean, how do all games do it? They have to loop somewhere in
order to receive input... Or do they use some form of event-driven code?

Games use OS specific mechanisms, which are going to vary from OS
to OS. The mechanisms I would use on my OS are probably quite different
than the mechanisms you would use on whatever OS you are using
(the OS I use isn't very common.)
 
J

Jack Klein

Walter Roberson schreef:


What I am looking for is something like the 'doevents()' call in VB
which allows one to keep the OS responsive while running my app (a
game). I mean, how do all games do it? They have to loop somewhere in
order to receive input... Or do they use some form of event-driven code?

Walter went over the top with his discussion of your off-topic
question. There is absolutely no way to do what you are asking about
in standard C, and that is what is discussed here.

There might be a way to do this using non-standard extensions on your
particular platform, but that has nothing at all to do with the C
language.

Based on your comment, I suggest asking in a Windows group like
or searching Microsoft's
MSDN web site.
 
S

Simon Biber

John said:
What I am looking for is something like the 'doevents()' call in VB
which allows one to keep the OS responsive while running my app (a
game). I mean, how do all games do it? They have to loop somewhere in
order to receive input... Or do they use some form of event-driven code?

Whatever library you are using to get input should provide functions to
wait for the next event or something similar. For example, in the Simple
DirectMedia Layer (SDL) you can use the function:
int SDL_WaitEvent(SDL_Event *event);

There is also an unconditional delay function that will not return as
soon as an event occurs:
void SDL_Delay(Uint32 ms);

These provided functions will generally release the processor to the
operating system wherever possible.
 
R

Richard Heathfield

John den Haan said:
Hey!

This question may be basic for you guys, but I haven't found anything
related to it in the FAQ. How do I make sure a simple while(1){} loop
(for example) doesn't consume half my CPU?

By not putting one into your program.
 
M

mazzawi

you can have your program block and wait for a signal instead of just
loop infinitely.
 
S

santosh

you can have your program block and wait for a signal instead of just
loop infinitely.

Please quote context. Yes, this is a good method but Standard C
provides no mechanism to accomplish this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top