---
name: "cpuset_getaffinity"
section: "2"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/cpuset_getaffinity"
---

# cpuset\_getaffinity(2)

manage CPU affinity

## NAME

**cpuset\_getaffinity**, **cpuset\_setaffinity** – manage CPU affinity

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/cpuset.h\>**

*int*\

**cpuset\_getaffinity**(*cpulevel\_t level*, *cpuwhich\_t which*, *id\_t id*, *size\_t setsize*, *cpuset\_t \*mask*);

*int*\

**cpuset\_setaffinity**(*cpulevel\_t level*, *cpuwhich\_t which*, *id\_t id*, *size\_t setsize*, *const cpuset\_t \*mask*);

## DESCRIPTION

**cpuset\_getaffinity**() and **cpuset\_setaffinity**() allow the manipulation of sets of CPUs available to processes, threads, interrupts, jails and other resources. These functions may manipulate sets of CPUs that contain many processes or per-object anonymous masks that effect only a single object.

The valid values for the *level* and *which* arguments are documented in [cpuset(2)](/cpuset/2.md). These arguments specify which object and which set of the object we are referring to. Not all possible combinations are valid. For example, only processes may belong to a numbered set accessed by a *level* argument of CPU\_LEVEL\_CPUSET. All resources, however, have a mask which may be manipulated with CPU\_LEVEL\_WHICH.

Masks of type *cpuset\_t* are composed using the CPU\_SET macros. If the user-supplied mask is not large enough to fit all of the matching CPUs, **cpuset\_getaffinity**() fails with ERANGE. Calls to **cpuset\_setaffinity**() tolerate masks of any size with no restrictions. The kernel uses the meaningful part of the mask, where the upper bound is the maximum CPU id present in the system. If bits for non-existing CPUs are set, calls to **cpuset\_setaffinity**() fail with EINVAL.

The supplied mask should have a size of *setsize* bytes. This size is usually provided by calling sizeof(mask) which is ultimately determined by the value of CPU\_SETSIZE as defined in ⟨sys/cpuset.h⟩.

**cpuset\_getaffinity**() retrieves the mask from the object specified by *level*, *which* and *id* and stores it in the space provided by *mask*.

**cpuset\_setaffinity**() attempts to set the mask for the object specified by *level*, *which* and *id* to the value in *mask*.

## RETURN VALUES

The **cpuset\_getaffinity()** function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

## ERRORS

The following error codes may be set in *errno*:

- \[EINVAL\]

  The *level* or *which* argument was not a valid value.

- \[EINVAL\]

  The *mask* argument specified when calling **cpuset\_setaffinity**() was not a valid value.

- \[EDEADLK\]

  The **cpuset\_setaffinity**() call would leave a thread without a valid CPU to run on because the set does not overlap with the thread's anonymous mask.

- \[EFAULT\]

  The mask pointer passed was invalid.

- \[ESRCH\]

  The object specified by the *id* and *which* arguments could not be found.

- \[ERANGE\]

  The *cpusetsize* was smaller than needed to fit all of the matching CPUs.

- \[EPERM\]

  The calling process did not have the credentials required to complete the operation.

- \[ECAPMODE\]

  The calling process attempted to act on a process other than itself, while in capability mode. See [capsicum(4)](/capsicum/4.md).

## SEE ALSO

[cpuset(1)](/cpuset/1.md), [cpuset(2)](/cpuset/2.md), [cpuset\_getdomain(2)](/cpuset_getdomain/2.md), [cpuset\_getid(2)](/cpuset_getid/2.md), [cpuset\_setdomain(2)](/cpuset_setdomain/2.md), [cpuset\_setid(2)](/cpuset_setid/2.md), [pthread\_affinity\_np(3)](/pthread_affinity_np/3.md), [pthread\_attr\_affinity\_np(3)](/pthread_attr_affinity_np/3.md), [capsicum(4)](/capsicum/4.md), [cpuset(9)](/cpuset/9.md)

## HISTORY

The **cpuset\_getaffinity** family of system calls first appeared in FreeBSD 7.1.

## AUTHORS

Jeffrey Roberson ⟨*jeff@FreeBSD.org*⟩
