スレッドのプライオリティをnice()で実装したけど、結果はniceでない話。
サンプルコードとしては、優先度を最大まで下げた後に、デフォルトより少し上げて、元に戻すなんてことをしたいのだが...
(できない前提で書いてます)
実行結果
$ ./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
やっぱり、相対値は使い勝手が悪い。