El módulo OS en Python proporciona funciones para interactuar con el sistema operativo. OS viene bajo los módulos de utilidad estándar de Python. Este módulo proporciona una forma portátil de usar la funcionalidad dependiente del sistema operativo.
El método os.WTERMSIG() en Python se usa para obtener el número de señal que provocó la salida del proceso. Este método toma el código de estado del proceso devuelto por el método os.wait() , os.system() o os.waitpid() como parámetro.
Sintaxis: os.WTERMSIG(estado)
Parámetros:
estado: este parámetro toma el código de estado del proceso (un valor entero) tal como lo devuelve el método os.system(), os.wait() o os.waitpid().
Tipo de devolución: este método devuelve un valor entero que representa el número de señal que provocó la salida del proceso.
Código #1: Uso del método os.WTERMSIG()
Python3
# Python program to explain os.WTERMSIG() 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 : # Wait for the completion of # the child process and get # its pid and # exit status indication info = os.wait() # info is a tuple # info[0] represents child's id # info[1] represents exit status code print("\nIn parent process") # Get the signal number which caused # the child process to exit sig_num = os.WTERMSIG(info[1]) print("Child process exited due to signal no:", sig_num ) # We can get the signal name # corresponding to the signal number # in following way print("Signal name:", signal.Signals(sig_num).name) else : print("In Child process") print("Process ID:", os.getpid()) print("Hello ! Geeks") # os.abort() method will # generate a SIGABRT signal # to the current process # and will produce core dump. os.abort()
In Child process Process ID: 3421 Hello! Geeks In parent process Child process exited due to signal no: 6 Signal name: SIGABRT
Código #2: Uso del método os.WTERMSIG()
Python3
# Python program to explain os.WTERMSIG() 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 : # Create one more child pid2 = os.fork() if pid2 : # Wait for the completion of # first child process and get # its pid and # exit status indication # using os.waitpid() method child1_info = os.waitpid(pid, 0) # Wait for the completion of # second child process and get # its pid and exit status indication # using os.waitpid() method child2_info = os.waitpid(pid2, 0) # os.waitpid() method # returns a tuple which # represents child's pid # and exit status code print("\nIn parent process") # Get the signal number which caused # first child process to exit 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") # Get the signal number which caused # second child process to exit 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") # sig_num equal to 0 represents # that no signal caused # the process to exit 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() method will # generate a SIGABRT signal # to the current process os.abort()
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
Referencias: https://docs.python.org/3/library/os.html#os.WTERMSIG