How to Use the Python os.getpid() Function

How to Use the Python os.getpid() Function

In this tutorial, you will learn the basics of working with the getpid() function from the os module to get the process ID (PID) of the current process.

The os module in Python provides functions for interacting with the operating system. This module provides a portable way of using operating system-dependent functionality.

Some of the OS module's functions include creating and deleting directories, starting and killing processes, and obtaining information about the system's users, environment, and current directory. It is, therefore, a powerful and very introductory module when working with OS functionality.

What is a PID?

A PID, or process ID, is a unique number assigned to each process running on a computer. It is used by the operating system to identify and keep track of each process and to differentiate between them.

The PID is typically a positive integer automatically assigned by the operating system when a process is created. It is possible for multiple processes to have the same PID if they are created simultaneously, but this is rare and is typically resolved by the operating system within a short time.

We can use the PID of a process to terminate the process, debug issues associated with the process, check logs, and much more.

Function Syntax, Parameters and Return Value

The getpid() function is a part of the os module in Python.The syntax for using the getpid() function is as follows:

os.getpid()

The getpid() function does not accept any arguments or parameters. It simply returns the process ID of the current process.

The return value of the getpid() function is an integer denoting the PID of the current process.

Example Usage

The following demonstrates how to use the getpid() function in Python.

import os

pid = os.getpid()
print(f"The PID of current process: {pid}.")

Explanation

In this example, we first import the os module.

We then use the getpid() function to obtain the process ID of the current process.

Finally, using the python string formatting operator, we print the PID of the current process.

Code output:

The PID of current process: 2954.

Note that the process ID will be different in your system.

Example 2

We can also use the getpid() function to get the PID of a parent and child process as shown in the example below:

import os
import time

# start new proc
pid = os.fork()

# are we in child proc?
if pid == 0:
    # sleep for 5 seconds
    time.sleep(5)
    
    # child PID
    print(f"Child PID: {os.getpid()}.")
else:
    # parent PID
    print(f"Parent PID: {os.getpid()}.")

Explanation

In the example above, we use the os.fork() function to start a new process.

The os.fork() function creates a new process by copying the current process, and it returns the process ID of the new process in the child process and the process ID of the parent process in the parent process.

We then use the os.getpid() function in both the parent and child processes to obtain the process IDs of each process, and we print the resulting values.

Output

Parent PID: 3013.
--- 5 seconds later
Child PID: 3014.

Note: The fork() function may be unavailable depending on your target system.

Points to note:

One thing to remember when using the getpid() function is that the operating system may reuse the process ID of a process after the process has terminated. Hence a new process can be assigned the same process ID as a previous process that has already been terminated.

In such a scenario, the getpid() function will return the same value for both processes, even though they are different. You can use the getpid() function with other information, such as the creation time, to differentiate the processes.

Conclusion

In this post, you learned how to use the getpid() function from the Python OS module to get the PID of the currently running process.

More Python tutorials coming soon. Get subscribed, so you don't miss them.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.