Python| os.WIFSTOPPED() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.WIFSTOPPED()
方法用于检查进程是否已停止。该方法将os.wait()
、 os.system()
或os.waitpid()
方法返回的进程状态码作为参数,如果进程已停止则返回 True,否则返回 False。
Syntax: os.WIFSTOPPED(status)
Parameter:
status: This parameter takes process status code (an integer value) as returned by os.system(), os.wait() or os.waitpid() method.
Return type: This method returns a boolean value of class ‘bool’. This method returns True if the process has been stopped, otherwise returns False.
代码:使用os.WIFSTOPPED()
方法
# Python program to explain os.WIFSTOPPED() method
# importing os and signal module
import os, signal
# Create a child process
# using os.fork() method
pid = os.fork()
# pid greater than 0
# indicates the parent process
if pid :
# Send signal 'SIGSTOP'
# to child process
# using os.kill() method
# signal will cause the child
# process to stop
os.kill(pid, signal.SIGSTOP)
# Get the child's pid and
# status code using
# os.waitpid() method
info = os.waitpid(pid, os.WSTOPPED)
# info is a tuple
# info[0] represents child's pid
# info[1] represents exit status code
print("\nIn parent process")
# Check whether the child process
# has been stopped or not
# using os.WIFSTOPPED() method
isStopped = os.WIFSTOPPED(info[1])
print("Has child process been stopped?")
print(isStopped)
else :
print("In Child process")
print("Process ID:", os.getpid())
print("Hello ! Geeks")
输出:
In Child process
Process ID: 10224
Hello! Geeks
In parent process
Has child process been stopped?
True
参考资料: https://docs。 Python.org/3/library/os.html#os.WIFSTOPPED