pthread_setschedparam
Table des matières
Retour à l'index
NOM
pthread_setschedparam, pthread_getschedparam — Définir ou obtenir la
politique et les paramètres d'ordonnancement d'un thread
BIBLIOTHÈQUE
Bibliothèque de threads POSIX (libpthread, -lpthread)
SYNOPSIS
#include <pthread.h>
int pthread_setschedparam(pthread_t thread, int politique,
const struct sched_param *param);
int pthread_getschedparam(pthread_t thread, int *restrict politique,
struct sched_param *restrict param);
DESCRIPTION
La fonction pthread_setschedparam() définit la politique et les
paramètres d'ordonnancement du thread thread.
L'argument politique spécifie la politique d'ordonnancement pour
thread. Les valeurs possibles, ainsi que leur signification, sont
décrites dans sched(7).
La structure vers laquelle pointe param spécifie les nouveaux paramètres
d'ordonnancement pour thread. Les paramètres d'ordonnancement sont gérés
dans la structure suivante :
struct sched_param {
int sched_priority; /* Priorité d'ordonnancement */
};
Comme on peut le voir, un seul paramètre d'ordonnancement est pris en
charge. Veuillez consulter sched(7) pour plus de détails sur les valeurs
possibles des priorités d'ordonnancement en fonction de chaque politique
d'ordonnancement.
La fonction pthread_getschedparam() renvoie, dans les tampons pointés par
politique et param, respectivement la politique et les paramètres
d'ordonnancement du thread thread. La valeur de priorité renvoyée est
celle utilisée lors de l'appel le plus récent à pthread_setschedparam(),
pthread_setschedprio(3) ou pthread_create(3) qui touchait
thread. La priorité renvoyée ne reflète pas des ajustements de priorité
temporaires qui peuvent résulter d'appels à une fonction qui modifie
l'héritage ou plafonne la priorité d'ordonnancement (voir, par exemple,
pthread_mutexattr_setprioceiling(3) et
pthread_mutexattr_setprotocol(3)).
VALEUR RENVOYÉE
En cas de réussite, ces fonctions renvoient 0 ; en cas d'erreur, elles
renvoient un numéro d'erreur non nul. Si pthread_setschedparam() échoue,
la politique et les paramètres d'ordonnancement du thread ne sont pas
modifiés.
ERREURS
Ces deux fonctions peuvent échouer avec l'erreur suivante :
- ESRCH
-
Aucun fil d’exécution avec pour identifiant thread n'a pu être trouvé.
pthread_setschedparam() peut également échouer avec les erreurs
suivantes :
- EINVAL
-
politique n'est pas une politique reconnue, ou param n'a pas de sens
pour cette valeur de politique.
- EPERM
-
L'appelant n'a pas les privilèges suffisants pour définir la politique et
les paramètres d'ordonnancement indiqués.
POSIX.1 documente également une erreur ENOTSUP (tentative de définition
de la politique ou de paramètres d'ordonnancement à une valeur non prise en
charge) pour pthread_setschedparam().
ATTRIBUTS
Pour une explication des termes utilisés dans cette section, consulter
attributes(7).
| Interface | Attribut | Valeur
|
|
pthread_setschedparam(),
pthread_getschedparam()
| Sécurité des threads | MT-Safe
|
STANDARDS
POSIX.1-2008.
HISTORIQUE
glibc 2.0 POSIX.1-2001.
NOTES
Pour une description des permissions nécessaires pour modifier la politique
et la priorité d'ordonnancement d'un thread (et leur effet), et les détails
sur les valeurs possibles de priorités pour chacune des politiques
d'ordonnancement, consultez sched(7).
EXEMPLES
Le programme ci-dessous montre l'utilisation de pthread_setschedparam()
et pthread_getschedparam(), ainsi que d'un certain nombre d'autres
fonctions de pthreads relatives à l'ordonnancement.
Lors de l'exécution suivante, le thread principal définit sa politique
d'ordonnancement à SCHED_FIFO avec une priorité de 10 et initialise un
objet d'attributs de thread avec un attribut de politique d'ordonnancement
mis à SCHED_RR et un attribut de priorité d'ordonnancement de 20. Le
programme appelle alors pthread_attr_setinheritsched(3) pour définir à
PTHREAD_EXPLICIT_SCHED l'attribut d'héritage de l'ordonnancement de
l'objets d'attributs de thread, ce qui signifie que les threads créés avec
cet objet d'attributs prendront les attributs d'ordonnancement dans l'objet
d'attributs de thread. Le programme crée alors un thread en utilisant
l'objets d'attributs de thread et ce thread affiche sa politique et sa
priorité d'ordonnancement.
$ 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
Nous pouvons voir dans l'affichage ci-dessus que les politique et priorité
d'ordonnancement ont pris comme valeurs celles de l'objets d'attributs de
thread.
L'exécution suivante est identique, sauf que l'attribut d'héritage
d'ordonnancement est mis à PTHREAD_INHERIT_SCHED, ce qui signifie que les
threads créés avec cet objet d'attributs devront ignorer les attributs
d'ordonnancement de l'objets d'attributs de thread et prendre à la place les
valeurs héritées du thread appelant.
# ./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
Nous pouvons voir dans l'affichage ci-dessus que les politique et priorité
d'ordonnancement ont pris comme valeurs celles du thread appelant et non de
l'objets d'attributs de thread.
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.
Source du programme
/* pthreads_sched_test.c */
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
[[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)
handle_error_en(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)
handle_error_en(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)
handle_error_en(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)
handle_error_en(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)
handle_error_en(s, "pthread_attr_setschedpolicy");
s = pthread_attr_setschedparam(&attr, ¶m);
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, ¶m);
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 '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)
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");
exit(EXIT_SUCCESS);
}
VOIR AUSSI
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)
TRADUCTION
La traduction française de cette page de manuel a été créée par
Christophe Blaess <https://www.blaess.fr/christophe/>,
Stéphan Rafin <stephan.rafin@laposte.net>,
Thierry Vignaud <tvignaud@mandriva.com>,
François Micaux,
Alain Portal <aportal@univ-montp2.fr>,
Jean-Philippe Guérard <fevrier@tigreraye.org>,
Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>,
Julien Cristau <jcristau@debian.org>,
Thomas Huriaux <thomas.huriaux@gmail.com>,
Nicolas François <nicolas.francois@centraliens.net>,
Florentin Duneau <fduneau@gmail.com>,
Simon Paillard <simon.paillard@resel.enst-bretagne.fr>,
Denis Barbier <barbier@debian.org>,
David Prévot <david@tilapin.org>,
Frédéric Hantrais <fhantrais@gmail.com>
et
Jean-Pierre Giraud <jean-pierregiraud@neuf.fr>
Cette traduction est une documentation libre ; veuillez vous reporter à la
GNU General Public License version 3
concernant les conditions de copie et
de distribution. Il n'y a aucune RESPONSABILITÉ LÉGALE.
Si vous découvrez un bogue dans la traduction de cette page de manuel,
veuillez envoyer un message à
Index
- NOM
-
- BIBLIOTHÈQUE
-
- SYNOPSIS
-
- DESCRIPTION
-
- VALEUR RENVOYÉE
-
- ERREURS
-
- ATTRIBUTS
-
- STANDARDS
-
- HISTORIQUE
-
- NOTES
-
- EXEMPLES
-
- Source du programme
-
- VOIR AUSSI
-
- TRADUCTION
-
This document was created by
man2html,
using the manual pages.
Time: 05:06:22 GMT, September 19, 2025