Python| os.WSTOPSIG() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.WSTOPSIG()
方法用于获取导致进程停止的信号号。该方法将os.wait()
、 os.system()
或os.waitpid()
方法返回的进程状态码作为参数,并返回导致进程停止的信号号。
Syntax: os.WSTOPSIG(status)
Parameters:
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 an integer value which represents the signal number which caused the process to stop.
代码:使用os.WSTOPSIG()
方法
# Python program to explain os.WSTOPSIG() 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 the child process
# using os.kill() method
# 'SIGSTOP' signal will
# cause the 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)
# os.waitpid() method
# returns a tuple which
# represents child's pid
# and exit status code
print("\nIn parent process")
# Get the signal number due
# to which child process stopped
# using os.WSTOPSIG() method
stopSignal = os.WSTOPSIG(info[1])
print("Child stopped due to signal no:", stopSignal)
print("Signal name:", signal.Signals(stopSignal).name)
else :
print("In child process")
print("Process ID:", os.getpid())
print("Hello ! Geeks")
print("Exiting")
输出:
In Child process
In parent process
Child stopped due to signal no: 19
Signal name: SIGSTOP
参考资料: https://docs。 Python.org/3/library/os.html#os.WSTOPSIG