- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2020-02-07T09:55:47+09:00","default:honma","honma")
* スレッドのプライオリティ(nice版) [#r388c51e]
スレッドのプライオリティをnice()で実装したけど、結果はniceでない話。~
~
サンプルコードとしては、優先度を最大まで下げた後に、デフォルトより少し上げて、元に戻すなんてことをしたいのだが...~
(できない前提で書いてます)~
参考:[[Man page of NICE:http://linuxjm.osdn.jp/html/LDP_man-pages/man2/nice.2.html]]
#highlight(c){{
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <errno.h>
typedef struct {
pthread_t thread;
} thread_param_t;
/* スレッドID を取得する */
pid_t gettid(void)
{
/*
* glibc のラッパー関数が存在しないので間接システムコールを用いる
*/
pid_t tid;
tid = syscall(SYS_gettid);
return tid;
}
/* スレッド */
void *thread_test(void *args)
{
int ret;
int tid;
int policy;
int prio[3] = {19, -1, 0};
struct sched_param schedParam;
thread_param_t *thread_param = args;
char sched_msg[] = "cat /proc/%d/sched | grep -A 1 policy";
char msg[80] = {0};
int i;
tid = (int)gettid();
printf("thread_test: pid = %d tid= %d\n", getpid(), tid);
sprintf(msg, sched_msg, tid);
system(msg);
/* スレッドのスケージューリングパラメータを取得する */
ret = pthread_getschedparam(thread_param->thread, &policy, &schedParam);
if (ret != 0) {
fprintf(stderr, "pthread_getschedparam = %d\n", ret);
}
printf("policy = %d, sched_priority = %d\n", policy, schedParam.sched_priority);
for (i=0; i<3; i++) {
printf("change nice = %d\n", prio[i]);
/* スレッドプライオリティの変更 */
errno = 0;
ret = nice(prio[i]);
if (errno != 0) {
perror("nice");
}
system(msg);
/* キー入力待ち */
getchar();
}
/* スレッドの終了 */
pthread_exit(NULL);
}
/* メイン */
int main(void)
{
int ret;
pthread_attr_t attr;
struct sched_param schedParam;
thread_param_t thread_param;
printf("my pid = %d\n", getpid());
#if 0
/* スレッド属性オブジェクトの初期化と破棄を行う */
ret = pthread_attr_init(&attr);
if (ret != 0) {
fprintf(stderr, "pthread_attr_init = %d\n", ret);
}
/* inherit-scheduler 属性の設定/取得を行う */
ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
if (ret != 0) {
fprintf(stderr, "pthread_attr_setinheritsched = %d\n", ret);
}
/* スケジューリングポリシー属性の設定を行う */
ret = pthread_attr_setschedpolicy(&attr, SCHED_RR);
if (ret != 0) {
fprintf(stderr, "pthread_attr_setschedpolicy = %d\n", ret);
}
/* スケジューリングパラメーター属性の設定を行う */
schedParam.sched_priority = 10;
ret = pthread_attr_setschedparam(&attr, &schedParam);
if (ret != 0) {
fprintf(stderr, "pthread_attr_setschedparam = %d\n", ret);
}
#endif
/* 新しいスレッドを作成する */
// ret = pthread_create(&thread_param.thread, &attr, thread_test, &thread_param);
ret = pthread_create(&thread_param.thread, NULL, thread_test, &thread_param);
if (ret != 0) {
fprintf(stderr, "pthread_create = %d\n", ret);
}
/* 終了したスレッドを join する */
ret = pthread_join(thread_param.thread, NULL);
if (ret != 0) {
fprintf(stderr, "pthread_join = %d\n", ret);
}
return 0;
}
}}
#highlight(end)
#ref(thread_nice.tgz)
実行結果
$ ./thread_nice
my pid = 72837
thread_test: pid = 72837 tid= 72838
thread_nice (72837, #threads: 2)
policy : 0
prio : 120
thread_nice (72838, #threads: 2)
policy : 0
prio : 120
policy = 0, sched_priority = 0
change nice = 19
thread_nice (72837, #threads: 2)
policy : 0
prio : 120
thread_nice (72838, #threads: 2)
policy : 0
prio : 139
change nice = -1
nice: Operation not permitted
thread_nice (72837, #threads: 2)
policy : 0
prio : 120
thread_nice (72838, #threads: 2)
policy : 0
prio : 139
change nice = 0
thread_nice (72837, #threads: 2)
policy : 0
prio : 120
thread_nice (72838, #threads: 2)
policy : 0
prio : 139
nice値を下げたいときはroot特権が必要。(Operation not permitted になる)~
nice値は相対値なので、自分で意識しないと目的が達成できない。~
$ sudo ./thread_nice
my pid = 72881
thread_test: pid = 72881 tid= 72882
thread_nice (72881, #threads: 2)
policy : 0
prio : 120
thread_nice (72882, #threads: 2)
policy : 0
prio : 120
policy = 0, sched_priority = 0
change nice = 19
thread_nice (72881, #threads: 2)
policy : 0
prio : 120
thread_nice (72882, #threads: 2)
policy : 0
prio : 139
change nice = -1
thread_nice (72881, #threads: 2)
policy : 0
prio : 120
thread_nice (72882, #threads: 2)
policy : 0
prio : 138
change nice = 0
thread_nice (72881, #threads: 2)
policy : 0
prio : 120
thread_nice (72882, #threads: 2)
policy : 0
prio : 138
やっぱり、相対値は使い勝手が悪い。
#htmlinsert(amazon_pc.html);