Python | os.WIFEXITED () method

Python Methods and Functions | wifexited

The os.wifexited () method in Python is used to check if the process has terminated using the exit (2) system call. This method takes as a parameter the process status code returned by the os.system (), os.wait () or os.waitpid () method and returns True if the process terminated using the exit system call (2), otherwise returns False.

Syntax: os.wifexited(status)

Parameter:
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 True 
if the process exited using exit(2) system call 
else returns False.





os.wifexited() example 1

def poll(self, flag=os.WNOHANG):
            if self.returncode is None:
                while True:
                    try:
                        pid, sts = os.waitpid(self.pid, flag)
                    except os.error as e:
                        if e.errno == errno.EINTR:
                            continue
                        # Child process not yet created. See #1731717
                        # e.errno == errno.ECHILD == 10
                        return None
                    else:
                        break
                if pid == self.pid:
                    if os.WIFSIGNALED(sts):
                        self.returncode = -os.WTERMSIG(sts)
                    else:
                        assert os.wifexited(sts)
                        self.returncode = os.WEXITSTATUS(sts)
            return self.returncode 





os.wifexited() example 2

def poll(self, flag=os.WNOHANG):
            if self.returncode is None:
                while True:
                    try:
                        pid, sts = os.waitpid(self.pid, flag)
                    except os.error as e:
                        if e.errno == errno.EINTR:
                            continue
                        # Child process not yet created. See #1731717
                        # e.errno == errno.ECHILD == 10
                        return None
                    else:
                        break
                if pid == self.pid:
                    if os.WIFSIGNALED(sts):
                        self.returncode = -os.WTERMSIG(sts)
                    else:
                        assert os.wifexited(sts)
                        self.returncode = os.WEXITSTATUS(sts)
            return self.returncode 





os.wifexited() example 3

def platformProcessEvent(self, status):

        if os.wifexited(status):
            tid = self.getMeta("ThreadId", -1)
            if tid != self.getPid():
                # Set the selected thread ID to the pid cause
                # the old one's invalid
                if tid in self.pthreads:
                    self.pthreads.remove(tid)
                self.setMeta("ThreadId", self.getPid())
                self._fireExitThread(tid, os.WEXITSTATUS(status))
            else:
                self._fireExit(os.WEXITSTATUS(status))

        elif os.WIFSIGNALED(status):
            self.setMeta("ExitCode", os.WTERMSIG(status))
            self.fireNotifiers(vtrace.NOTIFY_EXIT)

        elif os.WIFSTOPPED(status):
            sig = os.WSTOPSIG(status)
            self.handlePosixSignal(sig)

        else:
            print "OMG WTF JUST HAPPENED??!?11/!?1?>!" 





os.wifexited() example 4

def poll(self, flag=os.WNOHANG):
            if self.returncode is None:
                while True:
                    try:
                        pid, sts = os.waitpid(self.pid, flag)
                    except os.error as e:
                        if e.errno == errno.EINTR:
                            continue
                        # Child process not yet created. See #1731717
                        # e.errno == errno.ECHILD == 10
                        return None
                    else:
                        break
                if pid == self.pid:
                    if os.WIFSIGNALED(sts):
                        self.returncode = -os.WTERMSIG(sts)
                    else:
                        assert os.wifexited(sts)
                        self.returncode = os.WEXITSTATUS(sts)
            return self.returncode 





os.wifexited() example 5

def signalStatus(self, signal):
        """
        Construct a status from the given signal.

        @type signal: L{int} between 0 and 255 inclusive.
        @param signal: The signal number which the status will represent.

        @rtype: L{int}
        @return: A status integer for the given signal.
        """
        # /* If WIFSIGNALED(STATUS), the terminating signal.  */
        # #define __WTERMSIG(status)      ((status) & 0x7f)
        # /* Nonzero if STATUS indicates termination by a signal.  */
        # #define __WIFSIGNALED(status) \
        #    (((signed char) (((status) & 0x7f) + 1) >> 1) > 0)
        status = signal

        # Sanity check
        self.assertTrue(os.WIFSIGNALED(status))
        self.assertEqual(os.WTERMSIG(status), signal)
        self.assertFalse(os.wifexited(status))

        return status 





os.wifexited() example 6

