📜  Python| os.WSTOPSIG() 方法

📅  最后修改于: 2022-05-13 01:55:00.600000             🧑  作者: Mango

Python| os.WSTOPSIG() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。

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

代码:使用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