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.)