SUMMARY: Getting PID of a Sub-Shell

From: Crist Clark <Crist.Clark_at_globalstar.com>
Date: Fri Feb 29 2008 - 18:00:10 EST
I got a number of responses. Two pointed to this,

 
http://www.unix.com/shell-programming-scripting/27767-ksh-how-does-line-child-sub-process-get-its-own-pid.html


Which verifies that others have had the same problem, but
does not provide a good workaround.

Someone else noted that this type of behavior is documented
in the manpage for PPID,

     PPID  This variable is set by the shell to the decimal  pro-
           cess  ID  of  the process that invoked the shell. In a
           subshell, PPID will be set to the same value  as  that
           of  the parent of the current shell. For example, echo
           $PPID and (echo $PPID) would produce the same value.

Which lends weight to the idea that it is a feature, not a bug.

I got a note from self-appointed list-cop saying it was off topic.
This was actually urgent even if I did not state it in the message.
I wanted to get the script in production before COB today. Yes, it
turned out not to be Solaris specific, but I had initially done an
(incorrect) test on another OS that made it look like it had
different behavior. Sorry on that account.

OTOH, I got two posts where people wanted to find out if I had
an answer and to please post a summary.

One response just said, "$!", which is a workaround when you
are running asynchronously, but can be a lot of overhead depending
on how you want to do it. Similarly, pulling the info out using
ptree(1), is a little clunky and not reliable if your parent
process is no longer around (you'd presumably be parsing output
of "ptree $$" which doesn't work after $$ is dead).

Finally, the best response mentioned that "$$" is set at an exec(2),
but not at later fork(2)s. So combining that with PPID, the trick
I'm going to use is,

	echo "Parent: $$"
	(
		MYPID=$(ksh -c 'echo $PPID')
		echo "Subshell: $MYPID"
	)

That is, run a command that tells you its parent's PID, which is the
process that called it.

Thanks.

On 2/28/2008 at 2:36 PM, "Crist Clark" <Crist.Clark@globalstar.com>
wrote:
> I've got a ksh script where I'd like to learn the PID of
> a sub-shell within the sub-shell code.
> 
> If you have code like,
> 
> 	$ cat pidtest.sh
> 	/bin/echo "$$"
> 	( /bin/echo "$$" )
> 	$ ksh pidtest.sh
>       29586
>       29586
> 
> You'll see the same number even though the second echo runs
> in a sub-shell with a different PID. If I run the script
> in truss(1) I do see that it is indeed happening as I expect
> it would, but the "$$" does not get changed when the sub-shell
> gets fork(2)ed off.
> 
> Bug or feature?
> 
> Anyway, anyone know of a way to work around it? How do I get
> the PID of that sub-shell from code within the sub-shell?


BB<information contained in this e-mail message is confidential, intended
only for the use of the individual or entity named above. If the reader
of this e-mail is not the intended recipient, or the employee or agent
responsible to deliver it to the intended recipient, you are hereby
notified that any review, dissemination, distribution or copying of this
communication is strictly prohibited. If you have received this e-mail
in error, please contact postmaster@globalstar.com 
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
Received on Fri Feb 29 18:00:39 2008

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:44:10 EST