An application that is multi-threaded or an application that creates threads may have a contention problem on a multi-processor machine. The threads may actually be running at the same time on different processors. By binding a process to a cpu, no two threads are running at the same time and are not interfering with one another. Your application will run as if on a single-processor machine.
Binding on NT
You can bind a process to a cpu either from the Task Manager, using Win32
calls or permanently setting the processor affinity for an application executable.
(You need NT Administrator privileges to bind a process on NT.)
Binding from the Task Manager
1. Select the "Processes" tab in Task Manager.
2. Right mouse click on the process you want to bind and select "Set Affinity".
3. Select the cpu you want your application to run in.
Binding Using Win32 Calls
Use the SetProcessAffinityMask() function to bind a particular application to a cpu. The function is in the kernel32.dll. The first parameter is a handle to a process. Use the GetCurrentProcess to get the handle. The second parameter describes which processors the process should run on.
Declare the functions as follows:
function ulong GetCurrentProcess() library "kernel32.dll"
function boolean SetProcessAffinityMask(ulong hnd, ulong lpProcessAffinityMask) library "kernel32.dll"
// 0123 – As they appear in Task Manager (CPU 0, CPU 1, CPU 2, CPU 3)
ai_processor = 2
CHOOSE CASE ai_processor
lul_lpProcessAffinityMask = 1
lul_lpProcessAffinityMask = 2
lul_lpProcessAffinityMask = 4
lul_lpProcessAffinityMask = 8
lul_process = GetCurrentProcess( )
b_result = SetProcessAffinityMask(lul_process, lul_lpProcessAffinityMask )
Permanently Setting the Processor Affinity for an Application Executable
On Windows NT, Windows 2000 or Windows XP, you can permanently set the
processor affinity for an application executable by
using the imagecfg.exe tool. This tool is located in the \support\debug\i386 folder on a Windows NT cdrom and from the Windows 2000 or Windows XP Server Resource Kit Supplement. Open a DOS prompt and enter in the following:
imagecfg -a 0xn c:\program files\sybase\Jaguar CTS\bin\jagsrv.exe
where 0xn is the affinity mask and maps to a cpu as follows:
Binding on Solaris
You can bind a process to a cpu at the command line or using a shell script.
Binding at the command line
1. Use psrinfo to find the processor id # you want to bind to
2. pbind –b <processor id #> <pid>
Binding using a shell script
/usr/sbin/pbind –b 1 $!