Python | os.WTERMSIG() method
Last Updated :
05 Jul, 2021
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.WTERMSIG() method in Python is used to get the signal number which caused the process to exit. This method takes process status code as returned by os.wait(), os.system() or os.waitpid() method as a parameter.
Syntax: os.WTERMSIG(status)
Parameters:
status: This parameter takes process status code (an integer value) as returned by os.system(), os.wait() or os.waitpid() method.
Return type: This method returns an integer value which represent the signal number which caused the process to exit.
Code #1: Use of os.WTERMSIG() method
Python3
import os, signal
pid = os.fork()
if pid :
info = os.wait()
print ( "\nIn parent process" )
sig_num = os.WTERMSIG(info[ 1 ])
print ( "Child process exited due to signal no:" , sig_num )
print ( "Signal name:" , signal.Signals(sig_num).name)
else :
print ( "In Child process" )
print ( "Process ID:" , os.getpid())
print ( "Hello ! Geeks" )
os.abort()
|
Output:
In Child process
Process ID: 3421
Hello! Geeks
In parent process
Child process exited due to signal no: 6
Signal name: SIGABRT
Code #2: Use of os.WTERMSIG() method
Python3
import os, signal
pid = os.fork()
if pid :
pid2 = os.fork()
if pid2 :
child1_info = os.waitpid(pid, 0 )
child2_info = os.waitpid(pid2, 0 )
print ( "\nIn parent process" )
sig_num = os.WTERMSIG(child1_info[ 1 ])
if sig_num ! = 0 :
print ( "First child process exited due to signal no:" , sig_num )
print ( "Signal name:" , signal.Signals(sig_num).name)
else :
print ( "First child exited normally" )
sig_num = os.WTERMSIG(child2_info[ 1 ])
if sig_num ! = 0 :
print ( "Second child process exited due to signal no:" , sig_num )
print ( "Signal name:" , signal.Signals(sig_num).name)
else :
print ( "Second child exited normally" )
else :
print ( "\nIn second child process" )
print ( "Process id:" , os.getpid())
print ( "Hey ! there" )
print ( "Exiting.." )
else :
print ( "In first child process" )
print ( "Process ID:" , os.getpid())
print ( "Hello ! Geeks" )
os.abort()
|
Output:
In First child process
Process ID: 3752
Hello! Geeks
In second child process
Process id: 3753
Hey! there
Exiting..
In parent process
First child process exited due to signal no: 6
Signal name: SIGABRT
Second child exited normally
References: https://docs.python.org/3/library/os.html#os.WTERMSIG
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...