CPU_SET(3)

โœจ NAME โœจ

So bestie, gather round โ€” this is 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, and CPU_EQUAL_S โ€” and they are, collectively, macros for manipulating CPU sets ๐ŸŽ€

This one lives in the Library Functions Manual section 3, and it's about as iconic as it gets, fr fr.


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, link it with -lc). Periodt.


๐Ÿ”ง 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);
Yes you need that _GNU_SOURCE define, babe. It's literally right there at the top. Go read feature_test_macros(7) if you're curious ๐Ÿ’…

๐ŸŒธ DESCRIPTION ๐ŸŒธ

The cpu_set_t data structure represents a set of CPUs. These CPU sets are what sched_setaffinity(2) and similar interfaces reach for.

Here's the tea: cpu_set_t is implemented as a bit mask underneath. BUT โ€” and this is the part people get wrong โ€” you have to treat it as opaque. All manipulation of CPU sets should go through the macros on this page. Don't go poking at its insides, bestie, it's giving โœจprivateโœจ.

The basic macros ๐Ÿงš

These ones all operate on the CPU set called set:

  • CPU_ZERO() โ€” Clears set, so it contains no CPUs. A fresh start, a clean slate, main character energy.
  • CPU_SET() โ€” Add CPU cpu to set. Slay, you're in.
  • CPU_CLR() โ€” Remove CPU cpu from set. Not you anymore ๐Ÿ’€
  • CPU_ISSET() โ€” Test whether CPU cpu is a member of set.
  • CPU_COUNT() โ€” Return the number of CPUs in set.
โš ๏ธ important: wherever a cpu argument shows up, it should not produce side effects โ€” because these macros may evaluate the argument more than once. Don't be sneaky with cpu++ in there, I'm begging ๐Ÿ˜ญ

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. And ngl, you should make no assumptions about particular CPUs being available, or about the set of CPUs being contiguous โ€” because CPUs can be taken offline dynamically or just otherwise absent. Rude, but true.

The constant CPU_SETSIZE (currently 1024) specifies a value one greater than the maximum CPU number that can be stored in a cpu_set_t.

The logical-op macros ๐Ÿฆ‹

These ones do 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. It's giving "one of us but not both of us" ๐Ÿซถ
  • CPU_EQUAL() โ€” Test whether two CPU sets contain exactly the same CPUs.

๐Ÿ”ฅ Dynamically sized CPU sets ๐Ÿ”ฅ

Because some applications may require the ability to dynamically size CPU sets โ€” like, say, allocating sets bigger than the standard cpu_set_t data type can hold โ€” glibc nowadays provides a whole extra set of macros for exactly this. Iconic of them, honestly.

These are the alloc-and-free girlies:

  • 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 you can pass as the setsize argument in the CPU_*_S() macros described below.
  • CPU_FREE() โ€” Free a CPU set previously allocated by CPU_ALLOC().

And the ones whose names end in "_S"? They're the analogs of the similarly named macros without the suffix. Same tasks, same energy โ€” but they 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).)
  • CPU_ALLOC_SIZE() returns the number of bytes required to store a CPU set of the specified cardinality.
  • The other functions just... don't return a value. They simply are. ๐Ÿ’œ

๐ŸŒˆ STANDARDS ๐ŸŒˆ

Linux. That's it, that's the standard.


โญ HISTORY โญ

  • CPU_ZERO(), CPU_SET(), CPU_CLR(), and CPU_ISSET() 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() all first appeared in glibc 2.7. A whole era, honestly ๐Ÿ‘‘

๐Ÿ’– NOTES ๐Ÿ’–

  • Wanna duplicate a CPU set? Use memcpy(3). That's the move.
  • 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 those extra bits to be undefined. ๐Ÿ™ˆ
  • Don't let the names fool you โ€” despite the similarity, CPU_SETSIZE indicates the number of CPUs in the cpu_set_t data type (so it's effectively a count of the bits in the bit mask), while the setsize argument of the CPU_*_S() macros is a size in bytes. Two different vibes entirely, bestie.
  • The data types for arguments and return values shown in the SYNOPSIS are just hints about what's expected in each case. But because these interfaces are implemented as macros, the compiler won't necessarily catch all your type errors if you violate the suggestions. So like... be normal about it ๐Ÿ˜ญ

๐Ÿ’€ 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 โ€” phew โ€” but it 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. So update, bestie. โœจ


๐Ÿ“ EXAMPLES ๐Ÿ“

The following program demonstrates the use of some of the macros used for dynamically allocated CPU sets. It's lowkey a whole tutorial in 30 lines:

#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);
}

Notice it hops through every other CPU (cpu += 2) โ€” even-numbered CPUs only, she's picky like that ๐ŸŽ€


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘


okay bestie that's the whole page, go forth and affinity your threads ๐Ÿซถ this was Linux man-pages 6.20, dated 2026-02-08 โ€” mwah, take care of her ๐Ÿ’–โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms