Skip to content

Instantly share code, notes, and snippets.

@iljavs
Created July 15, 2020 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iljavs/5d693ae09c68fc5c2676450c3604a516 to your computer and use it in GitHub Desktop.
Save iljavs/5d693ae09c68fc5c2676450c3604a516 to your computer and use it in GitHub Desktop.
#include <Windows.h>
#include <stdio.h>
int main(int argc, char **argv){
if (argc < 3) {
printf("<pid> <percentage> arguments required\n");
exit(0);
}
DWORD pid = atoi(argv[1]);
DWORD pct = atoi(argv[2]);
HANDLE ph = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (ph == NULL) {
printf("open for pid %u failed (%u)\n", pid, GetLastError());
exit(0);
}
HANDLE jh = CreateJobObject(NULL, NULL);
if (jh == NULL) {
printf("CreateJobObject() failed (%u)\n", GetLastError());
exit(0);
}
BOOL r = AssignProcessToJobObject(jh, ph);
if (r == 0) {
printf("AssignProcessToJobObject() failed (%u)\n", GetLastError());
exit(0);
}
JOBOBJECT_CPU_RATE_CONTROL_INFORMATION jc;
jc.ControlFlags = JOB_OBJECT_CPU_RATE_CONTROL_ENABLE | JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP;
jc.CpuRate = pct * 100;
r = SetInformationJobObject(jh, JobObjectCpuRateControlInformation, &jc, sizeof(jc));
if (r == 0) {
printf("SetInformationJobObject() failed (%u)\n", GetLastError());
exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment