Python os.WSTOPSIG() - 获取导致进程停止的信号

Python os.WSTOPSIG()方法

Python中的os.WSTOPSIG()方法用于获取导致进程停止的信号。该方法以os.wait()、os.system()或os.waitpid()方法返回的进程状态码作为参数,并返回导致进程停止的信号号。

os.WSTOPSIG 语法

os.WSTOPSIG(status)

os.WSTOPSIG 参数

status:该参数接受进程状态码(一个整数值),由os.system(),os.wait()或os.waitpid()方法返回。

返回类型:该方法返回一个整数值,表示导致进程停止的信号号。

os.WSTOPSIG 示例

使用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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程