psコマンドに潜むBSDとSystemVの系譜

オライリーの「詳細システム・パフォーマンス」を読んでいると、経験豊富な作者による、さらりとした昔話に触れられる。

システムパフォーマンスを見るときにおなじみのpsコマンドについて「このスタイルのオペレーションはBSDが起源で、auxオプションの前にダッシュがないところから見分けられる」という記載があった。

なるほど、昔からps auxとps -efでハイフンがあったりなかったりするのは何かと思ったら、別々の起源のツールがひとつのpsというコマンドに集約されたというわけだ。Linuxのman psを見たら、確かにそう書いてあった。

This version of ps accepts several kinds of options:
1 UNIX options, which may be grouped and must be preceded by a dash.
2 BSD options, which may be grouped and must not be used with a dash.
3 GNU long options, which are preceded by two dashes.

UNIXオプションはダッシュを付けて、BSDオプションはダッシュを付けない。ちなみにダッシュを2つ付けるGNUオプションなんていうものもある。

ちなみに「詳細システム・パフォーマンス」ではUNIXオプションのことをsvr4スタイルと呼んでいる。

a : Lift the BSD-style "only yourself" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when the ps personality setting is BSD-like. The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes with a terminal (tty), or to list all processes when used together with the x option.

psは何も指定しないと「only yourself(自分自身が起動しているプロセスのみ表示する)」だが、aを「BSDスタイル(ハイフンなし)」で指定すると、これを解除されて全てのプロセスが表示される。

一方で、BSD由来のMacOSでman psと起動すると、同じ-aオプションも、ちょっと様子が違う。

-a : Display information about other users' processes as well as your own. This will skip any processes which do not have a controlling terminal, unless the -x option is also specified.

-aを付けると自分以外のプロセスも表示されるという記載だけだ。

さて、そのpsだが今はprocps-ngとしてメンテナンスされているようで、Gitlabでソースコードも読める。なかでもparser.cはBSDとの歴史的な経緯を一手に吸収していて、ジョージアの缶コーヒーのCMの「世界は誰かの仕事でできている」というキャッチコピーを思い出す。

https://gitlab.com/procps-ng/procps/-/blob/master/ps/parser.c




この記事が気に入ったらサポートをしてみませんか?