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
.
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.
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