Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
 SetProcessAffinityMask Function
SetProcessAffinityMask Function

Sets a processor affinity mask for the threads of the specified process.

Syntax

BOOL WINAPI SetProcessAffinityMask(
  __in  HANDLE hProcess,
  __in  DWORD_PTR dwProcessAffinityMask
);

Parameters

hProcess [in]

A handle to the process whose affinity mask is to be set. This handle must have the PROCESS_SET_INFORMATION access right. For more information, see Process Security and Access Rights.

dwProcessAffinityMask [in]

The affinity mask for the threads of the process.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

A process affinity mask is a bit vector in which each bit represents the processor on which the threads of the process are allowed to run.

The value of the process affinity mask must be a subset of the system affinity mask values obtained by the GetProcessAffinityMask function.

Do not call SetProcessAffinityMask in a DLL that may be called by processes other than your own.

Process affinity is inherited by any child process or newly instantiated local process.

Requirements

Client Requires Windows Vista, Windows XP, or Windows 2000 Professional.
Server Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
Header

Declared in Winbase.h; include Windows.h.

Library

Use Kernel32.lib.

DLL

Requires Kernel32.dll.

See Also

CreateProcess
GetProcessAffinityMask
Multiple Processors
Process and Thread Functions
Processes


Send comments about this topic to Microsoft

Build date: 8/7/2008

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
How to set affinity only to your real processors when Hyper-Thread is on      opedroso   |   Edit   |  

The following code sample comes from "Juice Up Your App with the Power of Hyper-Threading", an MSDN article:

public void SetProcessAffinityToPhysicalCPUForHyperthreadOnly(int processid)
{
int res;
int hProcess;
int ProcAffinityMask = 0, SysAffinityMask = 0;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, processid);
res = GetProcessAffinityMask(
hProcess, ref ProcAffinityMask, ref SysAffinityMask);
if (SysAffinityMask == 3) // 1 proc, 2 logical CPUs
res = SetProcessAffinityMask(hProcess, 1);
else if (SysAffinityMask == 15) //dual proc, 4 virtual CPUs
res = SetProcessAffinityMask(hProcess, 3);
res = CloseHandle(hProcess);
}

From the sample above, we see that the affinity mask is such that all physical processors come first in the mask, then the logical (Hyper-threaded) processors.

If your process is a heavy user of floating point instructions, setting the affinity mask to (number of processors/2) - 1 will make sure your threads will give preference for the physical processors which have FPU.

For a sample on how to detect if Hyper-Thread is on in C/C++, you could use this:

__inline BOOL hyperThreadingOn()
{
DWORD rEbx, rEdx;
__asm {
push eax // save registers used
push ebx
push ecx
push edx
xor eax,eax // cpuid(1)
add al, 0x01
_emit 0x0F
_emit 0xA2
mov rEdx, edx // Features Flags, bit 28 indicates if HTT (Hyper-Thread Technology) is
// available, but not if it is on; if on, Count of logical processors > 1.
mov rEbx, ebx // Bits 23-16: Count of logical processors.
// Valid only if Hyper-Threading Technology flag is set.
pop edx // restore registers used
pop ecx
pop ebx
pop eax
}
return (rEdx & (1<<28)) && (((rEbx & 0x00FF0000) >> 16) > 1);
}

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker