blob: a98dfbfd932f40841bed38e0d3f1c46dc3436fde (
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
|
# -*- 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()
|