Python os.getpgid() – Get the process group id of the process with the specified process id
Python os.getpgid()
Python All functions in the os module raise OSError if the file name or path is invalid or inaccessible, or if other arguments are of the correct type but not accepted by the operating system.
In Unix-like operating systems, a process group represents a collection of one or more processes. It is used to control the distribution of signals; that is, when a signal is directed to a process group, every member of the process group receives the signal. Each process group is uniquely identified by a process group id.
The Python os.getpgid() method is used to get the process group id of the process with the specified process id. If the specified process ID is 0, the process group ID of the current process is returned. The process group ID of the current process can also be obtained using the os.getpgrp() method.
Note: The os.getpgid() method is only available on UNIX platforms.
Syntax: os.getpgid(pid)
Parameters:
pid: An integer value representing the process ID for which the process group ID is to be found. If pid is 0, it represents the current process.
Return type: This method returns an integer value representing the process group ID of the process with the specified process ID.
Example 1
Use the os.getpgid() method
# Python program to explain os.getpgid() method
# importing os module
import os
# Get the process group id
# of the current process
# using os.getpgid() method
pid = os.getpid()
pgid = os.getpgid(pid)
# Print the process group id
# of the current process
print("Process group id of the current process:", pgid)
# If pid is 0, process group id
# of the current process
# will be returned
pid = 0
pgid = os.getpgid(pid)
print("Process group id of the current process:", pgid)
# Get the process group id
# of the current process
# using os.getpgrp() method
pgid = os.getpgrp()
print("Process group id of the current process:", pgid)
# Get the process group id
# of the parent process
pid = os.getppid()
pgid = os.getpgid(pid)
print("process group id of the parent process:", pgid)
Output:
Process group id of the current process: 18938
Process group ID of the current process: 18938
Process group id of the current process: 18938
process group id of the parent process: 11376