Skip to content

Instantly share code, notes, and snippets.

@andrewgho
Last active August 29, 2015 13:56
Show Gist options
  • Save andrewgho/8895435 to your computer and use it in GitHub Desktop.
Save andrewgho/8895435 to your computer and use it in GitHub Desktop.
Show that zombie processes get reaped by poll()
#!/usr/bin/env python
# show_poll.py - show that zombie processes get reaped by poll()
# Andrew Ho (andrew@zeuscat.com)
#
# To test: open two terminals. Run this program in one terminal, cut and
# paste the "ps" command that is printed in the other terminal, and keep
# re-running that ps command (Ctrl+P, Enter). After 5 seconds, the sleep
# subprocess exits and becomes a (sleep) zombie. After another 5 seconds,
# the parent process calls child.poll() and you should see it disappear.
import subprocess, os, time
# Subprocess sleeps for 5 seconds, then exits
print "sleep 5"
child = subprocess.Popen(["sleep", "5"])
# User can cut and paste this command to see the zombie after 5 seconds
print "ps u -p {0},{1}".format(os.getpid(), child.pid)
print "# time.sleep(10)"
time.sleep(10)
print "# child.poll()"
child.poll()
print "# child.returncode = {0}".format(child.returncode)
print "# time.sleep(5)"
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment