Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dedicate the CPU to run your process on 1 core using C++ [closed]

Tags:

c++

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++?

like image 258
Naif Avatar asked Nov 29 '13 15:11

Naif


1 Answers

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

    }
like image 193
ckv Avatar answered Oct 14 '22 23:10

ckv