1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"*******************************************************************
26 .\" This file was generated with po4a. Translate the source file.
28 .\"*******************************************************************
29 .TH PTHREAD_SETAFFINITY_NP 3 2010\-09\-10 Linux "Linux Programmer's Manual"
31 pthread_setaffinity_np, pthread_getaffinity_np \- スレッドの
32 CPU affinity の設定/取得を行う
35 \fB#define _GNU_SOURCE\fP /* See feature_test_macros(7) */
36 \fB#include <pthread.h>\fP
38 \fBint pthread_setaffinity_np(pthread_t \fP\fIthread\fP\fB, size_t \fP\fIcpusetsize\fP\fB,\fP
39 \fB const cpu_set_t *\fP\fIcpuset\fP\fB);\fP
40 \fBint pthread_getaffinity_np(pthread_t \fP\fIthread\fP\fB, size_t \fP\fIcpusetsize\fP\fB,\fP
41 \fB cpu_set_t *\fP\fIcpuset\fP\fB);\fP
43 \fI\-pthread\fP でコンパイルしてリンクする。
46 \fBpthread_setaffinity_np\fP() 関数は、スレッド \fIthread\fP の CPU affinity
47 マスクに \fIcpuset\fP が指す CPU 集合を設定する。呼び出しが成功し、
48 そのスレッドが現在 \fIcpuset\fP で指定された CPU 上でが実行されていない
49 場合は、スレッドは指定された CPU のいずれかに移動される。
51 \fBpthread_getaffinity_np\fP() 関数は、スレッド \fIthread\fP の CPU affinity
52 マスクを、\fIcpuset\fP が指すバッファに入れて返す。
54 CPU affinity マスクの詳細については、
55 \fBsched_setaffinity\fP(2) を参照してほしい。
56 CPU 集合の操作や取得を行う際に利用できるマクロ群の説明は
57 \fBCPU_SET\fP(3) を参照してほしい。
59 引き数 \fIcpusetsize\fP は \fIcpuset\fP が指すバッファの長さ (バイト単位) で
60 ある。通常は、この引き数には \fIsizeof(cpu_set_t)\fP を指定する
61 (\fBCPU_SET\fP(3) に書かれているマクロを使って CPU 集合を動的に
62 割り当てている場合には、別の値になることもある)。
72 (\fBpthread_setaffinity_np\fP()) affinity ビットマスク \fImask\fP に、
73 その時点でシステム上に物理的に存在して、かつそのスレッドに対して許可
74 されているプロセッサが一つも含まれていない。
75 スレッドに対してどのプロセッサの利用が許可されるかは、\fBcpuset\fP(7) で
76 説明されている "cpuset" 機構に適用される制限に基づいて決まる。
80 .\" Loic Domaigne commented: it seems that in the future the
81 .\" kernel developers want to make cpumask_t dynamic, so
82 .\" CONFIG_NR_CPUS might become obsolete in the future.
84 .\" The raw sched_getaffinity() system call returns the size (in bytes)
85 .\" of the cpumask_t type.
86 (\fBpthread_setaffinity_np\fP()) \fIcpuset\fP が、カーネルがサポートする CPU
87 集合に含まれない CPU を指定していた。(カーネルの設定オプション
88 \fBCONFIG_NR_CPUS\fP により、CPU 集合を表現するのに使われるカーネルの
89 データ型がサポートする CPU 集合の範囲が定義される。)
92 (\fBpthread_getaffinity_np\fP()) \fIcpusetsize\fP がカーネルが使用する
93 affinity マスクの大きさよりも小さい。
96 ID が \fIthread\fP のスレッドが見つからなかった。
98 これらの関数は glibc バージョン 2.3.4 以降で提供されている。
100 これらの関数は非標準の GNU による拡張である。
101 そのため、名前に "_np" (nonportable; 移植性がない) という接尾辞が
104 \fBpthread_setaffinity_np\fP() を呼び出した後、
105 そのスレッドが実際に実行される CPU 集合は、
106 \fIcpuset\fP 引き数で指定された集合と
107 システムに実際に存在する CPU 集合の共通部分になる。
108 また、 \fBcpuset\fP(7) で説明されている "cpuset" 機構が使われている場合
109 には、そのスレッドが実行される CPU 集合がシステムによってさらに制限
110 される場合がある。そのスレッドが実行される実際の CPU 集合に対する
111 これらの制限は、カーネルにより黙って適用される。
113 これらの関数は、システムコール \fBsched_setaffinity\fP(2) と
114 \fBsched_getaffinity\fP(2) を使って実装されている。
116 (このバージョンだけであるが) glibc 2.3.3 では、
117 これらの関数は \fIcpusetsize\fP 引き数を持っていなかった。
118 内部で呼ばれるシステムコールに渡される CPU セットの大きさは
119 常に \fIsizeof(cpu_set_t)\fP であった。
121 \fBpthread_create\fP(3) で作成される新しいスレッドは、
122 作成者の CPU affinity マスクを継承する。
125 \fBpthread_setaffinity_np\fP() を使って自分の CPU affinity マスクに
126 CPU 0 から 7 が含まれるように設定し
127 (システム上には 0 から 7 に対応する CPU が全て存在するとは限らない)、
128 その後で \fBpthread_getaffinity_np\fP() を使って
129 スレッドに実際に設定された CPU affinity マスクを確認している。
138 #define handle_error_en(en, msg) \e
139 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
142 main(int argc, char *argv[])
148 thread = pthread_self();
150 /* Set affinity mask to include CPUs 0 to 7 */
153 for (j = 0; j < 8; j++)
156 s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
158 handle_error_en(s, "pthread_setaffinity_np");
160 /* Check the actual affinity mask assigned to the thread */
162 s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
164 handle_error_en(s, "pthread_getaffinity_np");
166 printf("Set returned by pthread_getaffinity_np() contained:\en");
167 for (j = 0; j < CPU_SETSIZE; j++)
168 if (CPU_ISSET(j, &cpuset))
169 printf(" CPU %d\en", j);
175 \fBsched_setaffinity\fP(2), \fBsched_setscheduler\fP(2),
176 \fBpthread_attr_setaffinity_np\fP(3), \fBpthread_self\fP(3), \fBsched_getcpu\fP(3),
177 \fBcpuset\fP(7), \fBpthreads\fP(7)
179 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
180 である。プロジェクトの説明とバグ報告に関する情報は
181 http://www.kernel.org/doc/man\-pages/ に書かれている。