A process' priority determines how its execution is scheduled by the Win32kernel. Processes with a higher priority execute more quickly thanprocesses with a lower priority. This article describes how to modify theProcess priority of a shelled application.
Win32 supports the following four priority classes:
Idle is the correct priority for system-monitoring applications or screensavers.
Normal priority class is the default, indicating a process with no specialscheduling needs.
High priority indicates a process that performs time-critical tasksrequiring immediate execution for it to run correctly. The threads of ahigh-priority class process preempt the threads of normal or idle priorityclass processes. An example is the Windows Task List, which must respondquickly when called by the user, regardless of the load on the operatingsystem. Use extreme care when using the high-priority class, because a high-priority class CPU-bound application can use almost all available cycles.
The Realtime priority indicates a process that has the highest possiblepriority. The threads of a real-time priority class process preempt thethreads of all other processes, including operating system processesperforming important tasks. For example, a real-time process that executesfor more than a very brief interval can cause disk caches not to flush orcause the mouse to be unresponsive. This priority should almost never beused.
The process' priority is set by assigning one of the parameters of theCreateProcess API call.
The following example demonstrates running the Windows NOTEPAD applicationunder Windows NT 4.0 and later Windows operating systems using an idle priority. The process priority is controlledby the 'dwCreationFlags' variable passed to CreateProcess(). DEFINEs forthe four supported priority classes are listed at the top of the sampleprogram.
Once Notepad.exe is running, to examine the priority, start the Windows Task Manager, select the Processes tab, right-click Notepad.exe and select'Set Priority' from the pop-up menu.
The fully qualified pathname to Notepad.exe may need modification in theassignment statement of variable 'File2Run'.
#DEFINE NORMAL_PRIORITY_CLASS 32 #DEFINE IDLE_PRIORITY_CLASS 64 #DEFINE HIGH_PRIORITY_CLASS 128 #DEFINE REALTIME_PRIORITY_CLASS 1600 DECLARE INTEGER CreateProcess IN kernel32.DLL ; INTEGER lpApplicationName, ; STRING lpCommandLine, ; INTEGER lpProcessAttributes, ; INTEGER lpThreadAttributes, ; INTEGER bInheritHandles, ; INTEGER dwCreationFlags, ; INTEGER lpEnvironment, ; INTEGER lpCurrentDirectory, ; STRING @lpStartupInfo, ; STRING @lpProcessInformation DECLARE INTEGER GetLastError IN kernel32.DLL * Startup info is 68 bytes, of which we need to * initially populate the 'cb' or Count of Bytes member * with the overall length of the structure. * The remainder should be 0-filled. START = long2str(68) + REPLICATE(CHR(0), 64) * PROCESS_INFORMATION structure is 4 longs, * or 4*4 bytes = 16 bytes, which we'll initially zero. process_info = REPLICATE(CHR(0), 16) * Start a copy of NOTEPAD (EXE name must be null-terminated) File2Run = "C:\WINNT\NOTEPAD.EXE" + CHR(0) * Call CreateProcess, obtain a process handle. Treat the * application to run as the 'command line' argument, accept * all other defaults. Important to pass the start and * process_info by reference. RetCode = CreateProcess(0, File2Run, 0, 0, 1, ; IDLE_PRIORITY_CLASS, 0, 0, @START, @process_info) * Unable to run, exit now. IF RetCode = 0 =MESSAGEBOX("Error occurred. Error code: ", GetLastError()) RETURN ENDIF ******************** FUNCTION long2str ******************** * passed : 32-bit non-negative numeric value (m.longval) * returns : ascii character representation of passed * value in low-high format (m.retstr) * example : * m.long = 999999 * m.longstr = long2str(m.long) PARAMETERS m.longval PRIVATE i, m.retstr m.retstr = "" FOR i = 24 TO 0 STEP -8 m.retstr = CHR(INT(m.longval/(2^i))) + m.retstr m.longval = MOD(m.longval, (2^i)) NEXT RETURN m.retstr
"Advanced Windows", Jeffrey Richter, Microsoft Press, pages 92-103, ThirdEdition 1997, ISBN: 1-57231-548-2