blob: fc94a425dbcb13326460c6ca3f5fee267a560878 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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')
|