Trusted answers to developer questions

What is os.getpid() in Python?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

The os module

The os module in Python provides functions that help in interacting with the underlying operating system.

What is a process ID?

Every process is given an integer identifier called process ID or PID.

Note: Click here to learn more about processes in Linux.

The getpid() method

The getpid() method returns the PID of the current process in execution.

Syntax


os.getpid()

Parameters

The method does not take any argument value.

Return value

The method returns the current PID.

Code example

import os
print("Process ID of the current process is:", os.getpid())

Code explanation

  • Line 1: We import the os module.
  • Line 3: We retrieve the current process ID using os.getpid() method and print it to the console.

RELATED TAGS

python
Did you find this helpful?