child pid in ZSH

I am using ZSH shell in Linux.

I am calling a child program in background mode parallely (say 2-3 threads). I have problem in handling the temporary files of these child programs since the temp file names are unique for all the child process.

To distinguish i want to use the pid in the temp files. But when I use the $$ to fetch the pid it is same across all the chils process since the obtained pid is of the master program which called all these.

Could anyone help me to identify the PID of the current child process so taht i cann append in the temp file names. From the previos threads i saw there is a function called getpid() to get the pid. But am not sure of the usage..

When you spawn a child process (not a thread), the pid of the process should be in $!

eg

$ echo &
[1] 21616

[1]+  Done                    echo
$ echo $!
21616

Porter,

Thanks for your response.

I think i am not clear in my question.

Actually i am calling another shell script say xyz.sh in the master program abc.sh in a loop for n number of times in the background mode.

The $$ value gives the PID of the master script in the sub program and hence i am not able to use the pid of the script for naming temp files which gets clashed with the temp files of the other parallely running same sub programs running in background.

Actually in ZSH if i use $! i get 0 as result.

zsh behaves the same way, spawn a child using & and $! is set to the pid of the child.