📜  Python| os.abort() 方法

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

Python| os.abort() 方法

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

Python中的os.abort()方法用于向当前进程生成SIGABRT信号。在 Unix 上,此方法产生核心转储,而在 Windows 上,该进程立即返回退出代码 3。
此方法不会调用使用 signal.signal() 为SIGABRT信号注册的Python信号处理程序。

代码 #1:使用os.abort()方法

# Python program to explain os.abort() method 
  
# importing os module  
import os
  
  
print("Hello ! Geeks")
  
# os.abort() method
# will generate 'SIGABRT'
# signal to the current process
# On Unix, a core dump
# will be produced
# On windows, process
# will exit with exit code 3
os.abort()
  
# As process is aborted
# the line after os.abort() statement
# will not be executed.
print("This will not be printed")
输出:
Hello! Geeks
Aborted (core dumped)

代码 #2:使用os.abort()方法

# Python program to explain os.abort() method 
  
# importing os 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 > 0:
    # Parent process    
    print("\nIn Parent process")
      
    # Wait for the completion 
    # of child process and get 
    # its pid and exit status indication
    # using os.wait() method 
    info = os.wait()
          
    sig = os.WTERMSIG(info[1]) 
    print("Child exited due to signal no:", sig)
    print("Signal name:", signal.Signals(sig).name)
  
else :
  
    # child process
    print("In child process")
    print("Process ID:", os.getpid())
    print("Hello ! Geeks")
      
    # Abort the child process
    # by generating SIGABRT signal
    # using os.abort() method
    os.abort()
输出:
In child process
Process ID: 13914
Hello! Geeks

In Parent process
Child stopped due to signal no: 6
Signal name: SIGABRT

参考资料: https://docs。 Python.org/3/library/os.html#os.abort