OSDN Git Service

Update README
[linuxjm/LDP_man-pages.git] / draft / man3 / pthread_setaffinity_np.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
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 .\" %%%LICENSE_END
25 .\"
26 .\"*******************************************************************
27 .\"
28 .\" This file was generated with po4a. Translate the source file.
29 .\"
30 .\"*******************************************************************
31 .\"
32 .\" Japanese Version Copyright (c) 2012  Akihiro MOTOKI
33 .\"         all rights reserved.
34 .\" Translated 2012-05-03, Akihiro MOTOKI <amotoki@gmail.com>
35 .\"
36 .TH PTHREAD_SETAFFINITY_NP 3 2014\-05\-23 Linux "Linux Programmer's Manual"
37 .SH 名前
38 pthread_setaffinity_np, pthread_getaffinity_np \- スレッドの
39 CPU affinity の設定/取得を行う
40 .SH 書式
41 .nf
42 \fB#define _GNU_SOURCE\fP             /* See feature_test_macros(7) */
43 \fB#include <pthread.h>\fP
44
45 \fBint pthread_setaffinity_np(pthread_t \fP\fIthread\fP\fB, size_t \fP\fIcpusetsize\fP\fB,\fP
46 \fB                           const cpu_set_t *\fP\fIcpuset\fP\fB);\fP
47 \fBint pthread_getaffinity_np(pthread_t \fP\fIthread\fP\fB, size_t \fP\fIcpusetsize\fP\fB,\fP
48 \fB                           cpu_set_t *\fP\fIcpuset\fP\fB);\fP
49 .sp
50 \fI\-pthread\fP でコンパイルしてリンクする。
51 .fi
52 .SH 説明
53 \fBpthread_setaffinity_np\fP() 関数は、スレッド \fIthread\fP の CPU affinity
54 マスクに \fIcpuset\fP が指す CPU 集合を設定する。呼び出しが成功し、
55 そのスレッドが現在 \fIcpuset\fP で指定された CPU 上でが実行されていない
56 場合は、スレッドは指定された CPU のいずれかに移動される。
57
58 \fBpthread_getaffinity_np\fP() 関数は、スレッド \fIthread\fP の CPU affinity
59 マスクを、\fIcpuset\fP が指すバッファーに入れて返す。
60
61 CPU affinity マスクの詳細については、
62 \fBsched_setaffinity\fP(2) を参照してほしい。
63 CPU 集合の操作や取得を行う際に利用できるマクロ群の説明は
64 \fBCPU_SET\fP(3) を参照してほしい。
65
66 引き数 \fIcpusetsize\fP は \fIcpuset\fP が指すバッファーの長さ (バイト単位) で
67 ある。通常は、この引き数には \fIsizeof(cpu_set_t)\fP を指定する
68 (\fBCPU_SET\fP(3) に書かれているマクロを使って CPU 集合を動的に
69 割り当てている場合には、別の値になることもある)。
70 .SH 返り値
71 成功すると、これらの関数は 0 を返す。
72 エラーの場合、0 以外のエラー番号を返す。
73 .SH エラー
74 .TP 
75 \fBEFAULT\fP
76 指定されたメモリーアドレスが無効である。
77 .TP 
78 \fBEINVAL\fP
79 (\fBpthread_setaffinity_np\fP()) affinity ビットマスク \fImask\fP に、
80 その時点でシステム上に物理的に存在して、かつそのスレッドに対して許可
81 されているプロセッサが一つも含まれていない。
82 スレッドに対してどのプロセッサの利用が許可されるかは、\fBcpuset\fP(7) で
83 説明されている "cpuset" 機構に適用される制限に基づいて決まる。
84 .TP 
85 \fBEINVAL\fP
86 .\" FIXME . ?
87 .\" Loic Domaigne commented: it seems that in the future the
88 .\" kernel developers want to make cpumask_t dynamic, so
89 .\" CONFIG_NR_CPUS might become obsolete in the future.
90 .\" cpumask_t
91 .\" The raw sched_getaffinity() system call returns the size (in bytes)
92 .\" of the cpumask_t type.
93 (\fBpthread_setaffinity_np\fP()) \fIcpuset\fP が、カーネルがサポートする CPU
94 集合に含まれない CPU を指定していた。(カーネルの設定オプション
95 \fBCONFIG_NR_CPUS\fP により、CPU 集合を表現するのに使われるカーネルの
96 データ型がサポートする CPU 集合の範囲が定義される。)
97 .TP 
98 \fBEINVAL\fP
99 (\fBpthread_getaffinity_np\fP()) \fIcpusetsize\fP がカーネルが使用する
100 affinity マスクの大きさよりも小さい。
101 .TP 
102 \fBESRCH\fP
103 ID が \fIthread\fP のスレッドが見つからなかった。
104 .SH バージョン
105 これらの関数は glibc バージョン 2.3.4 以降で提供されている。
106 .SH 属性
107 .SS "マルチスレッディング (pthreads(7) 参照)"
108 関数 \fBpthread_setaffinity_np\fP() と \fBpthread_getaffinity_np\fP() はスレッドセーフである。
109 .SH 準拠
110 これらの関数は非標準の GNU による拡張である。
111 そのため、名前に "_np" (nonportable; 移植性がない) という接尾辞が
112 付いている。
113 .SH 注意
114 \fBpthread_setaffinity_np\fP() を呼び出した後、
115 そのスレッドが実際に実行される CPU 集合は、
116 \fIcpuset\fP 引き数で指定された集合と
117 システムに実際に存在する CPU 集合の共通部分になる。
118 また、 \fBcpuset\fP(7) で説明されている "cpuset" 機構が使われている場合
119 には、そのスレッドが実行される CPU 集合がシステムによってさらに制限
120 される場合がある。そのスレッドが実行される実際の CPU 集合に対する
121 これらの制限は、カーネルにより黙って適用される。
122
123 これらの関数は、システムコール \fBsched_setaffinity\fP(2) と
124 \fBsched_getaffinity\fP(2) を使って実装されている。
125
126 (このバージョンだけであるが) glibc 2.3.3 では、
127 これらの関数は \fIcpusetsize\fP 引き数を持っていなかった。
128 内部で呼ばれるシステムコールに渡される CPU セットの大きさは
129 常に \fIsizeof(cpu_set_t)\fP であった。
130
131 \fBpthread_create\fP(3) で作成される新しいスレッドは、
132 作成者の CPU affinity マスクを継承する。
133 .SH 例
134 以下のプログラムでは、メインスレッドは
135 \fBpthread_setaffinity_np\fP() を使って自分の CPU affinity マスクに
136 CPU 0 から 7 が含まれるように設定し
137 (システム上には 0 から 7 に対応する CPU が全て存在するとは限らない)、
138 その後で \fBpthread_getaffinity_np\fP() を使って
139 スレッドに実際に設定された CPU affinity マスクを確認している。
140
141 .nf
142 #define _GNU_SOURCE
143 #include <pthread.h>
144 #include <stdio.h>
145 #include <stdlib.h>
146 #include <errno.h>
147
148 #define handle_error_en(en, msg) \e
149         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
150
151 int
152 main(int argc, char *argv[])
153 {
154     int s, j;
155     cpu_set_t cpuset;
156     pthread_t thread;
157
158     thread = pthread_self();
159
160     /* Set affinity mask to include CPUs 0 to 7 */
161
162     CPU_ZERO(&cpuset);
163     for (j = 0; j < 8; j++)
164         CPU_SET(j, &cpuset);
165
166     s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
167     if (s != 0)
168         handle_error_en(s, "pthread_setaffinity_np");
169
170     /* Check the actual affinity mask assigned to the thread */
171
172     s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
173     if (s != 0)
174         handle_error_en(s, "pthread_getaffinity_np");
175
176     printf("Set returned by pthread_getaffinity_np() contained:\en");
177     for (j = 0; j < CPU_SETSIZE; j++)
178         if (CPU_ISSET(j, &cpuset))
179             printf("    CPU %d\en", j);
180
181     exit(EXIT_SUCCESS);
182 }
183 .fi
184 .SH 関連項目
185 \fBsched_setaffinity\fP(2), \fBpthread_attr_setaffinity_np\fP(3),
186 \fBpthread_self\fP(3), \fBsched_getcpu\fP(3), \fBcpuset\fP(7), \fBpthreads\fP(7),
187 \fBsched\fP(7)
188 .SH この文書について
189 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部
190 である。プロジェクトの説明とバグ報告に関する情報は
191 http://www.kernel.org/doc/man\-pages/ に書かれている。