Home > Windows API > Changing scheduling priority of a process at runtime

Changing scheduling priority of a process at runtime

I had a requirement sometime back to make my process have a higher priority at runtime, so went hunting around for an API which does this and found one with an intuitive name, “SetPriorityClass”. So what is process priority? Process priority is the value which determines priority of a process when it’s being scheduled. If there are two processes having normal priority and above normal priority respectively, then the one with above normal priority is given preference. Internally it’s more complicated than what I explained.

We normally set priority through task manager…

Changing process priority through task manager

Changing process priority through task manager

These are the priority classes defined in windows (From MSDN)…

  1. IDLE_PRIORITY_CLASS – Process whose threads run only when the system is idle. The threads of the process are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle-priority class is inherited by child processes.
  3. NORMAL_PRIORITY_CLASS – Process with no special scheduling needs.
  5. HIGH_PRIORITY_CLASS – Process that performs time-critical tasks that must be executed immediately. The threads of the process preempt the threads of normal or idle priority class processes. An example is the Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high-priority class application can use nearly all available CPU time.
  6. REALTIME_PRIORITY_CLASS –Process that has the highest possible priority. The threads of the process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive.

So these are the six priorities supported in windows and here is a small function which changes priority of current process to above normal class.

void ChangeProcessPriority()
   HANDLE hProcess = GetCurrentProcess();
   SetPriorityClass( hProcess , ABOVE_NORMAL_PRIORITY_CLASS );

Note that for above code to work; the handle given to SetPriorityClass must have PROCESS_SET_INFORMATION right. Use OpenProcessToken function to get a process token and then use AdjustTokenPrivileges function to set new privileges. A demo for changing process priority is given here -> http://nibuthomas.com/2007/06/28/function-for-enabling-privileges-for-your-application/

To retrieve current priority of a process call GetPriorityClass(). Also for this function to work handle given must have PROCESS_QUERY_INFORMATION privilege set.

DWORD GetProcessPriority()
   HANDLE hProcess = GetCurrentProcess();
   return GetPriorityClass( hProcess );
  1. John Schroedl
    March 4, 2009 at 4:31 pm

    Sorry for OT but I noticed your feed seems to have changed to snippet mode instead of full post. Please reconsider! Thanks, that is all.

    • March 5, 2009 at 4:57 am

      Hi John,

      I’ve changed from summary view to full view, also changed feed post count from 10 to 20. Thanks for taking your precious time to post a feedback.


  1. March 4, 2009 at 5:44 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: