---
name: "CPU_SET"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/CPU_SET"
---

# CPU\_SET(3)

macros for manipulating CPU sets

## NAME

CPU\_SET, CPU\_CLR, CPU\_ISSET, CPU\_ZERO, CPU\_COUNT, CPU\_AND, CPU\_OR, CPU\_XOR, CPU\_EQUAL, CPU\_ALLOC, CPU\_ALLOC\_SIZE, CPU\_FREE, CPU\_SET\_S, CPU\_CLR\_S, CPU\_ISSET\_S, CPU\_ZERO\_S, CPU\_COUNT\_S, CPU\_AND\_S, CPU\_OR\_S, CPU\_XOR\_S, CPU\_EQUAL\_S - macros for manipulating CPU sets

## LIBRARY

Standard C library (*libc*, *-lc*)

## SYNOPSIS

```
#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <sched.h>
```

**void CPU\_ZERO(cpu\_set\_t \****set***);**

**void CPU\_SET(int** *cpu***, cpu\_set\_t \****set***);** **void CPU\_CLR(int** *cpu***, cpu\_set\_t \****set***);** **int  CPU\_ISSET(int** *cpu***, cpu\_set\_t \****set***);**

**int  CPU\_COUNT(cpu\_set\_t \****set***);**

**void CPU\_AND(cpu\_set\_t \****destset***,**              **cpu\_set\_t \****srcset1***, cpu\_set\_t \****srcset2***);** **void CPU\_OR(cpu\_set\_t \****destset***,**              **cpu\_set\_t \****srcset1***, cpu\_set\_t \****srcset2***);** **void CPU\_XOR(cpu\_set\_t \****destset***,**              **cpu\_set\_t \****srcset1***, cpu\_set\_t \****srcset2***);**

**int  CPU\_EQUAL(cpu\_set\_t \****set1***, cpu\_set\_t \****set2***);**

**cpu\_set\_t \*CPU\_ALLOC(int** *num\_cpus***);** **void CPU\_FREE(cpu\_set\_t \****set***);** **size\_t CPU\_ALLOC\_SIZE(int** *num\_cpus***);**

**void CPU\_ZERO\_S(size\_t** *setsize***, cpu\_set\_t \****set***);**

**void CPU\_SET\_S(int** *cpu***, size\_t** *setsize***, cpu\_set\_t \****set***);** **void CPU\_CLR\_S(int** *cpu***, size\_t** *setsize***, cpu\_set\_t \****set***);** **int  CPU\_ISSET\_S(int** *cpu***, size\_t** *setsize***, cpu\_set\_t \****set***);**

**int  CPU\_COUNT\_S(size\_t** *setsize***, cpu\_set\_t \****set***);**

**void CPU\_AND\_S(size\_t** *setsize***, cpu\_set\_t \****destset***,**              **cpu\_set\_t \****srcset1***, cpu\_set\_t \****srcset2***);** **void CPU\_OR\_S(size\_t** *setsize***, cpu\_set\_t \****destset***,**              **cpu\_set\_t \****srcset1***, cpu\_set\_t \****srcset2***);** **void CPU\_XOR\_S(size\_t** *setsize***, cpu\_set\_t \****destset***,**              **cpu\_set\_t \****srcset1***, cpu\_set\_t \****srcset2***);**

**int  CPU\_EQUAL\_S(size\_t** *setsize***, cpu\_set\_t \****set1***, cpu\_set\_t \****set2***);**

## DESCRIPTION

The *cpu\_set\_t* data structure represents a set of CPUs. CPU sets are used by [**sched\_setaffinity**(2)](/sched_setaffinity/2.md) and similar interfaces.

The *cpu\_set\_t* data type is implemented as a bit mask. However, the data structure should be treated as opaque: all manipulation of CPU sets should be done via the macros described in this page.

The following macros are provided to operate on the CPU set *set*:

- **CPU\_ZERO**()

  Clears *set*, so that it contains no CPUs.

- **CPU\_SET**()

  Add CPU *cpu* to *set*.

- **CPU\_CLR**()

  Remove CPU *cpu* from *set*.

- **CPU\_ISSET**()

  Test to see if CPU *cpu* is a member of *set*.

- **CPU\_COUNT**()

  Return the number of CPUs in *set*.

Where a *cpu* argument is specified, it should not produce side effects, since the above macros may evaluate the argument more than once.

The first CPU on the system corresponds to a *cpu* value of 0, the next CPU corresponds to a *cpu* value of 1, and so on. No assumptions should be made about particular CPUs being available, or the set of CPUs being contiguous, since CPUs can be taken offline dynamically or be otherwise absent. The constant **CPU\_SETSIZE** (currently 1024) specifies a value one greater than the maximum CPU number that can be stored in *cpu\_set\_t*.

The following macros perform logical operations on CPU sets:

- **CPU\_AND**()

  Store the intersection of the sets *srcset1* and *srcset2* in *destset* (which may be one of the source sets).

- **CPU\_OR**()

  Store the union of the sets *srcset1* and *srcset2* in *destset* (which may be one of the source sets).

- **CPU\_XOR**()

  Store the XOR of the sets *srcset1* and *srcset2* in *destset* (which may be one of the source sets). The XOR means the set of CPUs that are in either *srcset1* or *srcset2*, but not both.

- **CPU\_EQUAL**()

  Test whether two CPU set contain exactly the same CPUs.

### Dynamically sized CPU sets

Because some applications may require the ability to dynamically size CPU sets (e.g., to allocate sets larger than that defined by the standard *cpu\_set\_t* data type), glibc nowadays provides a set of macros to support this.

The following macros are used to allocate and deallocate CPU sets:

- **CPU\_ALLOC**()

  Allocate a CPU set large enough to hold CPUs in the range 0 to *num\_cpus-1*.

- **CPU\_ALLOC\_SIZE**()

  Return the size in bytes of the CPU set that would be needed to hold CPUs in the range 0 to *num\_cpus-1*. This macro provides the value that can be used for the *setsize* argument in the **CPU\_\*\_S**() macros described below.

- **CPU\_FREE**()

  Free a CPU set previously allocated by **CPU\_ALLOC**().

The macros whose names end with "\_S" are the analogs of the similarly named macros without the suffix. These macros perform the same tasks as their analogs, but operate on the dynamically allocated CPU set(s) whose size is *setsize* bytes.

## RETURN VALUE

**CPU\_ISSET**() and **CPU\_ISSET\_S**() return nonzero if *cpu* is in *set*; otherwise, it returns 0.

**CPU\_COUNT**() and **CPU\_COUNT\_S**() return the number of CPUs in *set*.

**CPU\_EQUAL**() and **CPU\_EQUAL\_S**() return nonzero if the two CPU sets are equal; otherwise, they return 0.

**CPU\_ALLOC**() returns a pointer on success, or NULL on failure. (Errors are as for [**malloc**(3)](/malloc/3.md).)

**CPU\_ALLOC\_SIZE**() returns the number of bytes required to store a CPU set of the specified cardinality.

The other functions do not return a value.

## STANDARDS

Linux.

## HISTORY

The **CPU\_ZERO**(), **CPU\_SET**(), **CPU\_CLR**(), and **CPU\_ISSET**() macros were added in glibc 2.3.3.

**CPU\_COUNT**() first appeared in glibc 2.6.

**CPU\_AND**(), **CPU\_OR**(), **CPU\_XOR**(), **CPU\_EQUAL**(), **CPU\_ALLOC**(), **CPU\_ALLOC\_SIZE**(), **CPU\_FREE**(), **CPU\_ZERO\_S**(), **CPU\_SET\_S**(), **CPU\_CLR\_S**(), **CPU\_ISSET\_S**(), **CPU\_AND\_S**(), **CPU\_OR\_S**(), **CPU\_XOR\_S**(), and **CPU\_EQUAL\_S**() first appeared in glibc 2.7.

## NOTES

To duplicate a CPU set, use [**memcpy**(3)](/memcpy/3.md).

Since CPU sets are bit masks allocated in units of long words, the actual number of CPUs in a dynamically allocated CPU set will be rounded up to the next multiple of *sizeof(unsigned long)*. An application should consider the contents of these extra bits to be undefined.

Notwithstanding the similarity in the names, note that the constant **CPU\_SETSIZE** indicates the number of CPUs in the *cpu\_set\_t* data type (thus, it is effectively a count of the bits in the bit mask), while the *setsize* argument of the **CPU\_\*\_S**() macros is a size in bytes.

The data types for arguments and return values shown in the SYNOPSIS are hints what about is expected in each case. However, since these interfaces are implemented as macros, the compiler won't necessarily catch all type errors if you violate the suggestions.

## BUGS

On 32-bit platforms with glibc 2.8 and earlier, **CPU\_ALLOC**() allocates twice as much space as is required, and **CPU\_ALLOC\_SIZE**() returns a value twice as large as it should. This bug should not affect the semantics of a program, but does result in wasted memory and less efficient operation of the macros that operate on dynamically allocated CPU sets. These bugs are fixed in glibc 2.9.

## EXAMPLES

The following program demonstrates the use of some of the macros used for dynamically allocated CPU sets.

```
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
int
main(int argc, char *argv[])
{
    cpu_set_t *cpusetp;
    size_t size, num_cpus;
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <num-cpus>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    num_cpus = atoi(argv[1]);
    cpusetp = CPU_ALLOC(num_cpus);
    if (cpusetp == NULL) {
        perror("CPU_ALLOC");
        exit(EXIT_FAILURE);
    }
    size = CPU_ALLOC_SIZE(num_cpus);
    CPU_ZERO_S(size, cpusetp);
    for (size_t cpu = 0; cpu < num_cpus; cpu += 2)
        CPU_SET_S(cpu, size, cpusetp);
    printf("CPU_COUNT() of set:    %d\n", CPU_COUNT_S(size, cpusetp));
    CPU_FREE(cpusetp);
    exit(EXIT_SUCCESS);
}
```

## SEE ALSO

[**sched\_setaffinity**(2)](/sched_setaffinity/2.md), [**pthread\_attr\_setaffinity\_np**(3)](/pthread_attr_setaffinity_np/3.md), [**pthread\_setaffinity\_np**(3)](/pthread_setaffinity_np/3.md), [**cpuset**(7)](/cpuset/7.md)
