Unix-like On
Linux, the CPU affinity of a process can be altered with the taskset(1) program and the sched_setaffinity(2) system call. The affinity of a thread can be altered with one of the library functions: pthread_setaffinity_np(3) or pthread_attr_setaffinity_np(3). On
SGI systems, dplace binds a process to a set of CPUs. On
NetBSD 5.0,
FreeBSD 7.2,
DragonFly BSD 4.7 and later versions can use pthread_setaffinity_np and pthread_getaffinity_np. In
NetBSD, the psrset utility to set a thread's affinity to a certain CPU set. In
FreeBSD, cpuset utility is used to create CPU sets and to assign processes to these sets. On
DragonFly BSD 1.9 (2007) and later versions, usched_set system call can be used to control the affinity of a process. In
DragonFly BSD 3.1 (2012) and later, usched utility can be used for assigning processes to a certain CPU set. On
Solaris it is possible to control bindings of processes and LWPs to processor using the pbind(1) program. To control the affinity programmatically processor_bind(2) can be used. There are more generic interfaces available such as pset_bind(2) or lgrp_affinity_get(3LGRP) using processor set and locality groups concepts. On
AIX it is possible to control bindings of processes using the bindprocessor command and the bindprocessor() API. The AIX scheduler is SMT-aware and is able to switch the SMT states of the POWER7/8/9 cores from 1 to 8 threads to maximize throughput.
macOS macOS does not offer an API that manages the set of processors a process, task, or thread is allowed to run on. Instead it offers the Thread Affinity API, which tells the kernel which threads should be scheduled to share the same L2 cache, i.e. run on the same physical CPU core. The
XNU kernel internally translates each affinity tag to a set of allowed logical cores corresponding to a physical core. When a tag is set, it creates a Thread Affinity namespace when there is not one already. It then becomes bound to the core with the fewest tags already bound. A tag do not migrate between cores in XNU version 8792; as a result, so long as there are not more tags than there are physical cores, each tag will correspond to exactly one physical core. Namespaces as well as tags are inherited between parent and child processes. The API is not available on arm64 (Apple Silicon), where is hardcoded to return 0.
Windows NT On
Windows NT and its successors, thread and process CPU affinities can be set separately by using SetThreadAffinityMask and SetProcessAffinityMask API calls or via the Task Manager interface (for process affinity only). Forcing of each
OpenMP thread to distinct logical cores in Windows can be accomplished by means of the following
C code, with the Windows.h| header: • include • include // Set OpenMP thread affinity void setThreadAffinity(void) { #pragma omp parallel default(shared) { DWORD_PTR mask = (DWORD_PTR)1 == See also ==