Skip to content

Instantly share code, notes, and snippets.

View lextoumbourou's full-sized avatar

Lex Toumbourou lextoumbourou

View GitHub Profile
^CTraceback (most recent call last):
File "fork.py", line 11, in
time.sleep(1)
KeyboardInterrupt
> ps -ef | grep 26556
lex 26556 26555 0 21:40 pts/3 00:00:00 [python] <defunct>
> python fork_zombie.py
I created a child and now all I want to do is sleep…
Goodbye cruel world!
from os import fork, getppid, wait
from sys import exit
from time import sleep
pid = fork()
if pid == 0:
exit("Goodbye, cruel world")
else:
print "I created a child:", pid
> python fork_wait.py
Hope my parent doesn't forget me this time!
Right now, my parent is 20037
Right now, my parent is 20037
Right now, my parent is 20037
My child has finished processing. My work here is done.
from os import fork, getppid, wait
from sys import exit
from time import sleep
pid = fork()
if pid == 0:
print "Hope my parent doesn't forget me this time!"
for _ in range(3):
sleep(1)
> python fork_orphan.py
Parent here, I just created child 19683
Child process up in this.
Right now, my parent is 19682
> Right now, my parent is 1
Right now, my parent is 1
from os import fork, getppid
from time import sleep
pid = fork()
if pid == 0:
print "I'm about to become an orphan!"
for _ in range(3):
sleep(1)
print "My parent is", getppid()
> python fork_ppid.py
Parent here, I just created child 19741
Child process up in this.
My parent is 19740
from os import fork, getppid
from time import sleep
pid = fork()
if pid == 0: # We're in the child process
print "Child process up in this."
print "My parent is", getppid()
else:
print "Parent here, I just created child", pid