I am writing C code (on Linux) that must consume a certain amount of CPU when it starts. I am doing an experiment in which I run certain actions when a certain threshold of the CPU is reached. So, once Disposal reaches a certain threshold, I need to keep it in this state, for example, 30 seconds before the completion of my experiments. I control the CPU usage with the top command.
So my questions are 1. How do I increase CPU utilization to a given value (in a deterministic way, if possible)? 2. As soon as I get to the threshold, is there a way to keep it at this level for a predetermined time?
An example of the output of the upper command (the 9th column is the processor used by the "top" process) 19304 abcde 16 0 5448 1212 808 R 0.2 0.0 0: 00.06 top
As above, I will look at the line above that shows the use of my binary.
Any help would be greatly appreciated. Also, let me know if you need more information.
Thanks!
Edit: The following lines of code allowed me to pretty well control the CPU usage. In the following case, I have 2 options: keep it above 50% and keep it below 50%. After some trial and error, I settled these usleep values.
endwait = clock() + ( seconds * CLOCKS_PER_SEC ); while( clock() < endwait ) {} if (cpu_utilization > 50) usleep(250000); else usleep(700000);
Hope this helps!
source share