Python os.getppid() Method

Python os.getppid() Method

  1. Syntax of Python os.getppid() Method
  2. Example 1: Use the os.getppid() Method in Python
  3. Example 2: Make a Child Process in the os.getppid() Method
  4. Example 3: Make a Grandchild Process in the os.getppid() Method

Python os.getppid() method is an efficient way of retrieving the parent’s process id of the current process.

Syntax of Python os.getppid() Method

os.getppid()

Parameters

This method doesn’t accept any parameters.

Return

The return type of this method is an integer value that represents the parent process ID of the process in the running.

Example 1: Use the os.getppid() Method in Python

import os

pid = os.getppid()

print("The parent ID of the current process is:", pid)

Output:

The parent ID of the current process is: 4192

While multiple processes are running, you can use the os.getppid() method to know which parent function is being executed in that instance.

Example 2: Make a Child Process in the os.getppid() Method

import os

ppid = os.getpid()

print("The parent ID of the current process is:", ppid)

try:
	ppid = os.fork()

except OSError:

	exit("The child process could not be created.")

if ppid == 0:

	parentID = os.getppid()

	print("The parent ID of the child process is:", parentID)

Output:

The parent ID of the current process is: 2306
The parent ID of the child process is: 2306

On any UNIX system, when the parent process has exited, the id returned is of the init process.

Example 3: Make a Grandchild Process in the os.getppid() Method

import os

ppid = os.getpid()

print("The parent ID of the current process is:", ppid)

print("From the child process:")

print("My process id is:%d"%(os.getpid()))

val = os.fork()

if val == 0:

    print("From the grandchild process:")

    print("From the grandchild process: CHILD ID is:%d"%(os.getpid()))

    print("From the grandchild process: PARENT ID is:%d"%(os.getppid()))

    print("From the grandchild process: GRANDPARENT ID is %d"%(ppid))

    print("The grandchild process is exiting.")

Output:

The parent ID of the current process is: 1716
From the child process:
My process id is:1716
From the grandchild process:
From the grandchild process: CHILD ID is:1720
From the grandchild process: PARENT ID is:1716
From the grandchild process: GRANDPARENT ID is 1716
The grandchild process is exiting.

On any Windows OS, when the parent process has exited, the id returned is the same ID, which is reusable.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method