2

Is there a way to limit CPU usage in C++?

I mean can I write a program which takes input %CPU to be used and it uses maximum that much amount of CPU.

I basically want to add the functionality of cpulimit command internally in the program.

If there is, how to do it ?

Edits:

Environment: Linux (debian) with gcc 6.1. It should support as many arbitrary numbers as possible. i.e a range of 1% - 100% . If the OS cannot do, an error can be logged and the nearest value to it can be used or any other solution that is recommended when the OS restricts that number.

BEC
  • 45
  • 1
  • 11
  • 2
    To answer the question asked: yes, there is a way to do that. – Sam Varshavchik Sep 17 '16 at 14:41
  • @SamVarshavchik I guess now that I have edited the question, you can help me with telling how can I possibly achieve this ? – BEC Sep 17 '16 at 14:43
  • What operating system? – Raymond Chen Sep 17 '16 at 14:43
  • @RaymondChen Linux with gcc – BEC Sep 17 '16 at 14:44
  • @KrisVandermotten I have changed the question to add details about the operating system. – BEC Sep 17 '16 at 14:46
  • It sounds like you can do this co-operatively, modify the program itself. If so, and if there are easily identifiable parts in your program which do heavy calculations, then messure wall clock time spent in them. When it exceeds your desired max %, just sleep a bit. – hyde Sep 17 '16 at 15:52
  • the easiest way is to run inside Virtualbox, which has the ability to limit guest CPU usage – phuclv Sep 17 '16 at 15:56

2 Answers2

2

Linux does not provide the means to set a specific percentage. However, the nice(2) system call lowers the priority of the process in relation to other processes on the system, thus achieving some sort of a relative percentage of CPU, in relation to other processes on the system.

You can also use the setrlimit(2) system call to set your process's RLIMIT_CPU, as a fixed amount.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • 1
    That's a limit in seconds, not percentage. – n. m. could be an AI Sep 17 '16 at 14:47
  • RLIMIT_CPU only provides limit in time, I more of want to control CPU% which is displayed for every process in top. – BEC Sep 17 '16 at 14:48
  • But then how does cpulimit work if linux doesn't provide any such functionality? cpulimit command seems to do a fair job in limiting the %CPU usage of a process. – BEC Sep 17 '16 at 14:56
  • Since the source code to cpulimit is freely available, you can look at it yourself and find out. I just took a brief glance myself, and it is a heuristic algorithm that manually halts and resumes each process using `SIGSTOP` and `SIGCONT`. This is not a Linux kernel feature, but a heuristic rate-limiting implementation. You could do the same thing by periodically checking `/proc`, see how much CPU time your process has burned, then manually `sleep()` for the same period of time, in order to have a target 50% CPU utilization, for example. – Sam Varshavchik Sep 17 '16 at 15:09
1

Use getrusage(), see Linux commands to detect the computer resource usage of a program

And when you check, and you've used however many milliseconds of runtime you want, use nanosleep() to sleep a few milliseconds. Adjust percentages to match your requirements.

Community
  • 1
  • 1
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • It makes sense, now that linux kernel doesn't provide any such feature. I can do such things. – BEC Sep 17 '16 at 17:06