Python| os.wait() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.wait()
方法被进程用来等待子进程的完成。
此方法返回一个包含其 PID 和退出状态指示的元组。子进程的退出状态由一个 16 位数字表示,其低字节是杀死该进程的信号号,高字节是退出状态(如果信号号为零)。
Syntax: os.wait()
Parameter: No parameter is required.
Return type: This method returns a tuple which contains terminated child’s PID and exit status indication.
代码:使用os.wait()
方法
# Python program to explain os.wait() method
# importing os module
import os
# Create a child process
# using os.fork() method
pid = os.fork()
# a Non-zero process id (pid)
# indicates the parent process
if pid :
# Wait for the completion of
# child process using
# os.wait() method
status = os.wait()
print("\nIn parent process-")
print("Terminated child's process id:", status[0])
print("Signal number that killed the child process:", status[1])
else :
print("In Child process-")
print("Process ID:", os.getpid())
print("Hello ! Geeks")
print("Exiting")
# using os.wait() method
# Parent process will wait till
# the completion of child process
# and then only it will
# begin its execution
输出:
In Child process-
Process ID: 6276
Hello! Geeks
Exiting
In parent process-
Terminated child's process id: 6276
Signal number that killed the child process: 0