Just for fun: creating zombies with Python

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jun 2 04:56:46 EDT 2016


Just for fun, I thought I'd create some zombie processes using Linux.

(This will probably only work on POSIX-compliant operating systems. I don't 
know that Windows has zombies.)

I started with the C code given here:

https://en.wikipedia.org/wiki/Zombie_process#Examples

and re-wrote it into Python:


steve at runes:~$ cat zombie.py 
import os, sys, time
pids = [None]*10
for i in range(9, -1, -1):
    pids[i] = os.fork()
    if pids[i] == 0:
        time.sleep(i+1)
        os._exit(0)
for i in range(9, -1, -1):
    os.waitpid(pids[i], 0)



If you run that script on Linux, and watch the process list using (say) top, 
you will see the number of zombies grow up to a maximum of nine, then drop back 
down to (hopefully) zero.


-- 
Steve




More information about the Python-list mailing list