pthread_setschedparam(3) - wariua/manpages-ko GitHub Wiki
pthread_setschedparam, pthread_getschedparam - μ€λ λμ μ€μΌμ€λ§ μ μ± λ° λ§€κ°λ³μ μ€μ νκΈ°/μ»κΈ°
#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);
-pthread
λ‘ μ»΄νμΌ λ° λ§ν¬.
pthread_setschedparam()
ν¨μλ μ€λ λ thread
μ μ€μΌμ€λ§ μ μ±
κ³Ό λ§€κ°λ³μλ₯Ό μ€μ νλ€.
policy
κ° thread
λ₯Ό μν μ μ€μΌμ€λ§ μ μ±
μ λνλΈλ€. policy
μ μ§μ κ°λ€κ³Ό κ·Έ μλ―Έλ sched(7)μμ κΈ°μ νλ€.
param
μ΄ κ°λ¦¬ν€λ κ΅¬μ‘°μ²΄κ° thread
λ₯Ό μν μ μ€μΌμ€λ§ λ§€κ°λ³μλ₯Ό λνλΈλ€. λ€μ ꡬ쑰체μ μ€μΌμ€λ§ λ§€κ°λ³μλ₯Ό λ΄λλ€.
struct sched_param {
int sched_priority; /* μ€μΌμ€λ§ μ°μ μμ */
};
보λ€μνΌ ν κ°μ§ μ€μΌμ€λ§ λ§€κ°λ³μλ§ μ§μνλ€. κ° μ€μΌμ€λ§ μ μ± μμ νμ©νλ μ€μΌμ€λ§ μ°μ μμ λ²μμ λν μΈλΆ λ΄μ©μ sched(7)λ₯Ό 보λΌ.
pthread_getschedparam()
ν¨μλ μ€λ λ thread
μ μ€μΌμ€λ§ μ μ±
κ³Ό λ§€κ°λ³μλ₯Ό κ°κ° policy
λ° param
μ΄ κ°λ¦¬ν€λ λ²νΌλ‘ λ°ννλ€. λ°νλλ μ°μ μμ κ°μ thread
μ μν₯μ μ€ κ°μ₯ μ΅κ·Όμ pthread_setschedparam()
μ΄λ pthread_setschedprio(3), pthread_create(3) νΈμΆμμ μ€μ ν κ°μ΄λ€. λ°νλλ μ°μ μμλ μ°μ μμ μμ λ΄μ§ μ°μ μμ μν μ§μ ν¨μ νΈμΆλ‘ μΈν μΌμμ μ°μ μμ μ‘°μ μ λ°μνμ§ μλλ€. (μλ‘ pthread_mutexattr_setprioceiling(3) λ° pthread_mutexattr_setprotocol(3) μ°Έκ³ .)
μ±κ³΅ μ μ΄ ν¨μλ€μ 0μ λ°ννλ€. μ€λ₯ μ 0 μλ μ€λ₯ λ²νΈλ₯Ό λ°ννλ€. pthread_setschedparam()
κ° μ€ν¨ν κ²½μ° thread
μ μ€μΌμ€λ§ μ μ±
λ° λ§€κ°λ³μκ° λ°λμ§ μλλ€.
λ ν¨μ λͺ¨λ λ€μ μ€λ₯λ‘ μ€ν¨ν μ μλ€.
ESRCH
- IDκ°
thread
μΈ μ€λ λλ₯Ό μ°Ύμ μ μλ€.
pthread_setschedparam()
μ΄ μΆκ°λ‘ λ€μ μ€λ₯λ‘ μ€ν¨ν μλ μλ€.
EINVAL
-
policy
κ° μ μ μλ μ μ± μ΄κ±°λ,policy
μμparam
μ΄ λ§μ΄ λμ§ μλλ€. EPERM
- νΈμΆμκ° μ§μ ν μ€μΌμ€λ§ μ μ± λ° λ§€κ°λ³μλ₯Ό μ€μ νκΈ° μν μ μ ν νΉκΆμ κ°μ§κ³ μμ§ μλ€.
POSIX.1μμλ pthread_setschedparam()
μμ ENOTSUP
μ€λ₯("μ€μΌμ€λ§ μ μ±
λ° λ§€κ°λ³μλ₯Ό μ§μνμ§ μλ κ°μΌλ‘ μ€μ νλ €κ³ μλνμ")λ μ κ³ μλ€.
μ΄ μ μμ μ¬μ©νλ μ©μ΄λ€μ λν μ€λͺ μ attributes(7)λ₯Ό 보λΌ.
μΈν°νμ΄μ€ | μμ± | κ° |
---|---|---|
pthread_setschedparam() ,pthread_getschedparam()
|
μ€λ λ μμ μ± | MT-Safe |
POSIX.1-2001, POSIX.1-2008.
μ€λ λμ μ€μΌμ€λ§ μ°μ μμλ₯Ό λ°κΎΈλ λ° νμν κΆνκ³Ό κ·Έ ν¨κ³Ό, κ° μ€μΌμ€λ§ μ μ± μμ νμ©νλ μ°μ μμ λ²μμ λν μ€λͺ μ sched(7)λ₯Ό 보λΌ.
μλ νλ‘κ·Έλ¨μ pthread_setschedparam()
κ³Ό pthread_getschedparam()
μ μ¬μ© λ°©μλΏ μλλΌ μ¬λ¬ λ€λ₯Έ μ€μΌμ€λ§ κ΄λ ¨ pthreads ν¨μλ€μ μ¬μ© λ°©μλ λ³΄μ¬ μ€λ€.
μλ μ€νμμ μ£Ό μ€λ λλ μκΈ° μ€μΌμ€λ§ μ μ±
μ μ°μ μμ 10μΈ SCHED_FIFO
λ‘ μ€μ νκ³ , μ€λ λ μμ± κ°μ²΄λ₯Ό μ€μΌμ€λ§ μ μ±
μμ± SCHED_RR
κ³Ό μ€μΌμ€λ§ μ°μ μμ μμ± 20μΌλ‘ μ΄κΈ°ν νλ€. κ·Έλ¬κ³ μ νλ‘κ·Έλ¨μ (pthread_attr_setinheritsched(3)μ μ΄μ©ν΄) μ€λ λ μμ± κ°μ²΄μ μ€μΌμ€λ¬ μμ μμ±μ PTHREAD_EXPLICIT_SCHED
λ‘ μ€μ νλλ°, μ΄λ μ΄ μμ± κ°μ²΄λ₯Ό μ¬μ©ν΄ μμ±ν μ€λ λλ€μ΄ μ€λ λ μμ± κ°μ²΄λ‘λΆν° μκΈ° μ€μΌμ€λ§ μμ±λ€μ κ°μ ΈμμΌ νλ€λ μλ―Έμ΄λ€. κ·Έλ¬κ³ μ νλ‘κ·Έλ¨μ΄ κ·Έ μ€λ λ μμ± κ°μ²΄λ₯Ό μ΄μ©ν΄ μ€λ λλ₯Ό λ§λ€κ³ , κ·Έ μ€λ λκ° μκΈ° μ€μΌμ€λ§ μ μ±
κ³Ό μ°μ μμλ₯Ό νμνλ€.
$ su # μ€μκ° μ€μΌμ€λ§ μ μ±
μ€μ μ μν΄ νΉκΆ νμ
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
μ μΆλ ₯μ 보면 μ€λ λ μμ± κ°μ²΄μ μ§μ λ κ°λ€μμ μ€μΌμ€λ§ μ μ± κ³Ό μ°μ μμλ₯Ό κ°μ Έμμμ μ μ μλ€.
λ€μ μ€νμ μμμ κ°λ μ€μΌμ€λ¬ μμ μμ±μ PTHREAD_INHERIT_SCHED
λ‘ μ€μ νλλ°, μ΄λ μ€λ λ μμ± κ°μ²΄λ₯Ό μ¬μ©ν΄ μμ±ν μ€λ λλ€μ΄ κ·Έ μ€λ λ κ°μ²΄μ μ§μ λ μ€μΌμ€λ§ μμ±λ€μ 무μνκ³ λμ μμ±μ νλ μ€λ λμκ²μ μ€μΌμ€λ§ μμ±λ€μ κ°μ ΈμμΌ νλ€λ μλ―Έμ΄λ€.
# ./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
μ μΆλ ₯μ 보면 μ€λ λ μμ± κ°μ²΄κ° μλλΌ μμ±μ νλ μ€λ λμκ²μ μ€μΌμ€λ§ μ μ± κ³Ό μ°μ μμλ₯Ό κ°μ Έμμμ μ μ μλ€.
μ°Έκ³ λ‘ -i i
μ΅μ
μ μλ΅νμ΄λ μΆλ ₯μ΄ λμΌνμ κ²μ΄λ€. μ€μΌμ€λ¬ μμ μμ±μ PTHREAD_INHERIT_SCHED
κ° κΈ°λ³Έκ°μ΄κΈ° λλ¬Έμ΄λ€.
/* 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]\n", prog_name);
fprintf(stderr, "Options are:\n");
#define fpe(msg) fprintf(stderr, "\t%s", msg); /* μ§§κ² */
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, 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;
/* λͺ
λ Ήν μ΅μ
μ²λ¦¬ */
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");
/* μ νμ μΌλ‘ μ£Ό μ€λ λμ μ€μΌμ€λ§ μμ±μ μ€μ νκ³
μμ±μ νμ */
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");
/* μ΅μ
μ λ°λΌ μ€λ λ μμ± κ°μ²΄ μ΄κΈ°ν */
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 (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);
s = pthread_attr_getinheritsched(&attr, &inheritsched);
printf(" inheritsched is %s\n",
(inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
(inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
"???");
printf("\n");
}
/* μκΈ° μ€μΌμ€λ§ μμ±μ νμνλ μ€λ λ μμ± */
s = pthread_create(&thread, attrp, &thread_start, NULL);
if (s != 0)
handle_error_en(s, "pthread_create");
/* νμ μλ μμ± κ°μ²΄ νκΈ° */
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);
}
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)
2019-03-06