/ procからプロセスグループIDを取得できますか?

/ procからプロセスグループIDを取得できますか?

存在する」https://stackoverflow.com/questions/13038143/how-to-get-pids-in-one-process-group-in-linux-os「すべての答えで言及されたものを見ましたが、ps言及されていません/proc

「ps」は移植性があまり良くないようです(AndroidとBusyboxのバージョンには異なるパラメータが必要です)。シンプルで移植可能なツールを使用してpgidでpidを一覧表示できることを願っています。

Tgid:/proc/.../statusで(スレッドグループID)、Gid:(グループIDはプロセスをグループ化するのではなくセキュリティのためのものです)を見ることができますが、何もありませんPGid:...

pidでpgidを取得する他の(使用されていない)方法はps何ですか?

ベストアンサー1

あなたはフィールドを見ることができます5番目の場所出力から/proc/[pid]/stat

$ ps -ejH | grep firefox
 3043  2683  2683 ?        00:00:21   firefox

$ < /proc/3043/stat sed -n '$s/.*) [^ ]* [^ ]* \([^ ]*\).*/\1/p'
2683

~からman proc:

/proc/[pid]/stat
              Status information about the process.  This is used by ps(1).  It is defined in /usr/src/linux/fs/proc/array.c.

              The fields, in order, with their proper scanf(3) format specifiers, are:

              pid %d      The process ID.

              comm %s     The filename of the executable, in parentheses.  This is visible whether or not the executable is swapped out.

              state %c    One character from the string "RSDZTW" where R is running, S is sleeping in an interruptible wait, D is waiting in
                          uninterruptible disk sleep, Z is zombie, T is traced or stopped (on a signal), and W is paging.

              ppid %d     The PID of the parent.

              pgrp %d     The process group ID of the process.

              session %d  The session ID of the process.

以下は使用できません。

awk '{print $5}'

これは、ファイルがスペースで区切られたリストではないためです。 2番目のフィールド(プロセス名にはスペースまたは改行文字を含めることができます)。たとえば、ほとんどのスレッドには通常名前にfirefoxスペース文字があります。

)したがって、最後の文字が発生してから3番目のフィールドを印刷する必要があります。

おすすめ記事