Python | os.abort() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.abort() method in Python is used to generate a SIGABRT signal to the current process. On Unix, this method produces a core dump while on Windows, the process immediately returns an exit code of 3.
This method does not call the Python signal handler registered for SIGABRT signal with signal.signal().

Syntax: os.abort()



Parameter: No parameter is required.

Return type: This method does not return any value in the calling process.

Code #1: Use of os.abort() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")

chevron_right


Output:

Hello! Geeks
Aborted (core dumped)

Code #2: Use of os.abort() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:

In child process
Process ID: 13914
Hello! Geeks

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

References: https://docs.python.org/3/library/os.html#os.abort




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.