1 .\" Copyright (C) 2006 Michael Kerrisk
2 .\" and Copyright (C) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"*******************************************************************
27 .\" This file was generated with po4a. Translate the source file.
29 .\"*******************************************************************
30 .TH CPU_SET 3 2012\-03\-15 Linux "Linux Programmer's Manual"
32 CPU_SET, CPU_CLR, CPU_ISSET, CPU_ZERO, CPU_COUNT, CPU_AND, CPU_OR, CPU_XOR,
33 CPU_EQUAL, CPU_ALLOC, CPU_ALLOC_SIZE, CPU_FREE, CPU_SET_S, CPU_CLR_S,
34 CPU_ISSET_S, CPU_ZERO_S, CPU_COUNT_S, CPU_AND_S, CPU_OR_S, CPU_XOR_S,
35 CPU_EQUAL_S \- macros for manipulating CPU sets
38 \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */
39 \fB#include <sched.h>\fP
41 \fBvoid CPU_ZERO(cpu_set_t *\fP\fIset\fP\fB);\fP
43 \fBvoid CPU_SET(int \fP\fIcpu\fP\fB, cpu_set_t *\fP\fIset\fP\fB);\fP
44 \fBvoid CPU_CLR(int \fP\fIcpu\fP\fB, cpu_set_t *\fP\fIset\fP\fB);\fP
45 \fBint CPU_ISSET(int \fP\fIcpu\fP\fB, cpu_set_t *\fP\fIset\fP\fB);\fP
47 \fBint CPU_COUNT(cpu_set_t *\fP\fIset\fP\fB);\fP
49 \fBvoid CPU_AND(cpu_set_t *\fP\fIdestset\fP\fB,\fP
50 \fB cpu_set_t *\fP\fIsrcset1\fP\fB, cpu_set_t *\fP\fIsrcset2\fP\fB);\fP
51 \fBvoid CPU_OR(cpu_set_t *\fP\fIdestset\fP\fB,\fP
52 \fB cpu_set_t *\fP\fIsrcset1\fP\fB, cpu_set_t *\fP\fIsrcset2\fP\fB);\fP
53 \fBvoid CPU_XOR(cpu_set_t *\fP\fIdestset\fP\fB,\fP
54 \fB cpu_set_t *\fP\fIsrcset1\fP\fB, cpu_set_t *\fP\fIsrcset2\fP\fB);\fP
56 \fBint CPU_EQUAL(cpu_set_t *\fP\fIset1\fP\fB, cpu_set_t *\fP\fIset2\fP\fB);\fP
58 \fBcpu_set_t *CPU_ALLOC(int \fP\fInum_cpus\fP\fB);\fP
59 \fBvoid CPU_FREE(cpu_set_t *\fP\fIset\fP\fB);\fP
60 \fBsize_t CPU_ALLOC_SIZE(int \fP\fInum_cpus\fP\fB);\fP
62 \fBvoid CPU_ZERO_S(size_t \fP\fIsetsize\fP\fB, cpu_set_t *\fP\fIset\fP\fB);\fP
64 \fBvoid CPU_SET_S(int \fP\fIcpu\fP\fB, size_t \fP\fIsetsize\fP\fB, cpu_set_t *\fP\fIset\fP\fB);\fP
65 \fBvoid CPU_CLR_S(int \fP\fIcpu\fP\fB, size_t \fP\fIsetsize\fP\fB, cpu_set_t *\fP\fIset\fP\fB);\fP
66 \fBint CPU_ISSET_S(int \fP\fIcpu\fP\fB, size_t \fP\fIsetsize\fP\fB, cpu_set_t *\fP\fIset\fP\fB);\fP
68 \fBint CPU_COUNT_S(size_t \fP\fIsetsize\fP\fB, cpu_set_t *\fP\fIset\fP\fB);\fP
70 \fBvoid CPU_AND_S(size_t \fP\fIsetsize\fP\fB, cpu_set_t *\fP\fIdestset\fP\fB,\fP
71 \fB cpu_set_t *\fP\fIsrcset1\fP\fB, cpu_set_t *\fP\fIsrcset2\fP\fB);\fP
72 \fBvoid CPU_OR_S(size_t \fP\fIsetsize\fP\fB, cpu_set_t *\fP\fIdestset\fP\fB,\fP
73 \fB cpu_set_t *\fP\fIsrcset1\fP\fB, cpu_set_t *\fP\fIsrcset2\fP\fB);\fP
74 \fBvoid CPU_XOR_S(size_t \fP\fIsetsize\fP\fB, cpu_set_t *\fP\fIdestset\fP\fB,\fP
75 \fB cpu_set_t *\fP\fIsrcset1\fP\fB, cpu_set_t *\fP\fIsrcset2\fP\fB);\fP
77 \fBint CPU_EQUAL_S(size_t \fP\fIsetsize\fP\fB, cpu_set_t *\fP\fIset1\fP\fB, cpu_set_t *\fP\fIset2\fP\fB);\fP
80 The \fIcpu_set_t\fP data structure represents a set of CPUs. CPU sets are used
81 by \fBsched_setaffinity\fP(2) and similar interfaces.
83 The \fIcpu_set_t\fP data type is implemented as a bitset. However, the data
84 structure treated as considered opaque: all manipulation of CPU sets should
85 be done via the macros described in this page.
87 The following macros are provided to operate on the CPU set \fIset\fP:
90 Clears \fIset\fP, so that it contains no CPUs.
93 Add CPU \fIcpu\fP to \fIset\fP.
96 Remove CPU \fIcpu\fP from \fIset\fP.
99 Test to see if CPU \fIcpu\fP is a member of \fIset\fP.
102 Return the number of CPUs in \fIset\fP.
104 Where a \fIcpu\fP argument is specified, it should not produce side effects,
105 since the above macros may evaluate the argument more than once.
107 The first available CPU on the system corresponds to a \fIcpu\fP value of 0,
108 the next CPU corresponds to a \fIcpu\fP value of 1, and so on. The constant
109 \fBCPU_SETSIZE\fP (currently 1024) specifies a value one greater than the
110 maximum CPU number that can be stored in \fIcpu_set_t\fP.
112 The following macros perform logical operations on CPU sets:
115 Store the intersection of the sets \fIsrcset1\fP and \fIsrcset2\fP in \fIdestset\fP
116 (which may be one of the source sets).
119 Store the union of the sets \fIsrcset1\fP and \fIsrcset2\fP in \fIdestset\fP (which
120 may be one of the source sets).
123 Store the XOR of the sets \fIsrcset1\fP and \fIsrcset2\fP in \fIdestset\fP (which may
124 be one of the source sets). The XOR means the set of CPUs that are in
125 either \fIsrcset1\fP or \fIsrcset2\fP, but not both.
128 Test whether two CPU set contain exactly the same CPUs.
129 .SS "Dynamically sized CPU sets"
130 Because some applications may require the ability to dynamically size CPU
131 sets (e.g., to allocate sets larger than that defined by the standard
132 \fIcpu_set_t\fP data type), glibc nowadays provides a set of macros to support
135 The following macros are used to allocate and deallocate CPU sets:
138 Allocate a CPU set large enough to hold CPUs in the range 0 to
141 \fBCPU_ALLOC_SIZE\fP()
142 Return the size in bytes of the CPU set that would be needed to hold CPUs in
143 the range 0 to \fInum_cpus\-1\fP. This macro provides the value that can be
144 used for the \fIsetsize\fP argument in the \fBCPU_*_S\fP() macros described
148 Free a CPU set previously allocated by \fBCPU_ALLOC\fP().
150 The macros whose names end with "_S" are the analogs of the similarly named
151 macros without the suffix. These macros perform the same tasks as their
152 analogs, but operate on the dynamically allocated CPU set(s) whose size is
155 \fBCPU_ISSET\fP() and \fBCPU_ISSET_S\fP() return nonzero if \fIcpu\fP is in \fIset\fP;
156 otherwise, it returns 0.
158 \fBCPU_COUNT\fP() and \fBCPU_COUNT_S\fP() return the number of CPUs in \fIset\fP.
160 \fBCPU_EQUAL\fP() and \fBCPU_EQUAL_S\fP() return nonzero if the two CPU sets are
161 equal; otherwise it returns 0.
163 \fBCPU_ALLOC\fP() returns a pointer on success, or NULL on failure. (Errors
164 are as for \fBmalloc\fP(3).)
166 \fBCPU_ALLOC_SIZE\fP() returns the number of bytes required to store a CPU set
167 of the specified cardinality.
169 The other functions do not return a value.
171 The \fBCPU_ZERO\fP(), \fBCPU_SET\fP(), \fBCPU_CLR\fP(), and \fBCPU_ISSET\fP() macros
172 were added in glibc 2.3.3.
174 \fBCPU_COUNT\fP() first appeared in glibc 2.6.
176 \fBCPU_AND\fP(), \fBCPU_OR\fP(), \fBCPU_XOR\fP(), \fBCPU_EQUAL\fP(), \fBCPU_ALLOC\fP(),
177 \fBCPU_ALLOC_SIZE\fP(), \fBCPU_FREE\fP(), \fBCPU_ZERO_S\fP(), \fBCPU_SET_S\fP(),
178 \fBCPU_CLR_S\fP(), \fBCPU_ISSET_S\fP(), \fBCPU_AND_S\fP(), \fBCPU_OR_S\fP(),
179 \fBCPU_XOR_S\fP(), and \fBCPU_EQUAL_S\fP() first appeared in glibc 2.7.
181 These interfaces are Linux\-specific.
183 To duplicate a CPU set, use \fBmemcpy\fP(3).
185 Since CPU sets are bitsets allocated in units of long words, the actual
186 number of CPUs in a dynamically allocated CPU set will be rounded up to the
187 next multiple of \fIsizeof(unsigned long)\fP. An application should consider
188 the contents of these extra bits to be undefined.
190 Notwithstanding the similarity in the names, note that the constant
191 \fBCPU_SETSIZE\fP indicates the number of CPUs in the \fIcpu_set_t\fP data type
192 (thus, it is effectively a count of bits in the bitset), while the
193 \fIsetsize\fP argument of the \fBCPU_*_S\fP() macros is a size in bytes.
195 The data types for arguments and return values shown in the SYNOPSIS are
196 hints what about is expected in each case. However, since these interfaces
197 are implemented as macros, the compiler won't necessarily catch all type
198 errors if you violate the suggestions.
200 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7029
201 On 32\-bit platforms with glibc 2.8 and earlier, \fBCPU_ALLOC\fP() allocates
202 twice as much space as is required, and \fBCPU_ALLOC_SIZE\fP() returns a value
203 twice as large as it should. This bug should not affect the semantics of a
204 program, but does result in wasted memory and less efficient operation of
205 the macros that operate on dynamically allocated CPU sets. These bugs are
208 The following program demonstrates the use of some of the macros used for
209 dynamically allocated CPU sets.
220 main(int argc, char *argv[])
227 fprintf(stderr, "Usage: %s <num\-cpus>\en", argv[0]);
231 num_cpus = atoi(argv[1]);
233 cpusetp = CPU_ALLOC(num_cpus);
234 if (cpusetp == NULL) {
239 size = CPU_ALLOC_SIZE(num_cpus);
241 CPU_ZERO_S(size, cpusetp);
242 for (cpu = 0; cpu < num_cpus; cpu += 2)
243 CPU_SET_S(cpu, size, cpusetp);
245 printf("CPU_COUNT() of set: %d\en", CPU_COUNT_S(size, cpusetp));
252 \fBsched_setaffinity\fP(2), \fBpthread_attr_setaffinity_np\fP(3),
253 \fBpthread_setaffinity_np\fP(3), \fBcpuset\fP(7)
255 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
256 である。プロジェクトの説明とバグ報告に関する情報は
257 http://www.kernel.org/doc/man\-pages/ に書かれている。