man-pages/man3/clock_getcpuclockid.3.html
2021-03-31 01:06:50 +01:00

245 lines
6.5 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of CLOCK_GETCPUCLOCKID</TITLE>
</HEAD><BODY>
<H1>CLOCK_GETCPUCLOCKID</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
clock_getcpuclockid - obtain ID of a process CPU-time clock
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;</B>
<PRE>
<B>int clock_getcpuclockid(pid_t </B><I>pid</I><B>, clockid_t *</B><I>clock_id</I><B>);</B>
</PRE>
<P>
Link with <I>-lrt</I> (only for glibc versions before 2.17).
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>clock_getcpuclockid</B>():
<DL COMPACT><DT id="1"><DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>clock_getcpuclockid</B>()
function obtains the ID of the CPU-time clock of the process whose ID is
<I>pid</I>,
and returns it in the location pointed to by
<I>clock_id</I>.
If
<I>pid</I>
is zero, then the clock ID of the CPU-time clock
of the calling process is returned.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>clock_getcpuclockid</B>()
returns 0;
on error, it returns one of the positive error numbers listed in ERRORS.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="2"><B>ENOSYS</B>
<DD>
The kernel does not support obtaining the per-process
CPU-time clock of another process, and
<I>pid</I>
does not specify the calling process.
<DT id="3"><B>EPERM</B>
<DD>
The caller does not have permission to access
the CPU-time clock of the process specified by
<I>pid</I>.
(Specified in POSIX.1-2001;
does not occur on Linux unless the kernel does not support
obtaining the per-process CPU-time clock of another process.)
<DT id="4"><B>ESRCH</B>
<DD>
There is no process with the ID
<I>pid</I>.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
The
<B>clock_getcpuclockid</B>()
function is available in glibc since version 2.2.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>clock_getcpuclockid</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
Calling
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2)
with the clock ID obtained by a call to
<B>clock_getcpuclockid</B>()
with a
<I>pid</I>
of 0,
is the same as using the clock ID
<B>CLOCK_PROCESS_CPUTIME_ID</B>.
<A NAME="lbAK">&nbsp;</A>
<H2>EXAMPLE</H2>
The example program below obtains the
CPU-time clock ID of the process whose ID is given on the command line,
and then uses
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2)
to obtain the time on that clock.
An example run is the following:
<P>
$<B> ./a.out 1</B> # Show CPU clock of init process
CPU-time clock for PID 1 is 2.213466748 seconds
<A NAME="lbAL">&nbsp;</A>
<H3>Program source</H3>
#define _XOPEN_SOURCE 600
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;clockid_t&nbsp;clockid;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;timespec&nbsp;ts;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;%s&nbsp;&lt;process-ID&gt;\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(clock_getcpuclockid(atoi(argv[1]),&nbsp;&amp;clockid)&nbsp;!=&nbsp;0)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;clock_getcpuclockid&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(clock_gettime(clockid,&nbsp;&amp;ts)&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;clock_gettime&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;CPU-time&nbsp;clock&nbsp;for&nbsp;PID&nbsp;%s&nbsp;is&nbsp;%ld.%09ld&nbsp;seconds\n&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;argv[1],&nbsp;(long)&nbsp;ts.tv_sec,&nbsp;(long)&nbsp;ts.tv_nsec);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+clock_getres">clock_getres</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+timer_create">timer_create</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_getcpuclockid">pthread_getcpuclockid</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+time">time</A></B>(7)
<A NAME="lbAN">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="10"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="11"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="12"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="14"><A HREF="#lbAK">EXAMPLE</A><DD>
<DL>
<DT id="15"><A HREF="#lbAL">Program source</A><DD>
</DL>
<DT id="16"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="17"><A HREF="#lbAN">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:37 GMT, March 31, 2021
</BODY>
</HTML>