Python| os.getpgid() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError 。
在类 UNIX 操作系统中,进程组表示一个或多个进程的集合。它用于控制信号的分发,即当信号被定向到进程组时,进程组的每个成员都会接收到信号。每个进程组都使用进程组 ID 唯一标识。
Python中的os.getpgid()
方法用于获取指定进程id的进程的进程组id。如果指定的进程id为0,则返回当前进程的进程组id。当前进程的进程组 id 也可以使用os.getpgrp()
方法获取。
注意: os.getpgid()
方法仅在 UNIX 平台上可用。
Syntax: os.getpgid(pid)
Parameter:
pid: An integer value representing the process id of the process whose process group id is to be found. If pid is 0, it will represent the current process.
Return Type: This method returns an integer value which represents the process group id of the process with specified process id.
# 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)
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