What is os.getpid() in Python?
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 osprint("Process ID of the current process is:", os.getpid())
Code explanation
- Line 1: We import the
osmodule. - Line 3: We retrieve the current process ID using
os.getpid()method and print it to the console.