.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk .\" .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2012 Akihiro MOTOKI .\" all rights reserved. .\" Translated 2012-05-03, Akihiro MOTOKI .\" .TH PTHREAD_SETAFFINITY_NP 3 2014\-05\-23 Linux "Linux Programmer's Manual" .SH 名前 pthread_setaffinity_np, pthread_getaffinity_np \- スレッドの CPU affinity の設定/取得を行う .SH 書式 .nf \fB#define _GNU_SOURCE\fP /* See feature_test_macros(7) */ \fB#include \fP \fBint pthread_setaffinity_np(pthread_t \fP\fIthread\fP\fB, size_t \fP\fIcpusetsize\fP\fB,\fP \fB const cpu_set_t *\fP\fIcpuset\fP\fB);\fP \fBint pthread_getaffinity_np(pthread_t \fP\fIthread\fP\fB, size_t \fP\fIcpusetsize\fP\fB,\fP \fB cpu_set_t *\fP\fIcpuset\fP\fB);\fP .sp \fI\-pthread\fP でコンパイルしてリンクする。 .fi .SH 説明 \fBpthread_setaffinity_np\fP() 関数は、スレッド \fIthread\fP の CPU affinity マスクに \fIcpuset\fP が指す CPU 集合を設定する。呼び出しが成功し、 そのスレッドが現在 \fIcpuset\fP で指定された CPU 上でが実行されていない 場合は、スレッドは指定された CPU のいずれかに移動される。 \fBpthread_getaffinity_np\fP() 関数は、スレッド \fIthread\fP の CPU affinity マスクを、\fIcpuset\fP が指すバッファーに入れて返す。 CPU affinity マスクの詳細については、 \fBsched_setaffinity\fP(2) を参照してほしい。 CPU 集合の操作や取得を行う際に利用できるマクロ群の説明は \fBCPU_SET\fP(3) を参照してほしい。 引き数 \fIcpusetsize\fP は \fIcpuset\fP が指すバッファーの長さ (バイト単位) で ある。通常は、この引き数には \fIsizeof(cpu_set_t)\fP を指定する (\fBCPU_SET\fP(3) に書かれているマクロを使って CPU 集合を動的に 割り当てている場合には、別の値になることもある)。 .SH 返り値 成功すると、これらの関数は 0 を返す。 エラーの場合、0 以外のエラー番号を返す。 .SH エラー .TP \fBEFAULT\fP 指定されたメモリーアドレスが無効である。 .TP \fBEINVAL\fP (\fBpthread_setaffinity_np\fP()) affinity ビットマスク \fImask\fP に、 その時点でシステム上に物理的に存在して、かつそのスレッドに対して許可 されているプロセッサが一つも含まれていない。 スレッドに対してどのプロセッサの利用が許可されるかは、\fBcpuset\fP(7) で 説明されている "cpuset" 機構に適用される制限に基づいて決まる。 .TP \fBEINVAL\fP .\" FIXME . ? .\" Loic Domaigne commented: it seems that in the future the .\" kernel developers want to make cpumask_t dynamic, so .\" CONFIG_NR_CPUS might become obsolete in the future. .\" cpumask_t .\" The raw sched_getaffinity() system call returns the size (in bytes) .\" of the cpumask_t type. (\fBpthread_setaffinity_np\fP()) \fIcpuset\fP が、カーネルがサポートする CPU 集合に含まれない CPU を指定していた。(カーネルの設定オプション \fBCONFIG_NR_CPUS\fP により、CPU 集合を表現するのに使われるカーネルの データ型がサポートする CPU 集合の範囲が定義される。) .TP \fBEINVAL\fP (\fBpthread_getaffinity_np\fP()) \fIcpusetsize\fP がカーネルが使用する affinity マスクの大きさよりも小さい。 .TP \fBESRCH\fP ID が \fIthread\fP のスレッドが見つからなかった。 .SH バージョン これらの関数は glibc バージョン 2.3.4 以降で提供されている。 .SH 属性 .SS "マルチスレッディング (pthreads(7) 参照)" 関数 \fBpthread_setaffinity_np\fP() と \fBpthread_getaffinity_np\fP() はスレッドセーフである。 .SH 準拠 これらの関数は非標準の GNU による拡張である。 そのため、名前に "_np" (nonportable; 移植性がない) という接尾辞が 付いている。 .SH 注意 \fBpthread_setaffinity_np\fP() を呼び出した後、 そのスレッドが実際に実行される CPU 集合は、 \fIcpuset\fP 引き数で指定された集合と システムに実際に存在する CPU 集合の共通部分になる。 また、 \fBcpuset\fP(7) で説明されている "cpuset" 機構が使われている場合 には、そのスレッドが実行される CPU 集合がシステムによってさらに制限 される場合がある。そのスレッドが実行される実際の CPU 集合に対する これらの制限は、カーネルにより黙って適用される。 これらの関数は、システムコール \fBsched_setaffinity\fP(2) と \fBsched_getaffinity\fP(2) を使って実装されている。 (このバージョンだけであるが) glibc 2.3.3 では、 これらの関数は \fIcpusetsize\fP 引き数を持っていなかった。 内部で呼ばれるシステムコールに渡される CPU セットの大きさは 常に \fIsizeof(cpu_set_t)\fP であった。 \fBpthread_create\fP(3) で作成される新しいスレッドは、 作成者の CPU affinity マスクを継承する。 .SH 例 以下のプログラムでは、メインスレッドは \fBpthread_setaffinity_np\fP() を使って自分の CPU affinity マスクに CPU 0 から 7 が含まれるように設定し (システム上には 0 から 7 に対応する CPU が全て存在するとは限らない)、 その後で \fBpthread_getaffinity_np\fP() を使って スレッドに実際に設定された CPU affinity マスクを確認している。 .nf #define _GNU_SOURCE #include #include #include #include #define handle_error_en(en, msg) \e do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { int s, j; cpu_set_t cpuset; pthread_t thread; thread = pthread_self(); /* Set affinity mask to include CPUs 0 to 7 */ CPU_ZERO(&cpuset); for (j = 0; j < 8; j++) CPU_SET(j, &cpuset); s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_setaffinity_np"); /* Check the actual affinity mask assigned to the thread */ s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np"); printf("Set returned by pthread_getaffinity_np() contained:\en"); for (j = 0; j < CPU_SETSIZE; j++) if (CPU_ISSET(j, &cpuset)) printf(" CPU %d\en", j); exit(EXIT_SUCCESS); } .fi .SH 関連項目 \fBsched_setaffinity\fP(2), \fBpthread_attr_setaffinity_np\fP(3), \fBpthread_self\fP(3), \fBsched_getcpu\fP(3), \fBcpuset\fP(7), \fBpthreads\fP(7), \fBsched\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。