pthread_setschedparam(3)
okay bestie, this is the pthread_setschedparam(3) page from the Library Functions Manual π and it's literally all about telling threads how to schedule their little lives. group chat energy only β¨
πΈ NAME πΈ
pthread_setschedparam, pthread_getschedparam β set/get scheduling policy and parameters of a thread π§
π LIBRARY π
POSIX threads library (libpthread, -lpthread) π
β¨ SYNOPSIS β¨
#include <pthread.h>
int pthread_setschedparam(pthread_t thread, int policy,
const struct sched_param *param);
int pthread_getschedparam(pthread_t thread, int *restrict policy,
struct sched_param *restrict param);
π¦ DESCRIPTION π¦
pthread_setschedparam() does the thing where it sets the scheduling policy and parameters of the thread thread. main character energy for that thread, honestly. π
policy is the new scheduling policy you're giving to thread. the supported values for policy, plus their semantics, are described in sched(7). go read that if you need the full lore. π
the structure that param points to is where the new scheduling parameters live. scheduling parameters are kept in this structure, periodt:
struct sched_param {
int sched_priority; /* Scheduling priority */
};
so yeah, only one scheduling parameter is supported β that's sched_priority, the scheduling priority. for the details of the permitted ranges for scheduling priorities in each scheduling policy, see sched(7). no cap, that's where the numbers are. π’
pthread_getschedparam() returns the scheduling policy and parameters of the thread thread, dropping them into the buffers pointed to by policy and param, respectively. the returned priority value is the one 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 that happened because of calls to any priority inheritance or priority ceiling functions β see, for example, pthread_mutexattr_setprioceiling(3) and pthread_mutexattr_setprotocol(3). that's a whole inheritance/ceiling situation, iykyk.
π«Ά RETURN VALUE π«Ά
on success, these functions return 0; on error, they return a nonzero error number. and if pthread_setschedparam() fails, the scheduling policy and parameters of thread are not changed. so no sneaky half-updates. π«
π ERRORS π
both of these functions can fail with this error:
ESRCHβ no thread with the IDthreadcould be found. like, she's not here. π
pthread_setschedparam() may additionally fail with these errors:
EINVALβpolicyis not a recognized policy, orparamdoes not make sense for the policy. invalid vibes. πEPERMβ the caller does not have appropriate privileges to set the specified scheduling policy and parameters. permission slip denied, bestie. π
POSIX.1 also documents an ENOTSUP ("attempt was made to set the policy or scheduling parameters to an unsupported value") error for pthread_setschedparam().
π ATTRIBUTES π
for an explanation of the terms used in this section, see attributes(7).
pthread_setschedparam(),pthread_getschedparam()β Thread safety:MT-Safeβ
π STANDARDS π
POSIX.1-2008. π
π HISTORY π
glibc 2.0 POSIX.1-2001. π
π§ 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). permissions, effects, ranges β it's all in there.
π¬ EXAMPLES π
Okay bestie, this demo program is literally the moment. It shows off pthread_setschedparam() and pthread_getschedparam() in action, plus a whole lil squad of other scheduling-related pthreads functions. It's giving "here's how the whole vibe works". π¦
In this first 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. Then the program 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. Then it creates a thread using the thread attributes object, and that thread shows off 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
policy=SCHED_FIFO, priority=10
Scheduler settings in 'attr'
policy=SCHED_RR, priority=20
inheritsched is EXPLICIT
Scheduler attributes of new thread
policy=SCHED_RR, priority=20
Looking at that output, you can see the scheduling policy and priority were taken from the values specified in the thread attributes object. Iconic. π
The next run is the same as the previous one, except 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. She said "not me inheriting everything from my maker". π
# ./a.out -mf10 -ar20 -i i;
Scheduler settings of main thread
policy=SCHED_FIFO, priority=10
Scheduler settings in 'attr'
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. Fr fr. π₯
Ngl bestie, if we had omitted the-i ioption, the output would have been the same, sincePTHREAD_INHERIT_SCHEDis the default for the inherit scheduler attribute. It's the default for a reason. π
π Program source π
/* pthreads_sched_test.c */
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
[[noreturn]]
static void
usage(char *prog_name, char *msg)
{
if (msg != NULL)
fputs(msg, stderr);
fprintf(stderr, "Usage: %s [options]\n", prog_name);
fprintf(stderr, "Options are:\n");
#define fpe(msg) fprintf(stderr, "\t%s", msg) /* Shorter */
fpe("-a<policy><prio> Set scheduling policy and priority in\n");
fpe(" thread attributes object\n");
fpe(" <policy> can be\n");
fpe(" f SCHED_FIFO\n");
fpe(" r SCHED_RR\n");
fpe(" o SCHED_OTHER\n");
fpe("-A Use default thread attributes object\n");
fpe("-i {e|i} Set inherit scheduler attribute to\n");
fpe(" 'explicit' or 'inherit'\n");
fpe("-m<policy><prio> Set scheduling policy and priority on\n");
fpe(" main thread before pthread_create() call\n");
exit(EXIT_FAILURE);
}
static int
get_policy(char p, int *policy)
{
switch (p) {
case 'f': *policy = SCHED_FIFO; return 1;
case 'r': *policy = SCHED_RR; return 1;
case 'o': *policy = SCHED_OTHER; return 1;
default: return 0;
}
}
static void
display_sched_attr(int policy, const struct sched_param *param)
{
printf(" policy=%s, priority=%d\n",
(policy == SCHED_FIFO) ? "SCHED_FIFO" :
(policy == SCHED_RR) ? "SCHED_RR" :
(policy == SCHED_OTHER) ? "SCHED_OTHER" :
"???",
param->sched_priority);
}
static void
display_thread_sched_attr(char *msg)
{
int policy, s;
struct sched_param param;
s = pthread_getschedparam(pthread_self(), &policy, ¶m);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_getschedparam");
printf("%s\n", msg);
display_sched_attr(policy, ¶m);
}
static void *
thread_start(void *arg)
{
display_thread_sched_attr("Scheduler attributes of new thread");
return NULL;
}
int
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 'a': attr_sched_str = optarg; break;
case 'A': use_null_attrib = 1; break;
case 'i': inheritsched_str = optarg; break;
case 'm': main_sched_str = optarg; break;
default: usage(argv[0], "Unrecognized option\n");
}
}
if (use_null_attrib
&& (inheritsched_str != NULL || attr_sched_str != NULL))
{
usage(argv[0], "Can't specify -A with -i or -a\n");
}
/* 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)\n");
param.sched_priority = strtol(&main_sched_str[1], NULL, 0);
s = pthread_setschedparam(pthread_self(), policy, ¶m);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_setschedparam");
}
display_thread_sched_attr("Scheduler settings of main thread");
printf("\n");
/* Initialize thread attributes object according to options. */
attrp = NULL;
if (!use_null_attrib) {
s = pthread_attr_init(&attr);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_init");
attrp = &attr;
}
if (inheritsched_str != NULL) {
if (inheritsched_str[0] == 'e')
inheritsched = PTHREAD_EXPLICIT_SCHED;
else if (inheritsched_str[0] == 'i')
inheritsched = PTHREAD_INHERIT_SCHED;
else
usage(argv[0], "Value for -i must be 'e' or 'i'\n");
s = pthread_attr_setinheritsched(&attr, inheritsched);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched");
}
if (attr_sched_str != NULL) {
if (!get_policy(attr_sched_str[0], &policy))
usage(argv[0], "Bad policy for 'attr' (-a)\n");
param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);
s = pthread_attr_setschedpolicy(&attr, policy);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_setschedpolicy");
s = pthread_attr_setschedparam(&attr, ¶m);
if (s != 0)
errc(EXIT_FAILURE, 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, ¶m);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getschedparam");
s = pthread_attr_getschedpolicy(&attr, &policy);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy");
printf("Scheduler settings in 'attr'\n");
display_sched_attr(policy, ¶m);
pthread_attr_getinheritsched(&attr, &inheritsched);
printf(" inheritsched is %s\n",
(inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
(inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
"???");
printf("\n");
}
/* Create a thread that will display its scheduling attributes. */
s = pthread_create(&thread, attrp, &thread_start, NULL);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_create");
/* Destroy unneeded thread attributes object. */
if (!use_null_attrib) {
s = pthread_attr_destroy(&attr);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_destroy");
}
s = pthread_join(thread, NULL);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_join");
exit(EXIT_SUCCESS);
}
π SEE ALSO π«Ά
Go read these next bestie, they're the whole friend group:
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)
And that's a wrap on pthread_setschedparam(3) for real for real!! πβ¨ This lil page came from Linux man-pages 6.20, dated 2025-09-21, and it's genuinely so iconic of whoever maintains it. Go forth and schedule some threads, bestie β you ate. π
πΈπ
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms