OSDN Git Service

d615c1c2cf2e21d19f1ae2ad95fccfdaf85626c2
[linuxjm/jm.git] / manual / LDP_man-pages / 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 2020\-11\-01 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 .PP
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 .PP
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 .PP
58 \fBpthread_getaffinity_np\fP() 関数は、スレッド \fIthread\fP の CPU affinity
59 マスクを、\fIcpuset\fP が指すバッファーに入れて返す。
60 .PP
61 CPU affinity マスクの詳細については、
62 \fBsched_setaffinity\fP(2) を参照してほしい。
63 CPU 集合の操作や取得を行う際に利用できるマクロ群の説明は
64 \fBCPU_SET\fP(3) を参照してほしい。
65 .PP
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 .\" cpumask_t
87 .\" The raw sched_getaffinity() system call returns the size (in bytes)
88 .\" of the cpumask_t type.
89 (\fBpthread_setaffinity_np\fP()) \fIcpuset\fP が、カーネルがサポートする CPU
90 集合に含まれない CPU を指定していた。(カーネルの設定オプション
91 \fBCONFIG_NR_CPUS\fP により、CPU 集合を表現するのに使われるカーネルの
92 データ型がサポートする CPU 集合の範囲が定義される。)
93 .TP 
94 \fBEINVAL\fP
95 (\fBpthread_getaffinity_np\fP()) \fIcpusetsize\fP がカーネルが使用する
96 affinity マスクの大きさよりも小さい。
97 .TP 
98 \fBESRCH\fP
99 ID が \fIthread\fP のスレッドが見つからなかった。
100 .SH バージョン
101 これらの関数は glibc バージョン 2.3.4 以降で提供されている。
102 .SH 属性
103 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。
104 .TS
105 allbox;
106 lbw25 lb lb
107 l l l.
108 インターフェース        属性  値
109 T{
110 \fBpthread_setaffinity_np\fP(),
111 \fBpthread_getaffinity_np\fP()
112 T}      Thread safety   MT\-Safe
113 .TE
114 .SH 準拠
115 これらの関数は非標準の GNU による拡張である。
116 そのため、名前に "_np" (nonportable; 移植性がない) という接尾辞が
117 付いている。
118 .SH 注意
119 \fBpthread_setaffinity_np\fP() を呼び出した後、
120 そのスレッドが実際に実行される CPU 集合は、
121 \fIcpuset\fP 引き数で指定された集合と
122 システムに実際に存在する CPU 集合の共通部分になる。
123 また、 \fBcpuset\fP(7) で説明されている "cpuset" 機構が使われている場合
124 には、そのスレッドが実行される CPU 集合がシステムによってさらに制限
125 される場合がある。そのスレッドが実行される実際の CPU 集合に対する
126 これらの制限は、カーネルにより黙って適用される。
127 .PP
128 これらの関数は、システムコール \fBsched_setaffinity\fP(2) と
129 \fBsched_getaffinity\fP(2) を使って実装されている。
130 .PP
131 (このバージョンだけであるが) glibc 2.3.3 では、
132 これらの関数は \fIcpusetsize\fP 引き数を持っていなかった。
133 内部で呼ばれるシステムコールに渡される CPU セットの大きさは
134 常に \fIsizeof(cpu_set_t)\fP であった。
135 .PP
136 \fBpthread_create\fP(3) で作成される新しいスレッドは、
137 作成者の CPU affinity マスクを継承する。
138 .SH EXAMPLES
139 以下のプログラムでは、メインスレッドは
140 \fBpthread_setaffinity_np\fP() を使って自分の CPU affinity マスクに
141 CPU 0 から 7 が含まれるように設定し
142 (システム上には 0 から 7 に対応する CPU が全て存在するとは限らない)、
143 その後で \fBpthread_getaffinity_np\fP() を使って
144 スレッドに実際に設定された CPU affinity マスクを確認している。
145 .PP
146 .EX
147 #define _GNU_SOURCE
148 #include <pthread.h>
149 #include <stdio.h>
150 #include <stdlib.h>
151 #include <errno.h>
152
153 #define handle_error_en(en, msg) \e
154         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
155
156 int
157 main(int argc, char *argv[])
158 {
159     int s;
160     cpu_set_t cpuset;
161     pthread_t thread;
162
163     thread = pthread_self();
164
165     /* Set affinity mask to include CPUs 0 to 7 */
166
167     CPU_ZERO(&cpuset);
168     for (int j = 0; j < 8; j++)
169         CPU_SET(j, &cpuset);
170
171     s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
172     if (s != 0)
173         handle_error_en(s, "pthread_setaffinity_np");
174
175     /* Check the actual affinity mask assigned to the thread */
176
177     s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset);
178     if (s != 0)
179         handle_error_en(s, "pthread_getaffinity_np");
180
181     printf("Set returned by pthread_getaffinity_np() contained:\en");
182     for (int j = 0; j < CPU_SETSIZE; j++)
183         if (CPU_ISSET(j, &cpuset))
184             printf("    CPU %d\en", j);
185
186     exit(EXIT_SUCCESS);
187 }
188 .EE
189 .SH 関連項目
190 \fBsched_setaffinity\fP(2), \fBCPU_SET\fP(3), \fBpthread_attr_setaffinity_np\fP(3),
191 \fBpthread_self\fP(3), \fBsched_getcpu\fP(3), \fBcpuset\fP(7), \fBpthreads\fP(7),
192 \fBsched\fP(7)
193 .SH この文書について
194 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
195 \%https://www.kernel.org/doc/man\-pages/ に書かれている。