SetProcessAffinityMask
Synopsis
BOOL SetProcessAffinityMask(
HANDLE hProcess,
DWORD_PTR dwProcessAffinityMask
);
Parameters
hProcess- Handle to the process whose affinity mask is to be set. The handle must have PROCESS_SET_INFORMATION access.
dwProcessAffinityMask- A bitmask that specifies the processors that the threads of the process are allowed to run on.
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. Call GetLastError for more information.
Remarks
- The affinity mask must be a subset of the system affinity mask retrieved by
GetProcessAffinityMask. - Setting a mask that excludes all processors results in the function failing with
ERROR_INVALID_PARAMETER. - Changing the affinity mask may affect thread scheduling and overall system performance.
Example
#include <windows.h>
#include <stdio.h>
int main() {
HANDLE h = GetCurrentProcess();
DWORD_PTR mask = 0x00000003; // Use CPU 0 and 1
if (SetProcessAffinityMask(h, mask)) {
printf("Affinity set successfully.\n");
} else {
printf("Failed: %lu\n", GetLastError());
}
return 0;
}