Recipe6.1.Setting the Priority of a Process


Recipe 6.1. Setting the Priority of a Process

Problem

You want to raise or lower the priority of a process. This is beneficial if you want to boost a CPU-starved process or limit a process that is hogging the CPU.

Solution

Using a graphical user interface

  1. Open Windows Task Manager (taskmgr.exe).

  2. Click on the Processes tab.

  3. If you do not see the process you want to set, be sure the box beside Show processes from all users is checked.

  4. Right-click on the target process, select Set Priority, and select the desired priority.

You can also accomplish the same task using the Sysinternals Process Explorer (procexp.exe) tool: right-click on the process and select Set Priority.

Using a command-line interface

With the start command, you can set the priority of a process when you initially run it. The following example shows how to create a process with a high priority:

> start /HIGH <ProgramPath>

The other valid priority options include /LOW, /NORMAL, /REALTIME, /ABOVENORMAL, and /BELOWNORMAL.

Using VBScript
' This code sets the priority of a process     Const NORMAL        = 32 Const IDLE          = 64 Const HIGH_PRIORITY = 128 Const REALTIME      = 256 Const BELOW_NORMAL  = 16384 Const ABOVE_NORMAL  = 32768     ' ------ SCRIPT CONFIGURATION ------ strComputer = "." intPID      = 3280          ' set this to the PID of the target process intPriority = ABOVE_NORMAL  ' Set this to one of the constants above ' ------ END CONFIGURATION --------- WScript.Echo "Process PID: " & intPID set objWMIProcess = GetObject("winmgmts:\\" & strComputer & _                     "\root\cimv2:Win32_Process.Handle='" & intPID & "'") WScript.Echo "Process name: " & objWMIProcess.Name intRC = objWMIProcess.SetPriority(intPriority)  if intRC = 0 Then    Wscript.Echo "Successfully set priority." else    Wscript.Echo "Could not set priority. Error code: " & intRC end if

Discussion

Windows 2000 and Windows Server 2003 use a priority-driven, preemptive scheduling system. This means that processes are given priorities and those with higher priorities get more CPU time and subsequently, run quicker. This is useful for administrators to know because in certain situations, it can be advantageous to play with the priorities of processes to get the desired result from your system. For example, if you find a process that is pegging the CPU on a system, if you are able to run Task Manager, you can lower the priority of that process, which should help you launch other processes or applications to diagnose and troubleshoot the badly behaving process. Another way to tackle this problem is to suspend the process, which I describe in Recipe 6.2.

Windows supports six priority classes. The following is a list of the classes and their corresponding numeric value:


Realtime (24)

This is the highest priority class. Be extremely careful when setting this priority because it preempts all other non-realtime processes, including operating system processes. A realtime process can interrupt normal functioning of the computer, including mouse and keyboard I/O and disk read/writes. Note that realtime priority does not make a program run in realtime, it simply gives as much CPU time as the program can use and is available.

Windows operating systems are not considered real-time operating systems because they do not (and cannot) give performance guarantees.



High (13)

This priority class indicates that the process needs to perform time-sensitive functions that must be executed immediately upon being called. An example application that uses this priority is Task Manager, which needs a higher priority than normal processes so you can kill any that are CPU bound. Be careful when setting this priority class because a high priority process that is CPU bound can tie up the system indefinitely.


Above Normal (10)

This is an intermediate priority class that is above Normal but below High.


Normal (8)

This is the default priority class assigned to applications that do not require any special scheduling needs.


Below Normal (6)

This is an intermediate priority class that is above Idle but below Normal.


Idle or Low (4)

This priority indicates that the process only runs when the system is idle. Screen saver is an example application that utilizes this priority.

To view the current priority of all processes, run the following command:

> wmic process get name,processid,priority

You can also see the priority of processes in pslist output (the Pri column).

See Also

Recipe 6.2, MS KB 110853 (PRB: Can't Increase Process Priority), and MS KB 193846 (HOWTO: Modify the Process Priority of a Shelled Application)



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net