Method: Process.get_affinity
- Defined in:
- lib/win32/process.rb
.get_affinity(int = Process.pid) ⇒ Object
Returns the process and system affinity mask for the given pid, or the current process if no pid is provided. The return value is a two element array, with the first containing the process affinity mask, and the second containing the system affinity mask. Both are decimal values.
A process affinity mask is a bit vector indicating the processors that a process is allowed to run on. A system affinity mask is a bit vector in which each bit represents the processors that are configured into a system.
Example:
# System has 4 processors, current process is allowed to run on all.
Process.get_affinity # => [[15], [15]], where '15' is 1 + 2 + 4 + 8
# System has 4 processors, current process only allowed on 1 and 4.
Process.get_affinity # => [[9], [15]]
If you want to convert a decimal bit vector into an array of 0’s and 1’s indicating the flag value of each processor, you can use something like this approach:
mask = Process.get_affinity.first
(0..mask).to_a.map{ |n| mask[n] }
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/win32/process.rb', line 51 def get_affinity(int = Process.pid) pmask = FFI::MemoryPointer.new(:ulong) smask = FFI::MemoryPointer.new(:ulong) if int == Process.pid unless GetProcessAffinityMask(GetCurrentProcess(), pmask, smask) raise SystemCallError, FFI.errno, "GetProcessAffinityMask" end else begin handle = OpenProcess(PROCESS_QUERY_INFORMATION, 0 , int) if handle == 0 raise SystemCallError, FFI.errno, "OpenProcess" end unless GetProcessAffinityMask(handle, pmask, smask) raise SystemCallError, FFI.errno, "GetProcessAffinityMask" end ensure CloseHandle(handle) end end [pmask.read_ulong, smask.read_ulong] end |