OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / original / man3 / CPU_SET.3
1 .\" Copyright (C) 2006 Michael Kerrisk
2 .\" and Copyright (C) 2008 Linux Foundation, written by Michael Kerrisk
3 .\"     <mtk.manpages@gmail.com>
4 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"
25 .TH CPU_SET 3 2010-02-21 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 CPU_SET, CPU_CLR, CPU_ISSET, CPU_ZERO, CPU_COUNT,
28 CPU_AND, CPU_OR, CPU_XOR, CPU_EQUAL,
29 CPU_ALLOC, CPU_ALLOC_SIZE, CPU_FREE,
30 CPU_SET_S, CPU_CLR_S, CPU_ISSET_S, CPU_ZERO_S,
31 CPU_COUNT_S, CPU_AND_S, CPU_OR_S, CPU_XOR_S, CPU_EQUAL_S \-
32 macros for manipulating CPU sets
33 .SH SYNOPSIS
34 .nf
35 .B #define _GNU_SOURCE
36 .B #include <sched.h>
37 .sp
38 .BI "void CPU_ZERO(cpu_set_t *" set );
39 .sp
40 .BI "void CPU_SET(int " cpu ", cpu_set_t *" set );
41 .BI "void CPU_CLR(int " cpu ", cpu_set_t *" set );
42 .BI "int  CPU_ISSET(int " cpu ", cpu_set_t *" set );
43 .sp
44 .BI "int  CPU_COUNT(cpu_set_t *" set );
45 .sp
46 .BI "void CPU_AND(cpu_set_t *" destset ,
47 .BI "             cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
48 .BI "void CPU_OR(cpu_set_t *" destset ,
49 .BI "             cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
50 .BI "void CPU_XOR(cpu_set_t *" destset ,
51 .BI "             cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
52 .sp
53 .BI "int  CPU_EQUAL(cpu_set_t *" set1 ", cpu_set_t *" set2 );
54 .sp
55 .BI "cpu_set_t *CPU_ALLOC(int " num_cpus );
56 .BI "void CPU_FREE(cpu_set_t *" set );
57 .BI "size_t CPU_ALLOC_SIZE(int " num_cpus );
58 .sp
59 .BI "void CPU_ZERO_S(size_t " setsize ", cpu_set_t *" set );
60 .sp
61 .BI "void CPU_SET_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
62 .BI "void CPU_CLR_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
63 .BI "int  CPU_ISSET_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
64 .sp
65 .BI "int  CPU_COUNT_S(size_t " setsize ", cpu_set_t *" set );
66 .sp
67 .BI "void CPU_AND_S(size_t " setsize ", cpu_set_t *" destset ,
68 .BI "             cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
69 .BI "void CPU_OR_S(size_t " setsize ", cpu_set_t *" destset ,
70 .BI "             cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
71 .BI "void CPU_XOR_S(size_t " setsize ", cpu_set_t *" destset ,
72 .BI "             cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
73 .sp
74 .BI "int  CPU_EQUAL_S(size_t " setsize ", cpu_set_t *" set1 \
75 ", cpu_set_t *" set2 );
76 .fi
77 .SH DESCRIPTION
78 The
79 .I cpu_set_t
80 data structure represents a set of CPUs.
81 CPU sets are used by
82 .BR sched_setaffinity (2)
83 and similar interfaces.
84
85 The
86 .I cpu_set_t
87 data type is implemented as a bitset.
88 However, the data structure treated as considered opaque:
89 all manipulation of CPU sets should be done via the macros
90 described in this page.
91
92 The following macros are provided to operate on the CPU set
93 .IR set :
94 .TP 17
95 .BR CPU_ZERO ()
96 Clears
97 .IR set ,
98 so that it contains no CPUs.
99 .TP
100 .BR CPU_SET ()
101 Add CPU
102 .I cpu
103 to
104 .IR set .
105 .TP
106 .BR CPU_CLR ()
107 Remove CPU
108 .I cpu
109 from
110 .IR set .
111 .TP
112 .BR CPU_ISSET ()
113 Test to see if CPU
114 .I cpu
115 is a member of
116 .IR set .
117 .TP
118 .BR CPU_COUNT ()
119 Return the number of CPUs in
120 .IR set .
121 .PP
122 Where a
123 .I cpu
124 argument is specified, it should not produce side effects,
125 since the above macros may evaluate the argument more than once.
126 .PP
127 The first available CPU on the system corresponds to a
128 .I cpu
129 value of 0, the next CPU corresponds to a
130 .I cpu
131 value of 1, and so on.
132 The constant
133 .B CPU_SETSIZE
134 (currently 1024) specifies a value one greater than the maximum CPU
135 number that can be stored in
136 .IR cpu_set_t .
137
138 The following macros perform logical operations on CPU sets:
139 .TP 17
140 .BR CPU_AND ()
141 Store the logical AND of the sets
142 .I srcset1
143 and
144 .I srcset2
145 in
146 .I destset
147 (which may be one of the source sets).
148 .TP
149 .BR CPU_OR ()
150 Store the logical OR of the sets
151 .I srcset1
152 and
153 .I srcset2
154 in
155 .I destset
156 (which may be one of the source sets).
157 .TP
158 .BR CPU_XOR ()
159 Store the logical XOR of the sets
160 .I srcset1
161 and
162 .I srcset2
163 in
164 .I destset
165 (which may be one of the source sets).
166 .TP
167 .BR CPU_EQUAL ()
168 Test whether two CPU set contain exactly the same CPUs.
169 .SS Dynamically sized CPU sets
170 Because some applications may require the ability to dynamically
171 size CPU sets (e.g., to allocate sets larger than that
172 defined by the standard
173 .I cpu_set_t
174 data type), glibc nowadays provides a set of macros to support this.
175
176 The following macros are used to allocate and deallocate CPU sets:
177 .TP 17
178 .BR CPU_ALLOC ()
179 Allocate a CPU set large enough to hold CPUs
180 in the range 0 to
181 .IR num_cpus-1 .
182 .TP
183 .BR CPU_ALLOC_SIZE ()
184 Return the size in bytes of the CPU set that would be needed to
185 hold CPUs in the range 0 to
186 .IR num_cpus-1 .
187 This macro provides the value that can be used for the
188 .I setsize
189 argument in the
190 .BR CPU_*_S ()
191 macros described below.
192 .TP
193 .BR CPU_FREE ()
194 Free a CPU set previously allocated by
195 .BR CPU_ALLOC ().
196 .PP
197 The macros whose names end with "_S" are the analogs of
198 the similarly named macros without the suffix.
199 These macros perform the same tasks as their analogs,
200 but operate on the dynamically allocated CPU set(s) whose size is
201 .I setsize
202 bytes.
203 .SH "RETURN VALUE"
204 .BR CPU_ISSET ()
205 and
206 .BR CPU_ISSET_S ()
207 return nonzero if
208 .I cpu
209 is in
210 .IR set ;
211 otherwise, it returns 0.
212
213 .BR CPU_COUNT ()
214 and
215 .BR CPU_COUNT_S ()
216 return the number of CPUs in
217 .IR set .
218
219 .BR CPU_EQUAL ()
220 and
221 .BR CPU_EQUAL_S ()
222 return nonzero if the two CPU sets are equal; otherwise it returns 0.
223
224 .BR CPU_ALLOC ()
225 returns a pointer on success, or NULL on failure.
226 (Errors are as for
227 .BR malloc (3).)
228
229 .BR CPU_ALLOC_SIZE ()
230 returns the number of bytes required to store a
231 CPU set of the specified cardinality.
232
233 The other functions do not return a value.
234 .SH VERSIONS
235 The
236 .BR CPU_ZERO (),
237 .BR CPU_SET (),
238 .BR CPU_CLR (),
239 and
240 .BR CPU_ISSET ()
241 macros were added in glibc 2.3.3.
242
243 .BR CPU_COUNT ()
244 first appeared in glibc 2.6.
245
246 .BR CPU_AND (),
247 .BR CPU_OR (),
248 .BR CPU_XOR (),
249 .BR CPU_EQUAL (),
250 .BR CPU_ALLOC (),
251 .BR CPU_ALLOC_SIZE (),
252 .BR CPU_FREE (),
253 .BR CPU_ZERO_S (),
254 .BR CPU_SET_S (),
255 .BR CPU_CLR_S (),
256 .BR CPU_ISSET_S (),
257 .BR CPU_AND_S (),
258 .BR CPU_OR_S (),
259 .BR CPU_XOR_S (),
260 and
261 .BR CPU_EQUAL_S ()
262 first appeared in glibc 2.7.
263 .SH "CONFORMING TO"
264 These interfaces are Linux-specific.
265 .SH NOTES
266 To duplicate a CPU set, use
267 .BR memcpy (3).
268
269 Since CPU sets are bitsets allocated in units of long words,
270 the actual number of CPUs in a dynamically
271 allocated CPU set will be rounded up to the next multiple of
272 .IR "sizeof(unsigned long)" .
273 An application should consider the contents of these extra bits
274 to be undefined.
275
276 Notwithstanding the similarity in the names,
277 note that the constant
278 .B CPU_SETSIZE
279 indicates the number of CPUs in the
280 .I cpu_set_t
281 data type (thus, it is effectively a count of bits in the bitset),
282 while the
283 .I setsize
284 argument of the
285 .BR CPU_*_S ()
286 macros is a size in bytes.
287
288 The data types for arguments and return values shown
289 in the SYNOPSIS are hints what about is expected in each case.
290 However, since these interfaces are implemented as macros,
291 the compiler won't necessarily catch all type errors
292 if you violate the suggestions.
293 .SH EXAMPLE
294 The following program demonstrates the use of some of the macros
295 used for dynamically allocated CPU sets.
296
297 .nf
298 #define _GNU_SOURCE
299 #include <sched.h>
300 #include <stdlib.h>
301 #include <unistd.h>
302 #include <stdio.h>
303 #include <assert.h>
304
305 int
306 main(int argc, char *argv[])
307 {
308     cpu_set_t *cpusetp;
309     size_t size;
310     int num_cpus, cpu;
311
312     if (argc < 2) {
313         fprintf(stderr, "Usage: %s <num\-cpus>\\n", argv[0]);
314         exit(EXIT_FAILURE);
315     }
316
317     num_cpus = atoi(argv[1]);
318
319     cpusetp = CPU_ALLOC(num_cpus);
320     if (cpusetp == NULL) {
321         perror("CPU_ALLOC");
322         exit(EXIT_FAILURE);
323     }
324
325     size = CPU_ALLOC_SIZE(num_cpus);
326
327     CPU_ZERO_S(size, cpusetp);
328     for (cpu = 0; cpu < num_cpus; cpu += 2)
329         CPU_SET_S(cpu, size, cpusetp);
330
331     printf("CPU_COUNT() of set:    %d\\n", CPU_COUNT_S(size, cpusetp));
332
333     CPU_FREE(cpusetp);
334     exit(EXIT_SUCCESS);
335 }
336 .fi
337 .SH BUGS
338 On 32-bit platforms with glibc 2.8 and earlier,
339 .BR CPU_ALLOC ()
340 allocates twice as much space as is required, and
341 .BR CPU_ALLOC_SIZE ()
342 returns a value twice as large as it should.
343 This bug should not affect the semantics of a program,
344 but does result in wasted memory
345 and less efficient operation of the macros that
346 operate on dynamically allocated CPU sets.
347 These bugs are fixed in glibc 2.9.
348 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7029
349 .SH "SEE ALSO"
350 .BR sched_setaffinity (2),
351 .BR pthread_attr_setaffinity_np (3),
352 .BR pthread_setaffinity_np (3),
353 .BR cpuset (7)