Section (3) pthread_setschedparam

pthread_setschedparam(3) — Linux manual pages
Linux manual pages Section 3  


pthread_setschedparam, pthread_getschedparam — set/get scheduling policy and parameters of a thread


#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);
[Note] Note

Compile and link with −pthread.


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 {
  int   sched_priority;
/* Scheduling priority */

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)).


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.


Both of these functions can fail with the following error:


No thread with the ID thread could be found.

pthread_setschedparam() may additionally fail with the following errors:


policy is not a recognized policy, or param does not make sense for the policy.


The caller does not have appropriate privileges to set the specified scheduling policy and parameters.

POSIX.1 also documents an ENOTSUP (attempt was made to set the policy or scheduling parameters to an unsupported value) error for pthread_setschedparam().


For an explanation of the terms used in this section, see attributes(7).

Interface Attribute Value
pthread_setschedparam(), pthread_getschedparam() Thread safety MT-Safe


POSIX.1-2001, POSIX.1-2008.


For a description of the permissions required to, and the effect of, changing a thread_zsingle_quotesz_s scheduling policy and priority, and details of the permitted ranges for priorities in each scheduling policy, see sched(7).


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.

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
# ./a.out −mf10 −ar20 −i e
Scheduler settings of main thread
    policy=SCHED_FIFO, priority=10

Scheduler settings in _zsingle_quotesz_attr_zsingle_quotesz_
    policy=SCHED_RR, priority=20
    inheritsched is EXPLICIT

Scheduler attributes of new thread
    policy=SCHED_RR, priority=20

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
    policy=SCHED_FIFO, priority=10

Scheduler settings in _zsingle_quotesz_attr_zsingle_quotesz_
    policy=SCHED_RR, priority=20
    inheritsched is INHERIT

Scheduler attributes of new thread
    policy=SCHED_FIFO, priority=10

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.

Program source

/* pthreads_sched_test.c */

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define handle_error_en(en, msg) 
        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

