NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |STANDARDS |HISTORY |NOTES |BUGS |EXAMPLES |SEE ALSO |COLOPHON | |
CPU_SET(3) Library Functions ManualCPU_SET(3)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
Standard C library (libc,-lc)
#define _GNU_SOURCE/* See feature_test_macros(7) */#include <sched.h>void CPU_ZERO(cpu_set_t *set);void CPU_SET(intcpu, cpu_set_t *set);void CPU_CLR(intcpu, cpu_set_t *set);int CPU_ISSET(intcpu, 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(intnum_cpus);void CPU_FREE(cpu_set_t *set);size_t CPU_ALLOC_SIZE(intnum_cpus);void CPU_ZERO_S(size_tsetsize, cpu_set_t *set);void CPU_SET_S(intcpu, size_tsetsize, cpu_set_t *set);void CPU_CLR_S(intcpu, size_tsetsize, cpu_set_t *set);int CPU_ISSET_S(intcpu, size_tsetsize, cpu_set_t *set);int CPU_COUNT_S(size_tsetsize, cpu_set_t *set);void CPU_AND_S(size_tsetsize, cpu_set_t *destset,cpu_set_t *srcset1, cpu_set_t *srcset2);void CPU_OR_S(size_tsetsize, cpu_set_t *destset,cpu_set_t *srcset1, cpu_set_t *srcset2);void CPU_XOR_S(size_tsetsize, cpu_set_t *destset,cpu_set_t *srcset1, cpu_set_t *srcset2);int CPU_EQUAL_S(size_tsetsize, cpu_set_t *set1, cpu_set_t *set2);
Thecpu_set_t data structure represents a set of CPUs. CPU sets are used bysched_setaffinity(2) and similar interfaces. Thecpu_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 setset:CPU_ZERO() Clearsset, so that it contains no CPUs.CPU_SET() Add CPUcpu toset.CPU_CLR() Remove CPUcpu fromset.CPU_ISSET() Test to see if CPUcpu is a member ofset.CPU_COUNT() Return the number of CPUs inset. Where acpu 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 acpu value of 0, the next CPU corresponds to acpu 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 constantCPU_SETSIZE(currently 1024) specifies a value one greater than the maximum CPU number that can be stored incpu_set_t. The following macros perform logical operations on CPU sets:CPU_AND() Store the intersection of the setssrcset1 andsrcset2 indestset (which may be one of the source sets).CPU_OR() Store the union of the setssrcset1 andsrcset2 indestset (which may be one of the source sets).CPU_XOR() Store the XOR of the setssrcset1 andsrcset2 indestset (which may be one of the source sets). The XOR means the set of CPUs that are in eithersrcset1 orsrcset2, 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 standardcpu_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 tonum_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 tonum_cpus-1. This macro provides the value that can be used for thesetsize argument in theCPU_*_S() macros described below.CPU_FREE() Free a CPU set previously allocated byCPU_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 issetsize bytes.
CPU_ISSET() andCPU_ISSET_S() return nonzero ifcpu is inset; otherwise, it returns 0.CPU_COUNT() andCPU_COUNT_S() return the number of CPUs inset.CPU_EQUAL() andCPU_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 formalloc(3).)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.
Linux.
TheCPU_ZERO(),CPU_SET(),CPU_CLR(), andCPU_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(), andCPU_EQUAL_S() first appeared in glibc 2.7.
To duplicate a CPU set, usememcpy(3). 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 ofsizeof(unsigned long). An application should consider the contents of these extra bits to be undefined. Notwithstanding the similarity in the names, note that the constantCPU_SETSIZEindicates the number of CPUs in thecpu_set_t data type (thus, it is effectively a count of the bits in the bit mask), while thesetsize argument of theCPU_*_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.
On 32-bit platforms with glibc 2.8 and earlier,CPU_ALLOC() allocates twice as much space as is required, andCPU_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.
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); }sched_setaffinity(2),pthread_attr_setaffinity_np(3),pthread_setaffinity_np(3),cpuset(7)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17CPU_SET(3)Pages that refer to this page:sched_setaffinity(2), pthread_attr_setaffinity_np(3), pthread_setaffinity_np(3), tracefs_instance_set_affinity(3), cpuset(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |