スレッドのプライオリティ(nice版)

スレッドのプライオリティをnice()で実装したけど、結果はniceでない話。

サンプルコードとしては、優先度を最大まで下げた後に、デフォルトより少し上げて、元に戻すなんてことをしたいのだが...
(できない前提で書いてます)

参考:Man page of 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

やっぱり、相対値は使い勝手が悪い。
スレッドのプライオリティを操作するなら、やっぱりこちら


添付ファイル: filethread_nice.tgz 181件 [詳細]

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-05-29 (月) 14:05:52