Python| os.getppid() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.getppid()
方法用于获取当前进程的父进程ID。
Syntax: os.getppid()
Parameter: Not required
Return Type: This method returns a integer value denoting the parent process ID of the current process. The return type of this method is of class ‘int’.
代码 #1:使用 os.getppid() 方法
# Python program to explain os.getppid() method
# importing os module
import os
# Get the Parent process ID
# of the current process
ppid = os.getppid()
# Print the Parent process ID
# of the current process
print("Parent Process ID of current process:", ppid)
输出:
Parent process ID of current process: 7653
代码 #2:使用 os.getppid() 方法
# Python program to explain os.getppid() method
# importing os module
import os
# Check the process ID
# of the current process
pid = os.getpid()
print("Process ID of Current process:", pid)
# Create a child process
try:
pid = os.fork()
except OSError:
exit("Could not create a child process")
# In the child process
# Check its Parent process ID
# os.getppid() will return
# the process ID of its parent process
if pid == 0:
parent = os.getppid()
print("Parent process ID of child process:", parent)
输出:
Process ID of Current process: 7653
Parent process ID of child process: 7653