Python os.abort() - 向当前进程生成SIGABRT信号

Python os.abort()方法

Python中的os.abort()方法用于向当前进程生成SIGABRT信号。在Unix上,此方法产生一个核心转储,而在Windows上,进程立即返回退出码3。

此方法不会调用通过signal.signal()为SIGABRT信号注册的Python信号处理程序。

os.abort 语法

os.abort()

os.abort 参数

不需要参数。

返回类型:该方法在调用进程中不返回任何值。

os.abort 示例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)

os.abort 示例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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程