- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2019-05-30T11:32:17+09:00","default:honma","honma")
#author("2019-12-26T09:02:54+09:00","default:honma","honma")
* nice値の設定 [#d8aa1fe1]
参考:[[NICE:http://linuxjm.osdn.jp/html/LDP_man-pages/man2/nice.2.html]], [[GETPRIORITY:http://linuxjm.osdn.jp/html/LDP_man-pages/man2/getpriority.2.html]]
getpriority()を使用したプライオリティの取得
#highlight(c){{
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>
errno = 0;
ret = getpriority(PRIO_PROCESS, 0);
if (errno == 0) {
printf("priority = %d\n", ret);
}
else {
perror("getpriority");
}
}}
void getprio(void)
{
int ret = 0;
nice()を使用したプライオリティの設定
errno = 0;
ret = getpriority(PRIO_PROCESS, 0);
if (errno == 0) {
printf("priority = %d\n", ret);
}
else {
perror("getpriority");
}
}
int main(int argc, char **argv)
{
int ret = 0;
getprio();
/* キー入力待ち */
getchar();
/* nice値の設定 */
#highlight(c){{
errno = 0;
ret = nice(-1);
if (errno == 0) {
printf("priority = %d\n", ret);
getprio();
}
else {
perror("nice");
}
}}
/* キー入力待ち */
getchar();
setpriority()を使用したプライオリティの設定
getprio();
return 0;
}
#highlight(c){{
ret = setpriority(PRIO_PROCESS, 0, 1);
if (ret == 0) {
getprio();
}
else {
perror("setpriority");
}
}}
#highlight(end)
[[ソースコード:http://www.chobits.com/pukiwiki/index.php?plugin=attach&pcmd=open&file=sched_get_prio.c&refer=sched_getattr]]
#ref(getprio.c)
実行結果
$ sudo ./getprio
[sudo] ****** のパスワード:
[sudo] ***** のパスワード:
priority = 0
priority = -1
priority = -1
priority = 1
別ターミナルから確認
$ cat /proc/`pidof getprio`/sched | grep -e policy -e prio
getprio (96338, #threads: 1)
getprio (105166, #threads: 1)
policy : 0
prio : 120
$ cat /proc/`pidof getprio`/stat | awk -F' ' '{print $18,$19}'
20 0
※ priority を -1 に設定後、確認
$ cat /proc/`pidof getprio`/sched | grep -e policy -e prio
getprio (96338, #threads: 1)
getprio (105166, #threads: 1)
policy : 0
prio : 119
$ cat /proc/`pidof getprio`/stat | awk -F' ' '{print $18,$19}'
19 -1
※ priority を 1 に設定後、確認
$ cat /proc/`pidof getprio`/sched | grep -e policy -e prio
getprio (105166, #threads: 1)
policy : 0
prio : 121
$ cat /proc/`pidof getprio`/stat | awk -F' ' '{print $18,$19}'
21 1
$
**設定値(CFSクラス)一覧 [#i81507d4]
|CENTER:|CENTER:|CENTER:|CENTER:|c
|優先度|/proc/<pid>/sched|>|/proc/<pid>stat|h
|~|prio|priority|nice|h
|高い|100|0|-20|
||119|19|-1|
|デフォルト|120|20|0|
||121|21|1|
|低い|139|39|19|
~
~
#htmlinsert(amazon_pc.html);