I working on a project that measure the effect of some code pattern on the CPU. To do this I need to run my process on the CPU and stop all other processes on the CPU to see the real effect of my process.
Also I need to run my process on 1 core of the CPU. Can anyone help how to do this in C++?
You can set processor affinity for the process. When you do that your process will only run in that CPU. So you can measure the performance of your process. This is how i have done for a service in VC++. Hope this is helpful.
SYSTEM_INFO SystemInfo;
GetSystemInfo(&SystemInfo);
HANDLE hProcess = GetCurrentProcess();
if(SystemInfo.dwNumberOfProcessors >1)
{
//DWORD dwProcessAffinityMask, dwSystemAffinityMask;
//GetProcessAffinityMask( hProcess, &dwProcessAffinityMask, &dwSystemAffinityMask );
//SetProcessAffinityMask( hProcess, 1L );// use CPU 0 only
//SetProcessAffinityMask( hProcess, 2L );// use CPU 1 only
//SetProcessAffinityMask( hProcess, 3L );// allow running on both CPUs
SetProcessAffinityMask( hProcess, 2L );// use CPU 1 only
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With