It is possible to get the process ID and parent process ID from with in the process itself.

Python’s inbuilt ‘os’ module provides two methods namely os.getpid() and os.getppid() that returns the PID of the current process and the PID of parent process respectively.

>>> import os
>>> 
>>> # Get process ID of current process
>>> os.getpid()
7516
>>> Get process name for the PID
>>> os.system("ps -o cmd= 7516")
python
0
>>> 
>>> # Get process ID of the parent process
>>> os.getppid()
5177
>>> # Get process name for the PID.
>>> os.system("ps -o cmd= 5177")
-bash
0
>>> 

The process name of PID 7516 returned by os.getpid(), is ‘python’. This is obvious since we got into in the python prompt by running “python” command. The process name of PID 5177 returned by os.getppid() is bash. Python prompt is launched from a bash terminal and python is a child process to bash.