From 58b4fb228c3f7583386ded86505cb10f2a870129 Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Tue, 7 Feb 2017 20:24:29 -0700 Subject: Just add stuff --- unix_doublefork.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 unix_doublefork.py (limited to 'unix_doublefork.py') diff --git a/unix_doublefork.py b/unix_doublefork.py new file mode 100644 index 0000000..fc94a42 --- /dev/null +++ b/unix_doublefork.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +"""Example of the Unix double fork for daemons.""" +import os +import sys + + +def print_process_info(name): + """Print information about the currently executing process.""" + pid = os.getpid() + pgid = os.getpgid(pid) + sid = os.getsid(pid) + print('{} (PID:{}) (PGID:{}) (SID:{})'.format(name, pid, pgid, sid)) + + +# First Fork +newpid = os.fork() +if newpid > 0: + print_process_info('Parent') + sys.exit() +else: + print_process_info('Child') + os.setsid() + newpid = os.fork() + # Second fork + if newpid == 0: + print_process_info('Grandchild') + else: + print_process_info('Child') + sys.exit() + +# # POP QUIZ!!!! Why won't this work +# print_process_info('Parent') +# os.setsid() +# print_process_info('Parent') +# newpid = os.fork() +# if newpid == 0: +# print_process_info('Child') -- cgit v1.2.3