[comp.unix.questions] Parent process ID

ghe@nucthy.physics.orst.edu (Guangliang He) (08/11/89)

We know the shell variable $$ means the process ID of current process.
Is there a variable to represent the Parent Process ID (PPID)? Any hint
will be welcome.


-----------------------------------------------------------------------
                                   |
USMAIL:   Guangliang He            |  INTERNET: ghe@PHYSICS.ORST.EDU
          Department of Physics    |            ghe@jacobs.CS.ORST.EDU
          Oregon State University  |  BITNET:   hegl@orstvm.bitnet
          Corvallis, OR 97331      |  PHONE:    (503) 737-4631
                                   |
-----------------------------------------------------------------------

jim@hp-ptp.HP.COM (James_Rogers) (08/11/89)

There is no standard variable in the environment, but there is a way to
get what you need.

In ksh you can define the following function which will produce the
information you need:

function ppid {
ps -fp $$ | cut -c15-20 |&
read -p ppid
read -p ppid
echo $ppid
}

This function gets the parent process id from the "ps" comand and then
extracts all the surrounding information.

"ps -fp $$" produces the following line:

     UID   PID  PPID  C    STIME TTY      TIME COMMAND
     jim 27859 27818196 08:55:01 ttyp9    0:03 ps -fp 27859

Piping the above data through "cut -c15-20" produces:

 PPID
27818

Ending the pipe command with "|&" causes the output of the command to be
piped back into the current shell.  The first "read -p ppid" then reads
the first line of data from the pipe.  The second "read -p ppid" reads
the second line of data from the pipe.  Now "ppid" contains the value we
want and we echo that value to stdout.


Jim Rogers at Hewlett Packard

davidsen@sungod.crd.ge.com (ody) (08/11/89)

In article <12046@orstcs.CS.ORST.EDU> ghe@nucthy.physics.orst.edu (Guangliang He) writes:
| We know the shell variable $$ means the process ID of current process.
| Is there a variable to represent the Parent Process ID (PPID)? Any hint
| will be welcome.

Korn shell uses $PPID. You could trivially set your .profile to define
such a veriable if you run another shell.
	bill davidsen		(davidsen@crdos1.crd.GE.COM)
  {uunet | philabs}!crdgw1!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

jim@hp-ptp.HP.COM (James_Rogers) (08/12/89)

The previous function had some drawbacks (spawning a background job,
excess output, etc.).

A much more concise set of functions to do the same thing in ksh is:

function second {
	echo "$2\c"
	}

function ppid {
	second `ps -fp $$ | cut -c15-20`
	echo
	}

These functions work cleanly in the ksh.  You will not get the same results
in sh because functions in sh are run as separate processes.
In ksh functions are run as part of your existing shell.

For sh you would define the functions as follows:

second () {
	echo "$2\c"
	}

ppid () {
	second `ps -fp $1 | cut -c15-20`
	echo
	}

You would then call ppid in the following manner:

	ppid $$

This would pass your pid to "ppid" and then everything would execute properly.


Jim Rogers