目次: C言語とlibc
シグナルマスク(sigprocmaskのマニュアル)の「規定されていない」使い方をするとどうなるか?の続きです。5つのマルチスレッド(親スレッド+4つの子スレッド)で全員でsigwait()するのは共通で、誰がsigprocmask()を呼ぶか?を変えながら、下記4パターンを試します。
今回は結果3と4を紹介します。
全スレッドがsigprocmask()した場合です。ゆっくり5回シグナルを送ると、親スレッド(th 4)のsigwait()がシグナルを受け取り、子スレッド(th 0)のsigwait()はEINTRが返ります。
$ g++ -Wall -g -O2 -DUSE_SIGPROCMASK_ALL -DID_MAINTHREAD=1 signal_thread.cpp && ./a.out Use sigprocmask th 0: sub (child ) thread start th 0: sigprocmask(block) th 1: main (child ) thread start th 1: sigprocmask(block) th 2: sub (child ) thread start th 2: sigprocmask(block) th 4: sub (parent) thread start th 4: sigprocmask(block) th 3: sub (child ) thread start th 3: sigprocmask(block) th 3: loop start th 0: loop start th 2: loop start th 1: loop start th 4: loop start th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call)
良い感じです。また結果1(親スレッドがsigprocmask())と異なり、大量にシグナルを送りつけてもabortしないのも良いです。
$ g++ -Wall -g -O2 -DUSE_SIGPROCMASK_ALL -DID_MAINTHREAD=1 signal_thread.cpp && ./a.out Use sigprocmask th 0: sub (child ) thread start th 0: sigprocmask(block) th 1: main (child ) thread start th 1: sigprocmask(block) th 2: sub (child ) thread start th 2: sigprocmask(block) th 4: sub (parent) thread start th 4: sigprocmask(block) th 3: sub (child ) thread start th 3: sigprocmask(block) th 3: loop start th 0: loop start th 2: loop start th 1: loop start th 4: loop start (...略...) th 2: sigwait failed (Interrupted system call) th 0: sigwait failed (Interrupted system call) th 0: got SIGUSR1 th 1: got SIGUSR1 th 2: got SIGUSR1 th 2: got SIGUSR1 th 1: sigwait failed (Interrupted system call) th 0: got SIGUSR1 th 4: sigwait failed (Interrupted system call) th 3: sigwait failed (Interrupted system call) th 2: sigwait failed (Interrupted system call) th 1: got SIGUSR1 (別ターミナルから) $ while :; do kill -USR1 123450; if [ $? -ne 0 ]; then break; fi; done
良さそうですね。
正しい方法(全スレッドがpthread_sigmask())はどんな動きでしょうか?結果だけ先に書いてしまうと、全スレッドがsigprocmask()したときと同じ動きをするようです。
$ g++ -Wall -g -O2 -DUSE_PTHREADSIGMASK -DID_MAINTHREAD=1 signal_thread.cpp && ./a.out Use pthread_sigmask th 0: sub (child ) thread start th 0: pthread_sigmask(block) th 1: main (child ) thread start th 1: pthread_sigmask(block) th 2: sub (child ) thread start th 2: pthread_sigmask(block) th 4: sub (parent) thread start th 4: pthread_sigmask(block) th 3: sub (child ) thread start th 3: pthread_sigmask(block) th 3: loop start th 2: loop start th 0: loop start th 1: loop start th 4: loop start th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call)
結果3(全員sigprocmask())と同じ動きをしています。当然ながら、大量にシグナルを送りつけてもabortしません。
$ g++ -Wall -g -O2 -DUSE_PTHREADSIGMASK -DID_MAINTHREAD=1 signal_thread.cpp && ./a.out Use pthread_sigmask th 0: sub (child ) thread start th 0: pthread_sigmask(block) th 1: main (child ) thread start th 1: pthread_sigmask(block) th 2: sub (child ) thread start th 2: pthread_sigmask(block) th 4: sub (parent) thread start th 4: pthread_sigmask(block) th 3: sub (child ) thread start th 3: pthread_sigmask(block) th 3: loop start th 2: loop start th 0: loop start th 1: loop start th 4: loop start (...略...) th 2: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 1: sigwait failed (Interrupted system call) th 4: sigwait failed (Interrupted system call) th 0: got SIGUSR1 th 4: got SIGUSR1 th 0: got SIGUSR1 th 3: sigwait failed (Interrupted system call) th 3: got SIGUSR1 th 2: sigwait failed (Interrupted system call) th 2: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 0: got SIGUSR1 th 1: sigwait failed (Interrupted system call) th 3: sigwait failed (Interrupted system call) th 3: got SIGUSR1 th 0: got SIGUSR1 th 4: sigwait failed (Interrupted system call) th 2: sigwait failed (Interrupted system call) th 1: sigwait failed (Interrupted system call) (別ターミナルから) $ while :; do kill -USR1 123450; if [ $? -ne 0 ]; then break; fi; done
4つの結果から推測するにpthread_sigmask()とsigprocmask()は同じシステムコールを使っているかもしれません。libcのソースコードを見ればわかるはずなので、また今度に見ようと思います。
こちらからどうぞ。
目次: C言語とlibc
シグナルマスクのマニュアル(sigprocmaskのマニュアル)を見ると下記のように「規定されていない」とあります。実際Linuxだとどうなるか気になったのでやってみました。
マルチスレッドのプロセスでsigprocmask()を使用した場合の動作は規定されていない。
実行環境は下記のとおりです。他のプラットフォームや過去/将来のバージョンのLinuxで今回の実験結果と同じ動作をするとは限りませんのでご注意ください。
実験内容は5つのマルチスレッド(親スレッド+4つの子スレッド)で全員でsigwait()するのは共通、誰がsigprocmask()を呼ぶか?を変えながら、下記4パターンを試します。
最後のパターンはマルチスレッドでsigwait()する場合の正しい方法(全スレッドがpthread_sigmask())で、他の3つと動作を比較するためのものです。
コードは長くなってしまったので最後にファイルへのリンクを張っておきます。
実験方法はコンパイル時にマクロを適宜切り替えて、生成された./a.outを起動し、別のターミナルからkillコマンドなどでSIGUSR1を送るだけです。
最初のスレッドがsigprocmask()した場合です。ゆっくり5回シグナルを送ると、親スレッド(th 4)のsigwait()がシグナルを受け取り、子スレッド(th 0)のsigwait()はEINTRが返ってきます。
$ g++ -Wall -g -O2 -DUSE_SIGPROCMASK -DID_MAINTHREAD=4 signal_thread.cpp && ./a.out Use sigprocmask th 0: sub (child ) thread start th 1: sub (child ) thread start th 2: sub (child ) thread start th 4: main (parent) thread start th 4: sigprocmask(block) th 3: sub (child ) thread start th 3: loop start th 2: loop start th 4: loop start th 0: loop start th 1: loop start th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call) th 4: got SIGUSR1 th 0: sigwait failed (Interrupted system call)
一見すると良い感じに動くように見えますが、大量にシグナルを送りつけるとabortします。ありゃりゃ。
$ g++ -Wall -g -O2 -DUSE_SIGPROCMASK -DID_MAINTHREAD=4 signal_thread.cpp && ./a.out Use sigprocmask th 0: sub (child ) thread start th 1: sub (child ) thread start th 2: sub (child ) thread start th 4: main (parent) thread start th 4: sigprocmask(block) th 3: sub (child ) thread start th 3: loop start th 1: loop start th 0: loop start th 2: loop start th 4: loop start ユーザー定義シグナル1 (別ターミナルから) $ while :; do kill -USR1 123450; if [ $? -ne 0 ]; then break; fi; done
一見動くように見えて、だめなパターンですね。
最初のスレッド「以外」の1スレッドがsigprocmask()した場合です。1回シグナルを送っただけでabortしました。
$ g++ -Wall -g -O2 -DUSE_SIGPROCMASK -DID_MAINTHREAD=0 signal_thread.cpp && ./a.out Use sigprocmask th 0: main (child ) thread start th 0: sigprocmask(block) th 1: sub (child ) thread start th 2: sub (child ) thread start th 4: sub (parent) thread start th 3: sub (child ) thread start th 3: loop start th 0: loop start th 2: loop start th 1: loop start th 4: loop start ユーザー定義シグナル1
結果1の動きを見る限り納得の結果と言えるでしょう。シグナルは常に親スレッドにも飛んでいたので、シグナルをマスクしてない親スレッドはabortするのはそりゃそうだなと思います。
続きはまた今度やります。
こちらからどうぞ。
目次: C言語とlibc
POSIXには時間を表す構造体が2つあります。マイクロ秒単位のstruct timevalと、ナノ秒単位のstruct timespecです。struct timespecの方が後発なのか、比較的新しいAPIはstruct timespecを使う傾向にありますが、基本的には混在しています。
struct timeval {
time_t tv_sec; /* 秒 */
suseconds_t tv_usec; /* マイクロ秒 */
};
struct timespec {
time_t tv_sec; /* 秒 */
long tv_nsec; /* ナノ秒 */
};
見てのとおり構造体ですから、足し算や引き算をするにも桁上がりを考える必要があって一苦労必要かと思いきや、POSIXはtimeradd/timersubなどのマクロを用意しており、struct timevalの加減算と比較などが比較的簡単に行えます。便利ですね。
ではstruct timespecにも同様にtimespecadd/timespecsubがあると思いますよね?ところが一部のlibc(BSD系やnewlib)以外は実装していません。なぜ……!?
#define timespecadd(a, b, res) \
do { \
(res)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
(res)->tv_nsec = (a)->tv_nsec + (b)->tv_nsec; \
if ((res)->tv_nsec >= 1000000000) { \
(res)->tv_sec++; \
(res)->tv_nsec -= 1000000000; \
} \
} while (0)
#define timespecsub(a, b, res) \
do { \
(res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(res)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
if ((res)->tv_nsec < 0) { \
(res)->tv_sec--; \
(res)->tv_nsec += 1000000000; \
} \
} while (0)
#define timespecclear(tsp) ((tsp)->tv_sec = (tsp)->tv_nsec = 0)
#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
#define timespeccmp(a, b, cmp) \
(((a)->tv_sec == (b)->tv_sec) ? \
((a)->tv_nsec cmp (b)->tv_nsec) : \
((a)->tv_sec cmp (b)->tv_sec))
いつもstruct timespecを要求するAPIを使うたびにtimespecadd/timespecsubがなくて演算しづらさにイライラするので、コピペで使い回せるように実装例をメモしておきます。この程度なら誰が書いてもほとんど同じコードになると思いますが、気になる方のためにもし著作権が発生する場合はBSD 3条項ライセンス扱いでお願いします。
目次: ゲーム
首都高バトル(Steam版)高ランクの車をひたすらフルチューンするやつの続きです。前回と合わせて8車種ほどフルチューンしました。もう飽きました。
SUZUKI SWIFT Sport(ZC33S)フルチューン
検索用にフルチューン後の主要パラメータを書いておきます。ちなみにスピード指標はギア比を最高速重視にすると高い数値になるので、参考程度です。
車種 | 最高出力 | 最大トルク | スピード指標 | 重量 |
---|---|---|---|---|
SWIFT Sport(ZC33S) '22 | 241PS/5,600rpm | 42kg/2,800rpm | 348.40 | 949kg |
SUPRA RZ(JZA80) '97 | 435PS/5,600rpm | 74kg/3,600rpm | 419.78 | 1,480kg |
あとはインプレッサのどれかをフルチューンするかもなあくらいですが、あまり気力が沸きません。
アップデート前は車高は下げれば下げるほどカーブが速くなる謎システムでしたが、アップデート後は限界まで車高を下げるとストレートで地面と擦って火花が出る&逆に遅くなる変更が入ったそうです。キャプチャでは5段階のうち4まで下げた状態で撮っています。個人的には見た目は3か2くらいが一番バランスが良いですね。
5まで下げるとおかしなことが起きる車もあって、RX-7はフロントを限界まで下げるとタイヤがフェンダーとボンネットを貫通して変な表示になります……。
< | 2025 | > | ||||
<< | < | 05 | > | >> | ||
日 | 月 | 火 | 水 | 木 | 金 | 土 |
- | - | - | - | 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
合計:
本日: