pthread_setschedparam (3) - Linux Manuals
pthread_setschedparam: set/get
NAME
pthread_setschedparam, pthread_getschedparam - set/get scheduling policy and parameters of a thread
SYNOPSIS
#include <pthread.h> int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param); int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param); Compile and link with -pthread.
DESCRIPTION
The pthread_setschedparam() function sets the scheduling policy and parameters of the thread thread.policy specifies the new scheduling policy for thread. The supported values for policy, and their semantics, are described in sched(7).
The structure pointed to by param specifies the new scheduling parameters for thread. Scheduling parameters are maintained in the following structure:
struct sched_param {
As can be seen, only one scheduling parameter is supported.
For details of the permitted ranges for scheduling priorities
in each scheduling policy, see
sched(7).
The
pthread_getschedparam()
function returns the scheduling policy and parameters of the thread
thread,
in the buffers pointed to by
policy
and
param,
respectively.
The returned priority value is that set by the most recent
pthread_setschedparam(),
pthread_setschedprio(3),
or
pthread_create(3)
call that affected
thread.
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,
pthread_mutexattr_setprioceiling(3)
and
pthread_mutexattr_setprotocol(3)).
pthread_setschedparam()
may additionally fail with the following errors:
POSIX.1 also documents an
ENOTSUP
("attempt was made to set the policy or scheduling parameters
to an unsupported value") error for
pthread_setschedparam().
In the following run, the main thread sets its scheduling policy to
SCHED_FIFO
with a priority of 10,
and initializes a thread attributes object with
a scheduling policy attribute of
SCHED_RR
and a scheduling priority attribute of 20.
The program then sets (using
pthread_attr_setinheritsched(3))
the inherit scheduler attribute of the thread attributes object to
PTHREAD_EXPLICIT_SCHED,
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.
$ su # Need privilege to set real-time scheduling policies
Password:
# ./a.out -mf10 -ar20 -i e
Scheduler settings of main thread
Scheduler settings in 'attr'
Scheduler attributes of new thread
In the above output, one can see that the scheduling policy and priority
were taken from the values specified in the thread attributes object.
The next run is the same as the previous,
except that the inherit scheduler attribute is set to
PTHREAD_INHERIT_SCHED,
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.
# ./a.out -mf10 -ar20 -i i
Scheduler settings of main thread
Scheduler settings in 'attr'
Scheduler attributes of new thread
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.
Note that if we had omitted the
-i i
option, the output would have been the same, since
PTHREAD_INHERIT_SCHED
is the default for the inherit scheduler attribute.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define handle_error_en(en, msg) \
static void
usage(char *prog_name, char *msg)
{
static int
get_policy(char p, int *policy)
{
RETURN VALUE
On success, these functions return 0;
on error, they return a nonzero error number.
If
pthread_setschedparam()
fails, the scheduling policy and parameters of
thread
are not changed.
ERRORS
Both of these functions can fail with the following error:
ATTRIBUTES
For an explanation of the terms used in this section, see
attributes(7).
Interface Attribute Value
pthread_setschedparam(),
pthread_getschedparam()
Thread safety MT-Safe CONFORMING TO
POSIX.1-2001, POSIX.1-2008.
NOTES
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
sched(7).
EXAMPLES
The program below demonstrates the use of
pthread_setschedparam()
and
pthread_getschedparam(),
as well as the use of a number of other scheduling-related
pthreads functions.
Program source
/* pthreads_sched_test.c */