top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to control CPU utilization via C/C++/Python Program?

+2 votes
721 views

Below instruction will start CPU stress with maximum of 100%,

while(1);

Below instructions will start CPU stress with near to 0%.

while(1)
     sleep(1);

Is there any way to control this? I mean what if i want to start CPU stress of >100% and what if want to start CPU stress with particular number?

Can any one help in programming that? I am looking for C/C++/Python solution on Linux...

posted Aug 18, 2016 by Chirag Gangdev

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
You can write a simple shall script which can monitor a process and whenever it goes beyond say 99% just kill the process.

Let me know your thoughts
No Sir, My question is different.
In simple words....I want to create one function which will take 1 argument i.e. how much CPU stress we want to start using our application.
Suppose, if we give call a function with argument as 75 then our application should stress 75% CPU.
If we give call a function with argument as 140 then our application should stress 140% CPU.
Its difficult to write a generalized function as CPU cycle to different machine is different. For example on one machine sleep of 10 nanosection followed by 1000 loops may give 50% usage but the same will result in different usage on different machine.

I am not sure if there is a library function to achieve that but its less likely.

Similar Questions
+2 votes

Let's say I have an exe-file (for example computer game) and need to forbid to run it until certain date or time during the day. Any 'manipulations' with file are allowed.

Could you, please, offer me a simple way of how to encode/decode such a file mostly in C or C++?

+2 votes

I want to run a command in every 10 minute which should return the maximum CPU utilization of a linux machine in last 10 min irrespective of no. of core present.
Thanks in advance.

0 votes

I consider myself a python programmer, although C++ was one of the first languages I learned (not really deeply and long time ago).

Now I decided to retake C++, to broaden my view of the business. However, as I progress in learning C++, I cannot take out of my head one question

Why to use C++ instead of python?

It is not ranting against C++. I was/am looking for small-medium projects to exercise my C++ skills. But I'm interested in a "genuine" C++ project: some task where C++ is really THE language (and where python is actually a bad as initial choice).

The usual argument in favor of C++ (when comparing to python) is performance. But I'm convinced that, in general, the right approach is "python-profiling-(extension/numpy/Cython/...)". At least for a python
programmer. I might be wrong, though.

This is, perhaps, a bit off-topic, but I really want to know the thoughts of experienced python programmers on it.

...