3

I need to write a class that will attach to certain windows processes, monitor and limit their cpu usage. Process priority changing would not help me at all so i really need to write a program that is basically similar to BES or ThreadMaster. So i need to make a class that's doing something like this (pseudo code):

public void cpuLimiter(pid)
{
    ProcessHandle handle = attachToProcess(pid);
    while (cpuLimiting)
    {
        if (handle.cpuUsage > 30%)
        {
            handle.sleep(100miliseconds);
        }
        sleep(10miliseconds);
    }
    closeHandle(pid);
}

I hope i made it clear what i want to accomplish, just i have no idea how. Every help is appreciated.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Igor Gajic
  • 39
  • 2
  • 4
    `beg you for the most complete code` This kind of makes it sound like you want us to just spoon-feed you a solution rather than addressing a specific problem with *code you've written*. The latter is a good question, the former is not. Please show what you've tried. – tnw Aug 18 '14 at 14:28
  • It is definitely going to be harder than what you think. Can't be answered in StackOverflow answer. Try something, run into a **specific** problem. Come here. We're happy to help. – Sriram Sakthivel Aug 18 '14 at 14:31
  • 1
    possible duplicate of [How can I programmatically limit my program's CPU usage to below 70%?](http://stackoverflow.com/questions/989281/how-can-i-programmatically-limit-my-programs-cpu-usage-to-below-70) – Yair Nevet Aug 18 '14 at 14:32
  • tnw thanks for your remark, i agree with you, actually i didnt ask for a complete solution, i was just afraid if someone sends me to some assembly stuff, and i know i wouldnt even understand it – Igor Gajic Aug 18 '14 at 14:49
  • Is there any way to just send sleep(x miliseconds) to another process in windows? everything else i can improvise or i have already done, but i have no idea at all how to send sleep or suspend / start to another process in windows. – Igor Gajic Aug 18 '14 at 14:52
  • Are you programming in .NET C#? I had this issue with a console app that I created for Windows when I forgot to make the infinite while loop sleep. Make sure you call `System.Threading.Thead.Sleep(milliseconds)` in your infinite while loop in the Main() method of your program. That will reduce CPU usage, because if your infinite while loop does not sleep the CPU will spend a lot of time evaluating the condition of the while loop. – John Verco Aug 18 '14 at 15:43

1 Answers1

8
  • You create a JOB object
  • You limit the CPU for the JOB
  • you attach the target process to the JOB

See Job Objects, CreateJobObject, SetInformationJobObject and JOBOBJECT_CPU_RATE_CONTROL_INFORMATION, AssignProcessToJobObject. Do not attempt to hand-craft your own in-house process throttling, let the component specifically designed for this task (ie. jobs) handle the task.

See Working example of CreateJobObject/SetInformationJobObject pinvoke in .net? for managed usage of the NT Job native API.

Community
  • 1
  • 1
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569