static void
usage(char *prog_name, char *msg)
    if (msg != NULL)
        fputs(msg, stderr);

    fprintf(stderr, Usage: %s [options]
, prog_name);
    fprintf(stderr, Options are:
#define fpe(msg) fprintf(stderr, 	%s, msg);          /* Shorter */
    fpe(−a<policy><prio> Set scheduling policy and priority in
    fpe(                 thread attributes object
    fpe(                 <policy> can be
    fpe(                     f  SCHED_FIFO
    fpe(                     r  SCHED_RR
    fpe(                     o  SCHED_OTHER
    fpe(−A               Use default thread attributes object
    fpe(−i {e|i}         Set inherit scheduler attribute to
    fpe(                 _zsingle_quotesz_explicit_zsingle_quotesz_ or _zsingle_quotesz_inherit_zsingle_quotesz_
    fpe(−m<policy><prio> Set scheduling policy and priority on
    fpe(                 main thread before pthread_create() call

static int
get_policy(char p, int *policy)
    switch (p) {
    case _zsingle_quotesz_f_zsingle_quotesz_: *policy = SCHED_FIFO;     return 1;
    case _zsingle_quotesz_r_zsingle_quotesz_: *policy = SCHED_RR;       return 1;
    case _zsingle_quotesz_o_zsingle_quotesz_: *policy = SCHED_OTHER;    return 1;
    default:  return 0;

static void
display_sched_attr(int policy, struct sched_param *param)
    printf(    policy=%s, priority=%d
            (policy == SCHED_FIFO)  ? SCHED_FIFO :
            (policy == SCHED_RR)    ? SCHED_RR :
            (policy == SCHED_OTHER) ? SCHED_OTHER :

static void
display_thread_sched_attr(char *msg)
    int policy, s;
    struct sched_param param;

    s = pthread_getschedparam(pthread_self(), &policy, &param);
    if (s != 0)
        handle_error_en(s, pthread_getschedparam);

, msg);
    display_sched_attr(policy, &param);

static void *
thread_start(void *arg)
    display_thread_sched_attr(Scheduler attributes of new thread);

    return NULL;

main(int argc, char *argv[])
    int s, opt, inheritsched, use_null_attrib, policy;
    pthread_t thread;
    pthread_attr_t attr;
    pthread_attr_t *attrp;
    char *attr_sched_str, *main_sched_str, *inheritsched_str;
    struct sched_param param;

    /* Process command−line options */

    use_null_attrib = 0;
    attr_sched_str = NULL;
    main_sched_str = NULL;
    inheritsched_str = NULL;

    while ((opt = getopt(argc, argv, a:Ai:m:)) != −1) {
        switch (opt) {
        case _zsingle_quotesz_a_zsingle_quotesz_: attr_sched_str = optarg;      break;
        case _zsingle_quotesz_A_zsingle_quotesz_: use_null_attrib = 1;          break;
        case _zsingle_quotesz_i_zsingle_quotesz_: inheritsched_str = optarg;    break;
        case _zsingle_quotesz_m_zsingle_quotesz_: main_sched_str = optarg;      break;
        default:  usage(argv[0], Unrecognized option

    if (use_null_attrib &&
            (inheritsched_str != NULL || attr_sched_str != NULL))
        usage(argv[0], Can_zsingle_quotesz_t specify −A with −i or −a

    /* Optionally set scheduling attributes of main thread,
       and display the attributes */

    if (main_sched_str != NULL) {
        if (!get_policy(main_sched_str[0], &policy))
            usage(argv[0], Bad policy for main thread (−m)
        param.sched_priority = strtol(&main_sched_str[1], NULL, 0);

        s = pthread_setschedparam(pthread_self(), policy, &param);
        if (s != 0)
            handle_error_en(s, pthread_setschedparam);

    display_thread_sched_attr(Scheduler settings of main thread);

    /* Initialize thread attributes object according to options */

    attrp = NULL;

    if (!use_null_attrib) {
        s = pthread_attr_init(&attr);
        if (s != 0)
            handle_error_en(s, pthread_attr_init);
        attrp = &attr;

    if (inheritsched_str != NULL) {
        if (inheritsched_str[0] == _zsingle_quotesz_e_zsingle_quotesz_)
            inheritsched = PTHREAD_EXPLICIT_SCHED;
        else if (inheritsched_str[0] == _zsingle_quotesz_i_zsingle_quotesz_)
            inheritsched = PTHREAD_INHERIT_SCHED;
            usage(argv[0], Value for −i must be _zsingle_quotesz_e_zsingle_quotesz_ or _zsingle_quotesz_i_zsingle_quotesz_

        s = pthread_attr_setinheritsched(&attr, inheritsched);
        if (s != 0)
            handle_error_en(s, pthread_attr_setinheritsched);

    if (attr_sched_str != NULL) {
        if (!get_policy(attr_sched_str[0], &policy))
                    Bad policy for _zsingle_quotesz_attr_zsingle_quotesz_ (−a)
        param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);

        s = pthread_attr_setschedpolicy(&attr, policy);
        if (s != 0)
            handle_error_en(s, pthread_attr_setschedpolicy);
        s = pthread_attr_setschedparam(&attr, &param);
        if (s != 0)
            handle_error_en(s, pthread_attr_setschedparam);

    /* If we initialized a thread attributes object, display
       the scheduling attributes that were set in the object */

    if (attrp != NULL) {
        s = pthread_attr_getschedparam(&attr, &param);
        if (s != 0)
            handle_error_en(s, pthread_attr_getschedparam);
        s = pthread_attr_getschedpolicy(&attr, &policy);
        if (s != 0)
            handle_error_en(s, pthread_attr_getschedpolicy);

        printf(Scheduler settings in _zsingle_quotesz_attr_zsingle_quotesz_
        display_sched_attr(policy, &param);

        s = pthread_attr_getinheritsched(&attr, &inheritsched);
        printf(    inheritsched is %s
                (inheritsched == PTHREAD_INHERIT_SCHED)  ? INHERIT :
                (inheritsched == PTHREAD_EXPLICIT_SCHED) ? EXPLICIT :

    /* Create a thread that will display its scheduling attributes */

    s = pthread_create(&thread, attrp, &thread_start, NULL);
    if (s != 0)
        handle_error_en(s, pthread_create);

    /* Destroy unneeded thread attributes object */

    if (!use_null_attrib) {
      s = pthread_attr_destroy(&attr);
      if (s != 0)
          handle_error_en(s, pthread_attr_destroy);

    s = pthread_join(thread, NULL);
    if (s != 0)
        handle_error_en(s, pthread_join);



getrlimit(2), sched_get_priority_min(2), pthread_attr_init(3), pthread_attr_setinheritsched(3), pthread_attr_setschedparam(3), pthread_attr_setschedpolicy(3), pthread_create(3), pthread_self(3), pthread_setschedprio(3), pthreads(7), sched(7)


This page is part of release 5.04 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at−pages/.

  Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the
entire resulting derived work is distributed under the terms of a
permission notice identical to this one.

Since the Linux kernel and libraries are constantly changing, this
manual page may be incorrect or out-of-date.  The author(s) assume no
responsibility for errors or omissions, or for damages resulting from
the use of the information contained herein.  The author(s) may not
have taken the same level of care in the production of this manual,
which is licensed free of charge, as they might when working

Formatted or processed versions of this manual, if unaccompanied by
the source, must acknowledge the copyright and authors of this work.