OSDN Git Service

Update README
[linuxjm/LDP_man-pages.git] / draft / man3 / pthread_getcpuclockid.3
1 .\" Copyright (c) 2009 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_GETCPUCLOCKID 3 2014\-05\-23 Linux "Linux Programmer's Manual"
37 .SH 名前
38 pthread_getcpuclockid \- スレッドの CPU 時間時計の ID を取得する
39 .SH 書式
40 .nf
41 \fB#include <pthread.h>\fP
42 \fB#include <time.h>\fP
43
44 \fBint pthread_getcpuclockid(pthread_t \fP\fIthread\fP\fB, clockid_t *\fP\fIclock_id\fP\fB);\fP
45 .sp
46 \fI\-pthread\fP でコンパイルしてリンクする。
47 .fi
48 .SH 説明
49 .\" The clockid is constructed as follows:
50 .\" *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE)
51 .\" where CLOCK_IDFIELD_SIZE is 3.
52 \fBpthread_getcpuclockid\fP() 関数は、
53 スレッド \fIthread\fP の CPU 時間時計のクロック ID を返す。
54 .SH 返り値
55 成功すると、この関数は 0 を返す。
56 エラーの場合、 0 以外のエラー番号を返す。
57 .SH エラー
58 .TP 
59 \fBENOENT\fP
60 .\" CLOCK_THREAD_CPUTIME_ID not defined
61 .\"
62 .\" Looking at nptl/pthread_getcpuclockid.c an ERANGE error would
63 .\" be possible if kernel thread IDs took more than 29 bits (which
64 .\" they currently cannot).
65 スレッド単位の CPU 時間時計はこのシステムではサポートされていない。
66 .TP 
67 \fBESRCH\fP
68 ID が \fIthread\fP のスレッドが見つからなかった。
69 .SH バージョン
70 この関数は glibc バージョン 2.2 以降で利用できる。
71 .SH 属性
72 .SS "マルチスレッディング (pthreads(7) 参照)"
73 \fBpthread_getcpuclockid\fP() 関数はスレッドセーフである。
74 .SH 準拠
75 POSIX.1\-2001.
76 .SH 注意
77 \fIthread\fP が呼び出したスレッドを参照している場合、
78 クロック ID \fBCLOCK_THREAD_CPUTIME_ID\fP が指定されていれば、
79 \fBclock_gettime\fP(2) と \fBclock_settime\fP(2) が操作するのと同じ時計
80 を参照する ID が返される。
81 .SH 例
82 以下のプログラムは、スレッドを作成し、それから
83 \fBclock_gettime\fP(2) を使ってプロセス全体の CPU 時間を取得し、
84 \fBpthread_getcpuclockid\fP(3) を使って 2 つのスレッドが消費した
85 スレッド毎の CPU 時間を取得する。
86 下記のシェルのセッションは実行例である。
87
88 .in +4n
89 .nf
90 $ \fB./a.out\fP
91 Main thread sleeping
92 Subthread starting infinite loop
93 Main thread consuming some CPU time...
94 Process total CPU time:    1.368
95 Main thread CPU time:      0.376
96 Subthread CPU time:        0.992
97 .fi
98 .in
99 .SS プログラムのソース
100 \&
101 .nf
102 /* "\-lrt" でリンクする */
103
104 #include <time.h>
105 #include <stdio.h>
106 #include <stdlib.h>
107 #include <unistd.h>
108 #include <pthread.h>
109 #include <string.h>
110 #include <errno.h>
111
112 #define handle_error(msg) \e
113         do { perror(msg); exit(EXIT_FAILURE); } while (0)
114
115 #define handle_error_en(en, msg) \e
116         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
117
118 static void *
119 thread_start(void *arg)
120 {
121     printf("Subthread starting infinite loop\en");
122     for (;;)
123         continue;
124 }
125
126 static void
127 pclock(char *msg, clockid_t cid)
128 {
129     struct timespec ts;
130
131     printf("%s", msg);
132     if (clock_gettime(cid, &ts) == \-1)
133         handle_error("clock_gettime");
134     printf("%4ld.%03ld\en", ts.tv_sec, ts.tv_nsec / 1000000);
135 }
136
137 int
138 main(int argc, char *argv[])
139 {
140     pthread_t thread;
141     clockid_t cid;
142     int j, s;
143
144     s = pthread_create(&thread, NULL, thread_start, NULL);
145     if (s != 0)
146         handle_error_en(s, "pthread_create");
147
148     printf("Main thread sleeping\en");
149     sleep(1);
150
151     printf("Main thread consuming some CPU time...\en");
152     for (j = 0; j < 2000000; j++)
153         getppid();
154
155     pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
156
157     s = pthread_getcpuclockid(pthread_self(), &cid);
158     if (s != 0)
159         handle_error_en(s, "pthread_getcpuclockid");
160     pclock("Main thread CPU time:   ", cid);
161
162     /* The preceding 4 lines of code could have been replaced by:
163        pclock("Main thread CPU time:   ", CLOCK_THREAD_CPUTIME_ID); */
164
165     s = pthread_getcpuclockid(thread, &cid);
166     if (s != 0)
167         handle_error_en(s, "pthread_getcpuclockid");
168     pclock("Subthread CPU time: 1    ", cid);
169
170     exit(EXIT_SUCCESS);         /* Terminates both threads */
171 }
172 .fi
173 .SH 関連項目
174 \fBclock_gettime\fP(2), \fBclock_settime\fP(2), \fBtimer_create\fP(2),
175 \fBclock_getcpuclockid\fP(3), \fBpthread_self\fP(3), \fBpthreads\fP(7), \fBtime\fP(7)
176 .SH この文書について
177 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部
178 である。プロジェクトの説明とバグ報告に関する情報は
179 http://www.kernel.org/doc/man\-pages/ に書かれている。