OSDN Git Service

(split) LDP: Restore and add Copyrights for draft pages
[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 2010\-09\-10 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 これらの関数は非標準の GNU による拡張である。
108 そのため、名前に "_np" (nonportable; 移植性がない) という接尾辞が
109 付いている。
110 .SH 注意
111 \fBpthread_setaffinity_np\fP() を呼び出した後、
112 そのスレッドが実際に実行される CPU 集合は、
113 \fIcpuset\fP 引き数で指定された集合と
114 システムに実際に存在する CPU 集合の共通部分になる。
115 また、 \fBcpuset\fP(7) で説明されている "cpuset" 機構が使われている場合
116 には、そのスレッドが実行される CPU 集合がシステムによってさらに制限
117 される場合がある。そのスレッドが実行される実際の CPU 集合に対する
118 これらの制限は、カーネルにより黙って適用される。
119
120 これらの関数は、システムコール \fBsched_setaffinity\fP(2) と
121 \fBsched_getaffinity\fP(2) を使って実装されている。
122
123 (このバージョンだけであるが) glibc 2.3.3 では、
124 これらの関数は \fIcpusetsize\fP 引き数を持っていなかった。
125 内部で呼ばれるシステムコールに渡される CPU セットの大きさは
126 常に \fIsizeof(cpu_set_t)\fP であった。
127
128 \fBpthread_create\fP(3) で作成される新しいスレッドは、
129 作成者の CPU affinity マスクを継承する。
130 .SH 例
131 以下のプログラムでは、メインスレッドは
132 \fBpthread_setaffinity_np\fP() を使って自分の CPU affinity マスクに
133 CPU 0 から 7 が含まれるように設定し
134 (システム上には 0 から 7 に対応する CPU が全て存在するとは限らない)、
135 その後で \fBpthread_getaffinity_np\fP() を使って
136 スレッドに実際に設定された CPU affinity マスクを確認している。
137
138 .nf
139 #define _GNU_SOURCE
140 #include <pthread.h>
141 #include <stdio.h>
142 #include <stdlib.h>
143 #include <errno.h>
144
145 #define handle_error_en(en, msg) \e
146         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
147
148 int
149 main(int argc, char *argv[])
150 {
151     int s, j;
152     cpu_set_t cpuset;
153     pthread_t thread;
154
155     thread = pthread_self();
156
157     /* Set affinity mask to include CPUs 0 to 7 */
158
159     CPU_ZERO(&cpuset);
160     for (j = 0; j < 8; j++)
161         CPU_SET(j, &cpuset);
162
163     s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
164     if (s != 0)
165         handle_error_en(s, "pthread_setaffinity_np");
166
167     /* Check the actual affinity mask assigned to the thread */
168
169     s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
170     if (s != 0)
171         handle_error_en(s, "pthread_getaffinity_np");
172
173     printf("Set returned by pthread_getaffinity_np() contained:\en");
174     for (j = 0; j < CPU_SETSIZE; j++)
175         if (CPU_ISSET(j, &cpuset))
176             printf("    CPU %d\en", j);
177
178     exit(EXIT_SUCCESS);
179 }
180 .fi
181 .SH 関連項目
182 \fBsched_setaffinity\fP(2), \fBsched_setscheduler\fP(2),
183 \fBpthread_attr_setaffinity_np\fP(3), \fBpthread_self\fP(3), \fBsched_getcpu\fP(3),
184 \fBcpuset\fP(7), \fBpthreads\fP(7)
185 .SH この文書について
186 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
187 である。プロジェクトの説明とバグ報告に関する情報は
188 http://www.kernel.org/doc/man\-pages/ に書かれている。