OSDN Git Service

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