2

We have a C++ COM server application, from which user can select and launch their own processes. I want to add 'Processor affinity set' feature in the launcher application.

The customer has a multi CPU system (2 Physical CPU). User will give the physical CPU number , either 1 or 2. The application launcher need to set the processor affinity for their applications based on user's input. if they give CPU number as 2, then the applications that are launched from the launcher should use CPU 2.

How to calculate processor affinity mask (based on the CPU)?

This launcher application should work in Windows XP 32 bit and Windows7 64 bit machines. Also the processor type may vary from machine to machine (Intel , AMD, or others).

The application is developed as a COM server (C++).

Please provide me a good solution.

2

1 Answer 1

1

Not sure your question would like to get number of CPU or set it

//To get the CPU
HANDLE hProcess = GetCurrentProcess();
SYSTEM_INFO SystemInfo;
GetSystemInfo(&SystemInfo);
unsigned int nMaxProcessorMask = (1 << SystemInfo.dwNumberOfProcessors) - 1;
// Set the max processor mask
SetProcessAffinityMask(hProcess, nMaxProcessorMask);
2
  • As per MSDN, process affinity mask is a bit vector in which each bit represents a logical processor on which the threads of the process are allowed to run.
    – aathi
    Dec 11, 2015 at 8:10
  • As per MSDN, process affinity mask is a bit vector in which each bit represents a logical processor on which the threads of the process are allowed to run. But I need to set process affinity mask as a bit vector of the logical processors for the particular physical CPU. i.e. if the machine contains two physical CPU (say CPU 1 and CPU 2). I need to set the affinity mask on which the bits represents only for the logical processors of CPU 2.
    – aathi
    Dec 11, 2015 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.