575 lines
24 KiB
HTML
575 lines
24 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of PTHREAD_SETSCHEDPARAM</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>PTHREAD_SETSCHEDPARAM</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_setschedparam, pthread_getschedparam - set/get
|
|
scheduling policy and parameters of a thread
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>></B>
|
|
|
|
<B>int pthread_setschedparam(pthread_t </B><I>thread</I><B>, int </B><I>policy</I><B>,</B>
|
|
<B> const struct sched_param *</B><I>param</I><B>);</B>
|
|
<B>int pthread_getschedparam(pthread_t </B><I>thread</I><B>, int *</B><I>policy</I><B>,</B>
|
|
<B> struct sched_param *</B><I>param</I><B>);</B>
|
|
|
|
Compile and link with <I>-pthread</I>.
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>pthread_setschedparam</B>()
|
|
|
|
function sets the scheduling policy and parameters of the thread
|
|
<I>thread</I>.
|
|
|
|
<P>
|
|
|
|
<I>policy</I>
|
|
|
|
specifies the new scheduling policy for
|
|
<I>thread</I>.
|
|
|
|
The supported values for
|
|
<I>policy</I>,
|
|
|
|
and their semantics, are described in
|
|
<B><A HREF="/cgi-bin/man/man2html?7+sched">sched</A></B>(7).
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
The structure pointed to by
|
|
<I>param</I>
|
|
|
|
specifies the new scheduling parameters for
|
|
<I>thread</I>.
|
|
|
|
Scheduling parameters are maintained in the following structure:
|
|
<P>
|
|
|
|
|
|
|
|
struct sched_param {
|
|
<BR> int sched_priority; /* Scheduling priority */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
As can be seen, only one scheduling parameter is supported.
|
|
For details of the permitted ranges for scheduling priorities
|
|
in each scheduling policy, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+sched">sched</A></B>(7).
|
|
|
|
<P>
|
|
|
|
The
|
|
<B>pthread_getschedparam</B>()
|
|
|
|
function returns the scheduling policy and parameters of the thread
|
|
<I>thread</I>,
|
|
|
|
in the buffers pointed to by
|
|
<I>policy</I>
|
|
|
|
and
|
|
<I>param</I>,
|
|
|
|
respectively.
|
|
The returned priority value is that set by the most recent
|
|
<B>pthread_setschedparam</B>(),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_setschedprio">pthread_setschedprio</A></B>(3),
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3)
|
|
|
|
call that affected
|
|
<I>thread</I>.
|
|
|
|
The returned priority does not reflect any temporary priority adjustments
|
|
as a result of calls to any priority inheritance or
|
|
priority ceiling functions (see, for example,
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutexattr_setprioceiling">pthread_mutexattr_setprioceiling</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutexattr_setprotocol">pthread_mutexattr_setprotocol</A></B>(3)).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, these functions return 0;
|
|
on error, they return a nonzero error number.
|
|
If
|
|
<B>pthread_setschedparam</B>()
|
|
|
|
fails, the scheduling policy and parameters of
|
|
<I>thread</I>
|
|
|
|
are not changed.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
Both of these functions can fail with the following error:
|
|
<DL COMPACT>
|
|
<DT id="1"><B>ESRCH</B>
|
|
|
|
<DD>
|
|
No thread with the ID
|
|
<I>thread</I>
|
|
|
|
could be found.
|
|
</DL>
|
|
<P>
|
|
|
|
<B>pthread_setschedparam</B>()
|
|
|
|
may additionally fail with the following errors:
|
|
<DL COMPACT>
|
|
<DT id="2"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
<I>policy</I>
|
|
|
|
is not a recognized policy, or
|
|
<I>param</I>
|
|
|
|
does not make sense for the
|
|
<I>policy</I>.
|
|
|
|
<DT id="3"><B>EPERM</B>
|
|
|
|
<DD>
|
|
The caller does not have appropriate privileges
|
|
to set the specified scheduling policy and parameters.
|
|
</DL>
|
|
<P>
|
|
|
|
POSIX.1 also documents an
|
|
<B>ENOTSUP</B>
|
|
|
|
("attempt was made to set the policy or scheduling parameters
|
|
to an unsupported value") error for
|
|
<B>pthread_setschedparam</B>().
|
|
|
|
|
|
|
|
<A NAME="lbAG"> </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_setschedparam</B>(),
|
|
|
|
<B>pthread_getschedparam</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
For a description of the permissions required to, and the effect of,
|
|
changing a thread's scheduling policy and priority,
|
|
and details of the permitted ranges for priorities
|
|
in each scheduling policy, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+sched">sched</A></B>(7).
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below demonstrates the use of
|
|
<B>pthread_setschedparam</B>()
|
|
|
|
and
|
|
<B>pthread_getschedparam</B>(),
|
|
|
|
as well as the use of a number of other scheduling-related
|
|
pthreads functions.
|
|
<P>
|
|
|
|
In the following run, the main thread sets its scheduling policy to
|
|
<B>SCHED_FIFO</B>
|
|
|
|
with a priority of 10,
|
|
and initializes a thread attributes object with
|
|
a scheduling policy attribute of
|
|
<B>SCHED_RR</B>
|
|
|
|
and a scheduling priority attribute of 20.
|
|
The program then sets (using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setinheritsched">pthread_attr_setinheritsched</A></B>(3))
|
|
|
|
the inherit scheduler attribute of the thread attributes object to
|
|
<B>PTHREAD_EXPLICIT_SCHED</B>,
|
|
|
|
meaning that threads created using this attributes object should
|
|
take their scheduling attributes from the thread attributes object.
|
|
The program then creates a thread using the thread attributes object,
|
|
and that thread displays its scheduling policy and priority.
|
|
<P>
|
|
|
|
|
|
|
|
$ <B>su</B> # Need privilege to set real-time scheduling policies
|
|
Password:
|
|
# <B>./a.out -mf10 -ar20 -i e</B>
|
|
Scheduler settings of main thread
|
|
<BR> policy=SCHED_FIFO, priority=10
|
|
<P>
|
|
Scheduler settings in 'attr'
|
|
<BR> policy=SCHED_RR, priority=20
|
|
<BR> inheritsched is EXPLICIT
|
|
<P>
|
|
Scheduler attributes of new thread
|
|
<BR> policy=SCHED_RR, priority=20
|
|
|
|
|
|
<P>
|
|
|
|
In the above output, one can see that the scheduling policy and priority
|
|
were taken from the values specified in the thread attributes object.
|
|
<P>
|
|
|
|
The next run is the same as the previous,
|
|
except that the inherit scheduler attribute is set to
|
|
<B>PTHREAD_INHERIT_SCHED</B>,
|
|
|
|
meaning that threads created using the thread attributes object should
|
|
ignore the scheduling attributes specified in the attributes object
|
|
and instead take their scheduling attributes from the creating thread.
|
|
<P>
|
|
|
|
|
|
|
|
# <B>./a.out -mf10 -ar20 -i i</B>
|
|
Scheduler settings of main thread
|
|
<BR> policy=SCHED_FIFO, priority=10
|
|
<P>
|
|
Scheduler settings in 'attr'
|
|
<BR> policy=SCHED_RR, priority=20
|
|
<BR> inheritsched is INHERIT
|
|
<P>
|
|
Scheduler attributes of new thread
|
|
<BR> policy=SCHED_FIFO, priority=10
|
|
|
|
|
|
<P>
|
|
|
|
In the above output, one can see that the scheduling policy and priority
|
|
were taken from the creating thread,
|
|
rather than the thread attributes object.
|
|
<P>
|
|
|
|
Note that if we had omitted the
|
|
<I>-i i</I>
|
|
|
|
option, the output would have been the same, since
|
|
<B>PTHREAD_INHERIT_SCHED</B>
|
|
|
|
is the default for the inherit scheduler attribute.
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
/* pthreads_sched_test.c */
|
|
<P>
|
|
#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/unistd.h">unistd.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>
|
|
static void
|
|
usage(char *prog_name, char *msg)
|
|
{
|
|
<BR> if (msg != NULL)
|
|
<BR> fputs(msg, stderr);
|
|
<P>
|
|
<BR> fprintf(stderr, "Usage: %s [options]\n", prog_name);
|
|
<BR> fprintf(stderr, "Options are:\n");
|
|
#define fpe(msg) fprintf(stderr, "\t%s", msg); /* Shorter */
|
|
<BR> fpe("-a<policy><prio> Set scheduling policy and priority in\n");
|
|
<BR> fpe(" thread attributes object\n");
|
|
<BR> fpe(" <policy> can be\n");
|
|
<BR> fpe(" f SCHED_FIFO\n");
|
|
<BR> fpe(" r SCHED_RR\n");
|
|
<BR> fpe(" o SCHED_OTHER\n");
|
|
<BR> fpe("-A Use default thread attributes object\n");
|
|
<BR> fpe("-i {e|i} Set inherit scheduler attribute to\n");
|
|
<BR> fpe(" 'explicit' or 'inherit'\n");
|
|
<BR> fpe("-m<policy><prio> Set scheduling policy and priority on\n");
|
|
<BR> fpe(" main thread before pthread_create() call\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
}
|
|
<P>
|
|
static int
|
|
get_policy(char p, int *policy)
|
|
{
|
|
<BR> switch (p) {
|
|
<BR> case 'f': *policy = SCHED_FIFO; return 1;
|
|
<BR> case 'r': *policy = SCHED_RR; return 1;
|
|
<BR> case 'o': *policy = SCHED_OTHER; return 1;
|
|
<BR> default: return 0;
|
|
<BR> }
|
|
}
|
|
<P>
|
|
static void
|
|
display_sched_attr(int policy, struct sched_param *param)
|
|
{
|
|
<BR> printf(" policy=%s, priority=%d\n",
|
|
<BR> (policy == SCHED_FIFO) ? "SCHED_FIFO" :
|
|
<BR> (policy == SCHED_RR) ? "SCHED_RR" :
|
|
<BR> (policy == SCHED_OTHER) ? "SCHED_OTHER" :
|
|
<BR> "???",
|
|
<BR> param->sched_priority);
|
|
}
|
|
<P>
|
|
static void
|
|
display_thread_sched_attr(char *msg)
|
|
{
|
|
<BR> int policy, s;
|
|
<BR> struct sched_param param;
|
|
<P>
|
|
<BR> s = pthread_getschedparam(pthread_self(), &policy, &param);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_getschedparam");
|
|
<P>
|
|
<BR> printf("%s\n", msg);
|
|
<BR> display_sched_attr(policy, &param);
|
|
}
|
|
<P>
|
|
static void *
|
|
thread_start(void *arg)
|
|
{
|
|
<BR> display_thread_sched_attr("Scheduler attributes of new thread");
|
|
<P>
|
|
<BR> return NULL;
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int s, opt, inheritsched, use_null_attrib, policy;
|
|
<BR> pthread_t thread;
|
|
<BR> pthread_attr_t attr;
|
|
<BR> pthread_attr_t *attrp;
|
|
<BR> char *attr_sched_str, *main_sched_str, *inheritsched_str;
|
|
<BR> struct sched_param param;
|
|
<P>
|
|
<BR> /* Process command-line options */
|
|
<P>
|
|
<BR> use_null_attrib = 0;
|
|
<BR> attr_sched_str = NULL;
|
|
<BR> main_sched_str = NULL;
|
|
<BR> inheritsched_str = NULL;
|
|
<P>
|
|
<BR> while ((opt = getopt(argc, argv, "a:Ai:m:")) != -1) {
|
|
<BR> switch (opt) {
|
|
<BR> case 'a': attr_sched_str = optarg; break;
|
|
<BR> case 'A': use_null_attrib = 1; break;
|
|
<BR> case 'i': inheritsched_str = optarg; break;
|
|
<BR> case 'm': main_sched_str = optarg; break;
|
|
<BR> default: usage(argv[0], "Unrecognized option\n");
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> if (use_null_attrib &&
|
|
<BR> (inheritsched_str != NULL || attr_sched_str != NULL))
|
|
<BR> usage(argv[0], "Can't specify -A with -i or -a\n");
|
|
<P>
|
|
<BR> /* Optionally set scheduling attributes of main thread,
|
|
<BR> and display the attributes */
|
|
<P>
|
|
<BR> if (main_sched_str != NULL) {
|
|
<BR> if (!get_policy(main_sched_str[0], &policy))
|
|
<BR> usage(argv[0], "Bad policy for main thread (-m)\n");
|
|
<BR> param.sched_priority = strtol(&main_sched_str[1], NULL, 0);
|
|
<P>
|
|
<BR> s = pthread_setschedparam(pthread_self(), policy, &param);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_setschedparam");
|
|
<BR> }
|
|
<P>
|
|
<BR> display_thread_sched_attr("Scheduler settings of main thread");
|
|
<BR> printf("\n");
|
|
<P>
|
|
<BR> /* Initialize thread attributes object according to options */
|
|
<P>
|
|
<BR> attrp = NULL;
|
|
<P>
|
|
<BR> if (!use_null_attrib) {
|
|
<BR> s = pthread_attr_init(&attr);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_init");
|
|
<BR> attrp = &attr;
|
|
<BR> }
|
|
<P>
|
|
<BR> if (inheritsched_str != NULL) {
|
|
<BR> if (inheritsched_str[0] == 'e')
|
|
<BR> inheritsched = PTHREAD_EXPLICIT_SCHED;
|
|
<BR> else if (inheritsched_str[0] == 'i')
|
|
<BR> inheritsched = PTHREAD_INHERIT_SCHED;
|
|
<BR> else
|
|
<BR> usage(argv[0], "Value for -i must be 'e' or 'i'\n");
|
|
<P>
|
|
<BR> s = pthread_attr_setinheritsched(&attr, inheritsched);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_setinheritsched");
|
|
<BR> }
|
|
<P>
|
|
<BR> if (attr_sched_str != NULL) {
|
|
<BR> if (!get_policy(attr_sched_str[0], &policy))
|
|
<BR> usage(argv[0],
|
|
<BR> "Bad policy for 'attr' (-a)\n");
|
|
<BR> param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);
|
|
<P>
|
|
<BR> s = pthread_attr_setschedpolicy(&attr, policy);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_setschedpolicy");
|
|
<BR> s = pthread_attr_setschedparam(&attr, &param);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_setschedparam");
|
|
<BR> }
|
|
<P>
|
|
<BR> /* If we initialized a thread attributes object, display
|
|
<BR> the scheduling attributes that were set in the object */
|
|
<P>
|
|
<BR> if (attrp != NULL) {
|
|
<BR> s = pthread_attr_getschedparam(&attr, &param);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getschedparam");
|
|
<BR> s = pthread_attr_getschedpolicy(&attr, &policy);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getschedpolicy");
|
|
<P>
|
|
<BR> printf("Scheduler settings in 'attr'\n");
|
|
<BR> display_sched_attr(policy, &param);
|
|
<P>
|
|
<BR> s = pthread_attr_getinheritsched(&attr, &inheritsched);
|
|
<BR> printf(" inheritsched is %s\n",
|
|
<BR> (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
|
|
<BR> (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
|
|
<BR> "???");
|
|
<BR> printf("\n");
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Create a thread that will display its scheduling attributes */
|
|
<P>
|
|
<BR> s = pthread_create(&thread, attrp, &thread_start, NULL);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_create");
|
|
<P>
|
|
<BR> /* Destroy unneeded thread attributes object */
|
|
<P>
|
|
<BR> if (!use_null_attrib) {
|
|
<BR> s = pthread_attr_destroy(&attr);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_destroy");
|
|
<BR> }
|
|
<P>
|
|
<BR> s = pthread_join(thread, NULL);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_join");
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sched_get_priority_min">sched_get_priority_min</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_init">pthread_attr_init</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setinheritsched">pthread_attr_setinheritsched</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setschedparam">pthread_attr_setschedparam</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setschedpolicy">pthread_attr_setschedpolicy</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</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+pthread_setschedprio">pthread_setschedprio</A></B>(3),
|
|
|
|
<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="4"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="5"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="6"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="7"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="8"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="9"><A HREF="#lbAG">ATTRIBUTES</A><DD>
|
|
<DT id="10"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="11"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DT id="12"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="13"><A HREF="#lbAK">Program source</A><DD>
|
|
</DL>
|
|
<DT id="14"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="15"><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>
|