Python| os.waitid() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.waitid()
方法被进程用来等待一个或多个子进程完成。
Syntax: os.waitid(idtype, id, options)
Parameters:
id: An integer value representing the process id of child to wait on.
idtype: The idtype and id parameter specify which child the method waits for.
Return type: This method returns an object which represents the data contained in siginfo_t structure.
代码 #1:使用os.waitid()
方法
# Python program to explain os.waitid() 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.waitid() method
# Specify idtype
idtype = os.P_PID
# Specify id
id = pid
# Specify option
option = os.WEXITED
status = os.waitid(idtype, id, option)
print("\nIn parent process-")
# Print status
print("Status of child process:")
print(status)
else :
print("In Child process-")
print("Process ID:", os.getpid())
print("Hello ! Geeks")
print("Exiting..")
输出:
In Child process-
Process ID: 10309
Hello! Geeks
Exiting..
In parent process-
Status of child process:
posix.waitid_result(si_pid=10309, si_uid=1000, si_signo=17, si_status=0, si_code=1)
代码 #2:使用os.waitid()
方法
# Python program to explain os.waitid() 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 :
# Create one more child process
pid2 = os.fork()
if pid2 :
# Wait for the completion of
# any child processes using
# os.waitid() method
# Specify idtype
idtype = os.P_ALL
# Specify id
# As idtype is os.P_ALL
# method will wait for
# any children and specified id
# is ignored.
id = pid
# Specify option
option = os.WSTOPPED | os.WEXITED
status = os.waitid(idtype, id, option)
print("\nIn parent process-")
# Print status
print("Status of completed child process:")
print(status)
else :
print("\nIn Second Child process-")
print("Process ID:", os.getpid())
print("Hey ! There ")
print("Exiting")
else :
print("In First Child process-")
print("Process ID:", os.getpid())
print("Hello ! Geeks")
print("Exiting")
输出:
In First Child process-
Process ID: 11524
Hello! Geeks
Exiting
In Second Child process-
Process ID: 11525
Hey! There
Exiting
In parent process-
Status of completed child process:
posix.waitid_result(si_pid=11524, si_uid=1000, si_signo=17, si_status=0, si_code=1)
参考:
- https://文档。 Python.org/3/library/os.html#os.waitid
- https://linux.die.net/man/3/waitid