According to the man, $$:
Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.
I did some testing:
$ echo $$
20823
$ echo $$ &
[1] 6152
20823
$ ( echo $$ )
20823
[1]+ Done echo $$
$ ( echo $$ ) &
[1] 10092
$ 20823
[1]+ Done ( echo $$ )
$ f() { echo f: $$ ; }
$ f
f: 20823
$ f &
[1] 5674
$ f: 20823
[1]+ Done f
$ ( f ) &
[1] 7219
f: 20823
$ ( f )
f: 20823
[1]+ Done ( f )
Is it safe to assume that a backgrounded process counts as a subshell, for the purpose of $$? Is this reliable, or just a coincidence?
Instead of relying on $$, I could just save the PID in a var in the main process, and pass it around. But it seems, this is what already is happening with $$, be it (...) or &, or any variation of these.
The following questions-and-answers shed some light, but not conclusive enough for the backgrounded-process vs $$ issue:
How can I get the pid of a subshell?
Do parentheses really put the command in a subshell?
What is the exact difference between a "subshell" and a "child process"?