Python os.WIFEXITED() – Check if a process has exited using the exit(2) system call
Python os.WIFEXITED() Method
The os.WIFEXITED() method in Python is used to check if a process has exited using the exit(2) system call. This method takes the process status code returned by the os.system(), os.wait(), or os.waitpid() methods as an argument and returns True if the process has exited using the exit(2) system call, otherwise it returns False.
os.WIFEXITED Syntax
os.WIFEXITED(status)
os.WIFEXITED(status) Parameters
status: This parameter accepts the process status code (an integer value) returned by the os.system(), os.wait(), or os.waitpid() methods.
Return type: If the process exits using the exit(2) system call, this method returns True, otherwise it returns False.
os.WIFEXITED(status) Example 1
Usage of os.WIFEXITED() method
# Python program to explain os.WIFEXITED() method
# importing os module
import os
#Create a child process
# using os.fork() method
pid = os.fork()
# pid greater than 0
# indicates the parent process
if pid > 0:
#Create one more child
pid2 = os.fork()
If pid2 > 0:
print("nIn parent process")
# Wait for the completion
# of first child process and
# get its pid and
# exit status indication using
# os.waitpid() method
info1 = os.waitpid(pid, 0)
# Wait for the completion
# of second child process and
# get its pid and
# exit status indication using
# os.waitpid() method
info2 = os.waitpid(pid2, 0)
# os.waitpid() method returns a tuple
# first attribute represents child's pid
# while second one represents
# exit status indication
# Check if the first child
# exited using exit(2) system call
# using os.WIFEXITED() method
If os.WIFEXITED(info1[1]):
print("First child exited using exit(2) system call.")
else:
print("First child does not exited using
exit(2) system call.")
# Check if the second child
# exited using exit(2) system call
# using os.WIFEXITED() method
If os.WIFEXITED(info2[1]):
Print("Second child exited using exit(2) system call.")
else:
print("Second child does not exited using
exit(2) system call.")
else:
print("nIn second child process")
Print("Process ID:", os.getpid())
print("Hey! there")
print("Second child aborted")
# os.abort() method will
# generate a SIGABRT signal
# to the current process.
os.abort()
else :
Print("In first child process")
Print("Process ID:", os.getpid())
Print("Hello! Geeks")
Print("First child exiting..")
Exit using exit(2) system call
os._exit(5)
Output:
In first child process
Process ID: 11614
Hello! Geeks
First child exiting..
In second child process
Process ID: 11615
Hey! there
Second child aborted
In parent process
First child exited using exit(2) system call.
Second child does not exited using exit(2) system call.