Given that multi-core / multi-processor systems are the way of the future, this might seem like an odd post for us to be writing. However, we have run into a few instances where 32-bit applications run with no issues on single processor systems, but fail to run, or perform poorly on multiprocessor systems. I know that seems counterintuitive, but if an application is not multi-processor aware you may experience issues. In cases such as these, there are a couple of different approaches you might try:
Method 1: Setting processor affinity via Task Manager
If the application is already running, then you can use the “Set Affinity” feature within Task Manager on Windows XP and later operating systems. To use this feature, highlight the process within Task Manager, right-click and select the “Set Affinity” option as shown below:
You are then presented with the option to select your processor affinity:
Remember that this option is only available on Windows XP and later operating systems.
Method 2: Use IMAGECFG.EXE to permanently set the processor affinity for the executable (Windows 2000 Systems)
Using this method you can permanently bind an executable to a processor. IMAGECFG.EXE was available on the Windows 2000 Server Resource Kit (Supplement One). To set the processor affinity, use the following syntax from a Command Prompt: imagecfg –a 0xn <full path to the executable>. In our sample syntax, 0xn is the processor affinity mask. This mask is what restricts the application to a specific CPU. To determine the correct mask, use the table below:
You can use this syntax to set the affinity all the way up from processors 0 through 31. Remember that this would be the preferred method on a Windows 2000 system, where you cannot dynamically change the processor affinity via Task Manager.
Method 3: Use PsExec to set the Processor Affinity for an executable on Windows XP and later OS versions
In Method 1 we used Task Manager to set the processor affinity for an application that had already been launched. But, what happens if you need to launch the application with a specific processor affinity? You could certainly use Method 2, but you might have to hunt around for a copy of IMAGECFG.EXE. Instead you can use PsExec to pre-set your processor affinity. We normally use PsExec to execute programs on remote systems, but we can also use it to set processor affinity. The syntax is as follows: PsExec.exe –a 0 <path to executable>. The –a switch sets the affinity of the process – in this case to Processor 0. This isn’t a permanent method of setting the affinity, although you could create a .cmd or .bat file that launched the executable using these parameters.
Method 4: Use the /AFFINITY switch (Windows Server 2003 and later)
You can also use the START command with the /AFFINITY switch to start the program with whichever processor affinity you wish to set. This is not a persistent method (unlike Method 2) – you would need to use the command each time, or launch the program via a batch file or a similar mechanism. The syntax for this command is as follows: START /AFFINITY <processor number> <program>. The processor number is defined by the same mask we used above in Method 2.
To verify that your process is started with the proper processor affinity, launch Task Manager once the process has launched. Right click on the process name and click on “Set Affinity” – the processor number that you set should be the one that is checked. Here’s an example from my Windows Server 2008 system – using Notepad as my test application I want to constrain Notepad to CPU 0.
Notepad launches successfully – now let’s check Task Manager:
And as we can see, Notepad was launched and is constrained to CPU 0.
With that we’ve come to the end of this post. As always – thanks for stopping by! Until next time …
|Share this post :|