Reputation: 4857
I opened a terminal and executed the following commands:
$ sleep 100000 &
$ sleep 100000 &
$ bash -c '{ sleep 100000 & } && cat && sleep 500000'
Now I have this process tree:
SID PGRP PID Command
-------------------------------------------------------------
496410 496410 496410 -bash
496410 496427 496427 ├─ sleep 100000
496410 496430 496430 ├─ sleep 100000
496410 500081 500081 └─ bash -c { sleep 100000 & } && cat && sleep 500000
496410 500081 500082 ├─ sleep 100000
496410 500081 500083 └─ cat
From this tree, we can see
-bash
is the session leader because PID == SID
.
There are 4 process groups belonging to the session (PGRP = 496410, 496427, 496430, 500081
).
I have two questions:
A session can have at most 1 foreground process group. In the process tree above, it is the process group 500081 bash -c { sleep 100000 & } && cat && sleep 500000
, right?
The process groups 496427 sleep 100000
and 496430 sleep 100000
are clearly background process groups, but is the session leader 496410 -bash
also considered a member of the background process group (496410
)?
Terminology used in the OP: POSIX
Upvotes: 0
Views: 24
Reputation: 4857
The POSIX's terminology mentioned in the OP is abstract and hard to read. Instead you may want to refer to man 7 credentials
.
As for the questions in the OP:
A session can have at most 1 foreground process group. In the process tree above, it is the process group 500081 bash -c { sleep 100000 & } && cat && sleep 500000, right?
Yes.
The process groups 496427 sleep 100000 and 496430 sleep 100000 are clearly background process groups, but is the session leader 496410 -bash also considered a member of the background process group (496410)?
Yes.
These can be confirmed just by enabling TPGID (Process ID of the fg process group of the controlling terminal)
column in htop
:
SID PGRP PID TPGID Command
-------------------------------------------------------------
496410 496410 496410 500081 -bash
496410 496427 496427 500081 ├─ sleep 100000
496410 496430 496430 500081 ├─ sleep 100000
496410 500081 500081 500081 └─ bash -c { sleep 100000 & } && cat && sleep 500000
496410 500081 500082 500081 ├─ sleep 100000
496410 500081 500083 500081 └─ cat
Upvotes: 0