This is not the same as a few of the other posted items dealing with sub-process pids (that I saw anyway).
If zot contains:
echo "main mypid: $$ - lastpid: $!"
(
echo "block mypid: $$ - lastpid: $! - ppid: $PPID"
ps -ef > xxx
sleep 5
echo "block mypid: $$ - lastpid: $! - ppid: $PPID"
) &
echo "main mypid: $$ - lastpid: $! - ppid: $PPID"
sleep 6
Then: ksh zot (under Solaris and MKS) results in:
main mypid: 7318 - lastpid:
block mypid: 7318 - lastpid: - ppid: 607
main mypid: 7318 - lastpid: 7320 - ppid: 607
block mypid: 7318 - lastpid: - ppid: 607
It surprises me that in the forked child process, that $$ still returns the pid of the main process. i.e. both show $$ as 7318 - I would have expected the child to have $$ be $7320. (You can ignore the ppid - I was looking at related notions.)
If you do: "grep 7320 xxx" you get:
root 7320 7318 0 13:59:45 pts/9 0:00 ksh zot
root 7321 7320 1 13:59:45 pts/9 0:00 ps -ef
which clearly shows the child process and if you were to "kill -9" that pid before the 5 seconds runs out, you would not get the second block line.
My intention was for the child process to record its own pid rather than the parent doing it on its behalf. I realise that the parent can use $! but why can the child not use $$ for its own PID? By the way, if you extract the block of code and put it in a separate file and call it, then you get the expected results.
Thank you in advance for any insights.
Michel