开发者

Using Process.ProcessorAffinity on a system with 32+ logical cores

开发者 https://www.devze.com 2023-03-10 09:55 出处:网络
I\'m trying to set the processor affinity on a process on a machine that has 16 total physical processor cores, 32 logical. Before, we were using an int but that will overflow when you have 32 logical

I'm trying to set the processor affinity on a process on a machine that has 16 total physical processor cores, 32 logical. Before, we were using an int but that will overflow when you have 32 logical cores.

Will using a long, instead of an int, when setting the processor affinity still work?

See the code below.

 try
 {
    string pathToExe = GetPathToExe( jobType );

    long processorAffinity = DetermineProcessorAffinity();

    Process jobProcess = Process.Start( pathToExe, jobId.ToString() );
    if ( jobProcess != null )
    {
   开发者_开发百科    jobProcess.ProcessorAffinity = new IntPtr( processorAffinity );
    }

 }


Under the hood the ProcessorAffinity property will eventually call the Win32 method SetProcessAffinityMask.

  • http://msdn.microsoft.com/en-us/library/ms686223(v=vs.85).aspx

This method is intended to work with more than 32 processors and this solution should work.

Do be aware though that this approach doesn't work in a 32 bit process. In a 32 bit process the backing value of an IntPtr will still be 32 bits. The IntPtr constructor will silently truncate the long value into a int value and you`ll never be able to set the affinity more the extra processors.


From IntPtr documentation:

The IntPtr type is designed to be an integer whose size is platform-specific. That is, an instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems.

In other words: It should work on 64 bits systems, but not on 32 bit systems. I guess that's not a problem in your case.

0

精彩评论

暂无评论...
验证码 换一张
取 消