321 lines
9.5 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
pthread_setaffinity_np, pthread_getaffinity_np - set/get
|
|
CPU affinity of a thread
|
|
<A NAME="lbAC"> </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 <<A HREF="file:///usr/include/pthread.h">pthread.h</A>></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"> </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"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, these functions return 0;
|
|
on error, they return a nonzero error number.
|
|
<A NAME="lbAF"> </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 "cpuset" 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"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
These functions are provided by glibc since version 2.3.4.
|
|
<A NAME="lbAH"> </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"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
These functions are nonstandard GNU extensions;
|
|
hence the suffix "_np" (nonportable) in the names.
|
|
<A NAME="lbAJ"> </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 "cpuset" 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"> </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 <<A HREF="file:///usr/include/pthread.h">pthread.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
<P>
|
|
#define handle_error_en(en, msg) \
|
|
<BR> do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int s, j;
|
|
<BR> cpu_set_t cpuset;
|
|
<BR> pthread_t thread;
|
|
<P>
|
|
<BR> thread = pthread_self();
|
|
<P>
|
|
<BR> /* Set affinity mask to include CPUs 0 to 7 */
|
|
<P>
|
|
<BR> CPU_ZERO(&cpuset);
|
|
<BR> for (j = 0; j < 8; j++)
|
|
<BR> CPU_SET(j, &cpuset);
|
|
<P>
|
|
<BR> s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_setaffinity_np");
|
|
<P>
|
|
<BR> /* Check the actual affinity mask assigned to the thread */
|
|
<P>
|
|
<BR> s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_getaffinity_np");
|
|
<P>
|
|
<BR> printf("Set returned by pthread_getaffinity_np() contained:\n");
|
|
<BR> for (j = 0; j < CPU_SETSIZE; j++)
|
|
<BR> if (CPU_ISSET(j, &cpuset))
|
|
<BR> printf(" CPU %d\n", j);
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAL"> </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"> </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"> </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>
|