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

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">&nbsp;</A>
<H2>NAME</H2>
pthread_setschedparam, pthread_getschedparam - set/get
scheduling policy and parameters of a thread
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;</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">&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;sched_priority;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Scheduling&nbsp;priority&nbsp;*/
};
<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">&nbsp;</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">&nbsp;</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>
(&quot;attempt was made to set the policy or scheduling parameters
to an unsupported value&quot;) error for
<B>pthread_setschedparam</B>().
<A NAME="lbAG">&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_setschedparam</B>(),
<B>pthread_getschedparam</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAI">&nbsp;</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">&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;policy=SCHED_FIFO,&nbsp;priority=10
<P>
Scheduler settings in 'attr'
<BR>&nbsp;&nbsp;&nbsp;&nbsp;policy=SCHED_RR,&nbsp;priority=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;inheritsched&nbsp;is&nbsp;EXPLICIT
<P>
Scheduler attributes of new thread
<BR>&nbsp;&nbsp;&nbsp;&nbsp;policy=SCHED_RR,&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;policy=SCHED_FIFO,&nbsp;priority=10
<P>
Scheduler settings in 'attr'
<BR>&nbsp;&nbsp;&nbsp;&nbsp;policy=SCHED_RR,&nbsp;priority=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;inheritsched&nbsp;is&nbsp;INHERIT
<P>
Scheduler attributes of new thread
<BR>&nbsp;&nbsp;&nbsp;&nbsp;policy=SCHED_FIFO,&nbsp;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&nbsp;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">&nbsp;</A>
<H3>Program source</H3>
/* pthreads_sched_test.c */
<P>
#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/unistd.h">unistd.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>
static void
usage(char *prog_name, char *msg)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(msg&nbsp;!=&nbsp;NULL)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fputs(msg,&nbsp;stderr);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;[options]\n&quot;,&nbsp;prog_name);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Options&nbsp;are:\n&quot;);
#define fpe(msg) fprintf(stderr, &quot;\t%s&quot;, msg); /* Shorter */
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;-a&lt;policy&gt;&lt;prio&gt;&nbsp;Set&nbsp;scheduling&nbsp;policy&nbsp;and&nbsp;priority&nbsp;in\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thread&nbsp;attributes&nbsp;object\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;policy&gt;&nbsp;can&nbsp;be\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f&nbsp;&nbsp;SCHED_FIFO\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;&nbsp;SCHED_RR\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o&nbsp;&nbsp;SCHED_OTHER\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;-A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Use&nbsp;default&nbsp;thread&nbsp;attributes&nbsp;object\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;-i&nbsp;{e|i}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set&nbsp;inherit&nbsp;scheduler&nbsp;attribute&nbsp;to\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'explicit'&nbsp;or&nbsp;'inherit'\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;-m&lt;policy&gt;&lt;prio&gt;&nbsp;Set&nbsp;scheduling&nbsp;policy&nbsp;and&nbsp;priority&nbsp;on\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fpe(&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main&nbsp;thread&nbsp;before&nbsp;pthread_create()&nbsp;call\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
}
<P>
static int
get_policy(char p, int *policy)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(p)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'f':&nbsp;*policy&nbsp;=&nbsp;SCHED_FIFO;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'r':&nbsp;*policy&nbsp;=&nbsp;SCHED_RR;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'o':&nbsp;*policy&nbsp;=&nbsp;SCHED_OTHER;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;default:&nbsp;&nbsp;return&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
}
<P>
static void
display_sched_attr(int policy, struct sched_param *param)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;&nbsp;&nbsp;policy=%s,&nbsp;priority=%d\n&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(policy&nbsp;==&nbsp;SCHED_FIFO)&nbsp;&nbsp;?&nbsp;&quot;SCHED_FIFO&quot;&nbsp;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(policy&nbsp;==&nbsp;SCHED_RR)&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;&quot;SCHED_RR&quot;&nbsp;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(policy&nbsp;==&nbsp;SCHED_OTHER)&nbsp;?&nbsp;&quot;SCHED_OTHER&quot;&nbsp;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;???&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;param-&gt;sched_priority);
}
<P>
static void
display_thread_sched_attr(char *msg)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;policy,&nbsp;s;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;sched_param&nbsp;param;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_getschedparam(pthread_self(),&nbsp;&amp;policy,&nbsp;&amp;param);
<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_getschedparam&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s\n&quot;,&nbsp;msg);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;display_sched_attr(policy,&nbsp;&amp;param);
}
<P>
static void *
thread_start(void *arg)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;display_thread_sched_attr(&quot;Scheduler&nbsp;attributes&nbsp;of&nbsp;new&nbsp;thread&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;NULL;
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;s,&nbsp;opt,&nbsp;inheritsched,&nbsp;use_null_attrib,&nbsp;policy;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;thread;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_attr_t&nbsp;attr;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_attr_t&nbsp;*attrp;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*attr_sched_str,&nbsp;*main_sched_str,&nbsp;*inheritsched_str;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;sched_param&nbsp;param;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Process&nbsp;command-line&nbsp;options&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;use_null_attrib&nbsp;=&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;attr_sched_str&nbsp;=&nbsp;NULL;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;main_sched_str&nbsp;=&nbsp;NULL;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;inheritsched_str&nbsp;=&nbsp;NULL;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((opt&nbsp;=&nbsp;getopt(argc,&nbsp;argv,&nbsp;&quot;a:Ai:m:&quot;))&nbsp;!=&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(opt)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'a':&nbsp;attr_sched_str&nbsp;=&nbsp;optarg;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'A':&nbsp;use_null_attrib&nbsp;=&nbsp;1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'i':&nbsp;inheritsched_str&nbsp;=&nbsp;optarg;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'m':&nbsp;main_sched_str&nbsp;=&nbsp;optarg;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:&nbsp;&nbsp;usage(argv[0],&nbsp;&quot;Unrecognized&nbsp;option\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(use_null_attrib&nbsp;&amp;&amp;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(inheritsched_str&nbsp;!=&nbsp;NULL&nbsp;||&nbsp;attr_sched_str&nbsp;!=&nbsp;NULL))
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usage(argv[0],&nbsp;&quot;Can't&nbsp;specify&nbsp;-A&nbsp;with&nbsp;-i&nbsp;or&nbsp;-a\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Optionally&nbsp;set&nbsp;scheduling&nbsp;attributes&nbsp;of&nbsp;main&nbsp;thread,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;display&nbsp;the&nbsp;attributes&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(main_sched_str&nbsp;!=&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!get_policy(main_sched_str[0],&nbsp;&amp;policy))
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usage(argv[0],&nbsp;&quot;Bad&nbsp;policy&nbsp;for&nbsp;main&nbsp;thread&nbsp;(-m)\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;param.sched_priority&nbsp;=&nbsp;strtol(&amp;main_sched_str[1],&nbsp;NULL,&nbsp;0);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_setschedparam(pthread_self(),&nbsp;policy,&nbsp;&amp;param);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_setschedparam&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;display_thread_sched_attr(&quot;Scheduler&nbsp;settings&nbsp;of&nbsp;main&nbsp;thread&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Initialize&nbsp;thread&nbsp;attributes&nbsp;object&nbsp;according&nbsp;to&nbsp;options&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;attrp&nbsp;=&nbsp;NULL;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!use_null_attrib)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_attr_init(&amp;attr);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_attr_init&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrp&nbsp;=&nbsp;&amp;attr;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(inheritsched_str&nbsp;!=&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(inheritsched_str[0]&nbsp;==&nbsp;'e')
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inheritsched&nbsp;=&nbsp;PTHREAD_EXPLICIT_SCHED;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(inheritsched_str[0]&nbsp;==&nbsp;'i')
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inheritsched&nbsp;=&nbsp;PTHREAD_INHERIT_SCHED;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usage(argv[0],&nbsp;&quot;Value&nbsp;for&nbsp;-i&nbsp;must&nbsp;be&nbsp;'e'&nbsp;or&nbsp;'i'\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_attr_setinheritsched(&amp;attr,&nbsp;inheritsched);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_attr_setinheritsched&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(attr_sched_str&nbsp;!=&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!get_policy(attr_sched_str[0],&nbsp;&amp;policy))
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usage(argv[0],
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Bad&nbsp;policy&nbsp;for&nbsp;'attr'&nbsp;(-a)\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;param.sched_priority&nbsp;=&nbsp;strtol(&amp;attr_sched_str[1],&nbsp;NULL,&nbsp;0);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_attr_setschedpolicy(&amp;attr,&nbsp;policy);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_attr_setschedpolicy&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_attr_setschedparam(&amp;attr,&nbsp;&amp;param);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_attr_setschedparam&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;If&nbsp;we&nbsp;initialized&nbsp;a&nbsp;thread&nbsp;attributes&nbsp;object,&nbsp;display
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;scheduling&nbsp;attributes&nbsp;that&nbsp;were&nbsp;set&nbsp;in&nbsp;the&nbsp;object&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(attrp&nbsp;!=&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_attr_getschedparam(&amp;attr,&nbsp;&amp;param);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_attr_getschedparam&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_attr_getschedpolicy(&amp;attr,&nbsp;&amp;policy);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_attr_getschedpolicy&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Scheduler&nbsp;settings&nbsp;in&nbsp;'attr'\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display_sched_attr(policy,&nbsp;&amp;param);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_attr_getinheritsched(&amp;attr,&nbsp;&amp;inheritsched);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;&nbsp;&nbsp;inheritsched&nbsp;is&nbsp;%s\n&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(inheritsched&nbsp;==&nbsp;PTHREAD_INHERIT_SCHED)&nbsp;&nbsp;?&nbsp;&quot;INHERIT&quot;&nbsp;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(inheritsched&nbsp;==&nbsp;PTHREAD_EXPLICIT_SCHED)&nbsp;?&nbsp;&quot;EXPLICIT&quot;&nbsp;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;???&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Create&nbsp;a&nbsp;thread&nbsp;that&nbsp;will&nbsp;display&nbsp;its&nbsp;scheduling&nbsp;attributes&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_create(&amp;thread,&nbsp;attrp,&nbsp;&amp;thread_start,&nbsp;NULL);
<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_create&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Destroy&nbsp;unneeded&nbsp;thread&nbsp;attributes&nbsp;object&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!use_null_attrib)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_attr_destroy(&amp;attr);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_attr_destroy&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_join(thread,&nbsp;NULL);
<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_join&quot;);
<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+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">&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="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>