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

前回のnice版より、使い勝手の良いsetpriority()を使用してみた。
glibc 2.2.4 以降では、 nice() は getpriority() を呼び出すライブラリ関数として実装されている。情報元はこちら
懸念点があるとすれば、本来はプロセス単位で制御する点で、今後の互換性や移植性に難あり。

参考:Man page of GETPRIORITY

実行結果

$ ./thread_setpriority
my pid = 74433
thread_test: pid = 74433 tid= 74434
thread_setprior (74433, #threads: 2)
policy                                       :                    0
prio                                         :                  120
thread_setprior (74434, #threads: 2)
policy                                       :                    0
prio                                         :                  120
policy = 0, sched_priority = 0
change nice = 19
thread_setprior (74433, #threads: 2)
policy                                       :                    0
prio                                         :                  120
thread_setprior (74434, #threads: 2)
policy                                       :                    0
prio                                         :                  139

change nice = -1
setpriority: Permission denied
thread_setprior (74433, #threads: 2)
policy                                       :                    0
prio                                         :                  120
thread_setprior (74434, #threads: 2)
policy                                       :                    0
prio                                         :                  139

change nice = 0
setpriority: Permission denied
thread_setprior (74433, #threads: 2)
policy                                       :                    0
prio                                         :                  120
thread_setprior (74434, #threads: 2)
policy                                       :                    0
prio                                         :                  139

前回のnice版同様にプライオリティを上げたいときはroot特権が必要。(Operation not permitted になる)
nice()と異なりデフォルトに対する相対値なので、プライオリティに0を指定してもエラーになるケースが出る。

$ sudo ./thread_setpriority
my pid = 74476
thread_test: pid = 74476 tid= 74477
thread_setprior (74476, #threads: 2)
policy                                       :                    0
prio                                         :                  120
thread_setprior (74477, #threads: 2)
policy                                       :                    0
prio                                         :                  120
policy = 0, sched_priority = 0
change nice = 19
thread_setprior (74476, #threads: 2)
policy                                       :                    0
prio                                         :                  120
thread_setprior (74477, #threads: 2)
policy                                       :                    0
prio                                         :                  139

change nice = -1
thread_setprior (74476, #threads: 2)
policy                                       :                    0
prio                                         :                  120
thread_setprior (74477, #threads: 2)
policy                                       :                    0
prio                                         :                  119

change nice = 0
thread_setprior (74476, #threads: 2)
policy                                       :                    0
prio                                         :                  120
thread_setprior (74477, #threads: 2)
policy                                       :                    0
prio                                         :                  120

nice版と異なり、デフォルトに対する相対値で指定できるだけ使い勝手は良い。


添付ファイル: filethread_setpriority.tgz 319件 [詳細]

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