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

321 lines
9.5 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of PTHREAD_SETAFFINITY_NP</TITLE>
</HEAD><BODY>
<H1>PTHREAD_SETAFFINITY_NP</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>
pthread_setaffinity_np, pthread_getaffinity_np - set/get
CPU affinity of a thread
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#define _GNU_SOURCE</B> /* See <A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A>(7) */
<B>#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;</B>
<B>int pthread_setaffinity_np(pthread_t </B><I>thread</I><B>, size_t </B><I>cpusetsize</I><B>,</B>
<B> const cpu_set_t *</B><I>cpuset</I><B>);</B>
<B>int pthread_getaffinity_np(pthread_t </B><I>thread</I><B>, size_t </B><I>cpusetsize</I><B>,</B>
<B> cpu_set_t *</B><I>cpuset</I><B>);</B>
Compile and link with <I>-pthread</I>.
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>pthread_setaffinity_np</B>()
function
sets the CPU affinity mask of the thread
<I>thread</I>
to the CPU set pointed to by
<I>cpuset</I>.
If the call is successful,
and the thread is not currently running on one of the CPUs in
<I>cpuset</I>,
then it is migrated to one of those CPUs.
<P>
The
<B>pthread_getaffinity_np</B>()
function returns the CPU affinity mask of the thread
<I>thread</I>
in the buffer pointed to by
<I>cpuset</I>.
<P>
For more details on CPU affinity masks, see
<B><A HREF="/cgi-bin/man/man2html?2+sched_setaffinity">sched_setaffinity</A></B>(2).
For a description of a set of macros
that can be used to manipulate and inspect CPU sets, see
<B><A HREF="/cgi-bin/man/man2html?3+CPU_SET">CPU_SET</A></B>(3).
<P>
The argument
<I>cpusetsize</I>
is the length (in bytes) of the buffer pointed to by
<I>cpuset</I>.
Typically, this argument would be specified as
<I>sizeof(cpu_set_t)</I>.
(It may be some other value, if using the macros described in
<B><A HREF="/cgi-bin/man/man2html?3+CPU_SET">CPU_SET</A></B>(3)
for dynamically allocating a CPU set.)
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, these functions return 0;
on error, they return a nonzero error number.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EFAULT</B>
<DD>
A supplied memory address was invalid.
<DT id="2"><B>EINVAL</B>
<DD>
(<B>pthread_setaffinity_np</B>())
The affinity bit mask
<I>mask</I>
contains no processors that are currently physically on the system
and permitted to the thread according to any restrictions that
may be imposed by the &quot;cpuset&quot; mechanism described in
<B><A HREF="/cgi-bin/man/man2html?7+cpuset">cpuset</A></B>(7).
<DT id="3"><B>EINVAL</B>
<DD>
(<B>pthread_setaffinity_np</B>())
<I>cpuset</I>
specified a CPU that was outside the set supported by the kernel.
(The kernel configuration option
<B>CONFIG_NR_CPUS</B>
defines the range of the set supported by the kernel data type
used to represent CPU sets.)
<DT id="4"><B>EINVAL</B>
<DD>
(<B>pthread_getaffinity_np</B>())
<I>cpusetsize</I>
is smaller than the size of the affinity mask used by the kernel.
<DT id="5"><B>ESRCH</B>
<DD>
No thread with the ID
<I>thread</I>
could be found.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
These functions are provided by glibc since version 2.3.4.
<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>pthread_setaffinity_np</B>(),
<B>pthread_getaffinity_np</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
These functions are nonstandard GNU extensions;
hence the suffix &quot;_np&quot; (nonportable) in the names.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
After a call to
<B>pthread_setaffinity_np</B>(),
the set of CPUs on which the thread will actually run is
the intersection of the set specified in the
<I>cpuset</I>
argument and the set of CPUs actually present on the system.
The system may further restrict the set of CPUs on which the thread
runs if the &quot;cpuset&quot; mechanism described in
<B><A HREF="/cgi-bin/man/man2html?7+cpuset">cpuset</A></B>(7)
is being used.
These restrictions on the actual set of CPUs on which the thread
will run are silently imposed by the kernel.
<P>
These functions are implemented on top of the
<B><A HREF="/cgi-bin/man/man2html?2+sched_setaffinity">sched_setaffinity</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+sched_getaffinity">sched_getaffinity</A></B>(2)
system calls.
<P>
In glibc 2.3.3 only,
versions of these functions were provided that did not have a
<I>cpusetsize</I>
argument.
Instead the CPU set size given to the underlying system calls was always
<I>sizeof(cpu_set_t)</I>.
<P>
A new thread created by
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3)
inherits a copy of its creator's CPU affinity mask.
<A NAME="lbAK">&nbsp;</A>
<H2>EXAMPLE</H2>
In the following program, the main thread uses
<B>pthread_setaffinity_np</B>()
to set its CPU affinity mask to include CPUs 0 to 7
(which may not all be available on the system),
and then calls
<B>pthread_getaffinity_np</B>()
to check the resulting CPU affinity mask of the thread.
<P>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
<P>
#define handle_error_en(en, msg) \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{&nbsp;errno&nbsp;=&nbsp;en;&nbsp;perror(msg);&nbsp;exit(EXIT_FAILURE);&nbsp;}&nbsp;while&nbsp;(0)
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;s,&nbsp;j;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;cpu_set_t&nbsp;cpuset;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;thread;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;thread&nbsp;=&nbsp;pthread_self();
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Set&nbsp;affinity&nbsp;mask&nbsp;to&nbsp;include&nbsp;CPUs&nbsp;0&nbsp;to&nbsp;7&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;CPU_ZERO(&amp;cpuset);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;8;&nbsp;j++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CPU_SET(j,&nbsp;&amp;cpuset);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_setaffinity_np(thread,&nbsp;sizeof(cpu_set_t),&nbsp;&amp;cpuset);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_setaffinity_np&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Check&nbsp;the&nbsp;actual&nbsp;affinity&nbsp;mask&nbsp;assigned&nbsp;to&nbsp;the&nbsp;thread&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_getaffinity_np(thread,&nbsp;sizeof(cpu_set_t),&nbsp;&amp;cpuset);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_getaffinity_np&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Set&nbsp;returned&nbsp;by&nbsp;pthread_getaffinity_np()&nbsp;contained:\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;CPU_SETSIZE;&nbsp;j++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(CPU_ISSET(j,&nbsp;&amp;cpuset))
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;&nbsp;&nbsp;CPU&nbsp;%d\n&quot;,&nbsp;j);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+sched_setaffinity">sched_setaffinity</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+CPU_SET">CPU_SET</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setaffinity_np">pthread_attr_setaffinity_np</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_self">pthread_self</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sched_getcpu">sched_getcpu</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+cpuset">cpuset</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+sched">sched</A></B>(7)
<A NAME="lbAM">&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="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="12"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="13"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="14"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="15"><A HREF="#lbAK">EXAMPLE</A><DD>
<DT id="16"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="17"><A HREF="#lbAM">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:53 GMT, March 31, 2021
</BODY>
</HTML>