On 2015-04-09, Hongyi Zhao <
hongy...@gmail.com> wrote:
> On Thu, 09 Apr 2015 00:00:32 -0400, Barry Margolin wrote:
>
> [snip]
>
>> 4930 is the process group leader, 4935 is a child of that process.
>
> Then, as I've posted in the this thread, how can I obtain the pids for
> all of these children processes?
There isn't any systel-level API for doing that in POSIX. You have to
scrape that information out of the output of "ps", or else look toward
system-specific methods like the /proc filesystem on Linux.
POSIX groups and job control exists in order to support some features of the
tty + shell environment.
The shell command interpreter parses command pipelines and generates processes
which are lumped together in a group. The shell knows the process ID's of all
these processes because it created them. It knows which group is the foreground
process group at any time, and so on. Internally, it has data structures to
keep track of this.
The shell language could have language features to allow scripts to get at this
information: a command pipeline or group could be exposed as some kind of
object, which could be traversed to obtain its components, including the ID's
of the processes it contains. This just doesn't exist, though.
There is the "jobs" command which only lists the active command pipes, along
with the pid of their process group leader. You cannot get to the individual pids
of the pipeline components.
"jobs" works by traversing the internal data structures of the shell that it
uses for keeping track of jobs. Similar commands could be provided to inspect
more deeply into the jobs, but aren't.