def exitStatus(self, code):
        """
        Construct a status from the given exit code.

        @type code: L{int} between 0 and 255 inclusive.
        @param code: The exit status which the code will represent.

        @rtype: L{int}
        @return: A status integer for the given exit code.
        """
        # /* Macros for constructing status values.  */
        # #define __W_EXITCODE(ret, sig)  ((ret) << 8 | (sig))
        status = (code << 8) | 0

        # Sanity check
        self.assertTrue(os.wifexited(status))
        self.assertEqual(os.WEXITSTATUS(status), code)
        self.assertFalse(os.WIFSIGNALED(status))

        return status 





os.wifexited() example 7

def GetExitStatus(exit_code):
  """Returns the argument to exit(), or -1 if exit() wasn't called.

  Args:
    exit_code: the result value of os.system(command).
  """

  if os.name == 'nt':
    # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns
    # the argument to exit() directly.
    return exit_code
  else:
    # On Unix, os.WEXITSTATUS() must be used to extract the exit status
    # from the result of os.system().
    if os.wifexited(exit_code):
      return os.WEXITSTATUS(exit_code)
    else:
      return -1 





os.wifexited() example 8

def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
                _WTERMSIG=os.WTERMSIG, _wifexited=os.wifexited,
                _WEXITSTATUS=os.WEXITSTATUS, _WIFSTOPPED=os.WIFSTOPPED,
                _WSTOPSIG=os.WSTOPSIG):
            """All callers to this function MUST hold self._waitpid_lock."""
            # This method is called (indirectly) by __del__, so it cannot
            # refer to anything outside of its local scope.
            if _WIFSIGNALED(sts):
                self.returncode = -_WTERMSIG(sts)
            elif _wifexited(sts):
                self.returncode = _WEXITSTATUS(sts)
            elif _WIFSTOPPED(sts):
                self.returncode = -_WSTOPSIG(sts)
            else:
                # Should never happen
                raise SubprocessError("Unknown child exit status!") 





os.wifexited() example 9

def poll(self, flag=os.WNOHANG):
        if self.returncode is None:
            while True:
                try:
                    pid, sts = os.waitpid(self.pid, flag)
                except OSError as e:
                    # Child process not yet created. See #1731717
                    # e.errno == errno.ECHILD == 10
                    return None
                else:
                    break
            if pid == self.pid:
                if os.WIFSIGNALED(sts):
                    self.returncode = -os.WTERMSIG(sts)
                else:
                    assert os.wifexited(sts)
                    self.returncode = os.WEXITSTATUS(sts)
        return self.returncode 





Archived version

os.wifexited() in Python is used for validation, os.wifexited () Whether the process 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 a parameter and returns True if the process exited using system call exit (2) , otherwise it returns False .

Syntax: os.wifexited (status)

Parameter:
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 True if the process exited using exit (2) system call else returns False.

Code: Method usage os.wifexited ()

# Python program to explain the os method .wifexited ()

 
# import of the os module

import os

  
# Create a child process
# using the os.fork () method

pid = os.fork ()

 

 
# pid more 0
# points to the parent process

if pid & gt;  0 :

 

# Create another child

pid2 = os.fork ()

 

if pid2 & gt;  0 :

 

print ( "In parent process" )

# Wait for completion

# of the first child process and

# get your pid and

  # indication of exit status with

# os.waitpid () method

info1 = os.waitpid (pid, 0 )

 

 

# Wait for completion

  # of the second child process and

# get your pid and

# indicate exit status with

# os.waitpid () method

info2 = os.waitpid (pid2, 0 )

 

 

# os.waitpid () method returns a tuple

# the first attribute represents the child's pid

# while the second represents

# indication of exit status

 

# Check if the first child

# exit using system call exit (2)

# using the 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

# exit using a system call exit (2)

# using method o s.wifexited ()

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 ( "In second child process" )

print ( " Process ID: " , os.getpid ())

print ( "Hey! there " )

  print ( "Second child aborted" )

 

# os.abort () method

# generate 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 the system call exit (2)

os._exit ( 5 )

Exit:

 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. 

Links: https://docs.python.org/3/library/os.html#os.wifexited





Python | os.WIFEXITED () method: StackOverflow Questions

Tutorials