Process & Thread Priorities – 101

 

Recently my friend Nibu has blogged about controlling process priority at run time. On seeing that I feel like to write on the basics of thread and process priorities. Of course veterans of Windows programming will not be able make anything better out of this post.

Each process in the system has assigned a certain level of priority when it’s created. Most of the programs are running in the system having normal priority. Some special programs like task manager will be having “Real-Time” priority level. This means, those programs are having higher chance to get the CPU allocated. There are different classes defined to specify the process priority.

Priority class Symbolic Identifier Description
Real-Time REAL_TIME_PRIORITY_CLASS Used to handle time critical tasks. Threads in this process can pre-empts operating system tasks. So carefully use this priority class.
High HIGH_PRIORITY_CLASS High response to the events. E.g Task manager application
Above Normal ABOVE_NORMAL_PRIORITY_CLASS Slightly above normal priority
Below Normal BELOW_NORMAL_PRIORITY_CLASS Below normal priority
Idle IDLE_PRIORITY_CLASS Threads in this process scheduled when the system is idle. Used by screensavers, indexing services etc.

The priority for a process can be specified when it’s created using CreateProcess API. If nothing specified, the process will run under normal priority.

It’s possible to modify the priority of a Process at run-time using SetPriorityClass API. The function takes following form

[sourcecode language='cpp']
BOOL WINAPI SetPriorityClass( __in HANDLE hProcess, __in DWORD dwPriorityClass );
[/sourcecode]

It’s possible to specify any process handle to modify the thread priority class of corresponding process, provided you have a valid handle and permission to modify the process priority. To modify the priority of your own process can be done in the following way

[sourcecode language='cpp']
SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS);
[/sourcecode]

You can get the current priority of a process by using GetPriorityClass function.

[sourcecode language='cpp']
DWORD WINAPI GetPriorityClass( __in HANDLE hProcess );
[/sourcecode]

OK that’s about Process Priority. Here comes about Thread Priority level. Process and thread priority levels are different and related. As specified above the threads are also having time-critical, high, above normal, below normal and idle priorities. The symbolic names are namely THREAD_PRIORITY_TIME_CRITICAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_IDLE

You can specify a thread’s priority by calling SetThreadPriority Function

[sourcecode language='cpp']
BOOL WINAPI SetThreadPriority( __in HANDLE hThread, __in int nPriority );
[/sourcecode]

Note that you can’t set the the thread priority when you’re creating a thread using CreateThread API. Also, windows doesn’t provide no function to get a thread’s priority because operating system may dynamically boost thread’s priority at special situations (e.g keyboard inputs).

Relative priority of threads

Even we’ve different level of priorities for threads; it’s still related to priority of the running process. The actual range of priorities is 0-31. None of the time-critical threads in a normal priority process can attain priority level 31. The operating system maps the thread based on the process’s priority. For example a process having priority class high can attain a maximum thread priority of 15 even we set the thread priority to time critical. While a thread in a process having real-time priority can gain thread priority level 31.

Developers should not work with numerical values of priority, instead use priority class symbolic names to achieve the desired priority because the range process priorities and other related specification can be changed in the future releases of the operating systems.

As a last point. Thread and process priority should not make you confused. Actually the threads in a process doing the real work and process is actually inert but it providing context for the execution.

 
This entry was posted in Uncategorized. Bookmark the permalink.
  • http://www.rondeetvoyante.com voyance

    i truly needed to tell you that your forum is great

    thank you so much

  • wedeNdeatoWat

    Louisville Kentucky is a rapid developing cosmopolitan diocese in the Collective States of America. The city was established in 1778 on George Rodgers Clark. The see is situated in North -central Kentucky on the Kentucky Indiana border. The city stands exposed mid the safest cities in the United States with a whole populace of 1,268,323 as at 2008. This is the 42nd most populated new zealand urban area in the Connected States of America.

    Louisville Kentucky borough is controlled by both the Metro mayor and a legislative confederation called Metro Council. The Metro Directors comprises of 26 seats which arrive from the 26 Districts constituting the Louisville Kentucky metropolitan area. The caucus is directed close the caucus president with 13 seats being reelected after each two years of political office.

    Louisville Kentucky is the obligation to worship in if you are looking in return an best investment arena in the Synergistic States of America. The saving of this urban district is radically improved with shipping and merchandise industries being the most suitable developed. Louisville Kentucky is centrally situated in the US and features principal road networks and highways linking celebrated cities across the US. It is also assisted close to a well developed railway system that is morals looking for freight and passenger traffic. Wiser stillness, Louisville is conversant with to the Louisville Oecumenical Airport which is a intercontinental air centre with trendy trainload handling capabilities.

    Louisville Kentucky is the hottest journey’s end of churches assuring your weekends memorable and gorged of fun. The burg is served not later than a vibrant media with prominent intercontinental networks and newspaper coverage. Parks and outdoor attractions are within the burg environs making it harmonious of must-go-before-I-die cities. Don’t hang about, make headway presumptuous and go to Louisville Kentucky sooner than possibility thaws.
    Louisville Kentucky Attorney

  • http://www.cheapbootsforsale.us ugg boots

    “Here air jordan 21 products xx, has fashion model, superior quality and service, cheap price and updates quickly.I support strongly always! I want to buy XX, I hesitate to select which style more better.Hope your unique recommends.