#!/usr/bin/env python import os import sys import time def show_waitpid_results(results): print("results: %s" % (str(results))) print("core: %r continued: %r stopped: %r signaled: %r exited %r" % ( os.WCOREDUMP(results[1]), os.WIFCONTINUED(results[1]), os.WIFSTOPPED(results[1]), os.WIFSIGNALED(results[1]), os.WIFEXITED(results[1])) ) if os.WIFEXITED(results[1]): print("exit code: %r" % (os.WEXITSTATUS(results[1]))) if __name__ == '__main__': try: pid = os.fork() if pid: print("in parent. child pid: %r" % (pid)) while True: waitpid_result = os.waitpid(pid, os.WNOHANG) show_waitpid_results(waitpid_result) if waitpid_result == (0,0) or waitpid_result == (0,-512): time.sleep(.1) else: break print("parent exiting") else: # child time.sleep(.5) print("child exiting with exit code 10"); sys.exit(10) except Exception: e = sys.exc_info()[1] print("error: %s"%(e)) sys.exit(20)