-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork.py
35 lines (28 loc) · 860 Bytes
/
fork.py
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
import os as o
import logboot
import logging
import sys
import time
from colorama import Fore
hello = " Hello World!"
logboot.config_formatter('P', Fore.YELLOW)
logging.info(f"command line args: {sys.argv}")
if len(sys.argv) > 1: me = 'E'
logging.info(f"{o.getpid()} pre-fork")
f_pid = o.fork()
if f_pid == 0:
logboot.config_formatter('C', Fore.GREEN)
logging.info(f"post-fork forked={f_pid}")
logging.info(f"{hello} parent pid={o.getppid()}")
if f_pid > 0:
logging.info("waiting for child to terminate")
result = o.waitpid(f_pid, 0)
logging.info(f"child terminated, result={result}")
logging.info("I will terminate")
if len(sys.argv) < 2:
logging.info("I will now exec")
o.execl(sys.executable, sys.argv[0], sys.argv[0], "exec")
else:
logging.info("sleeping")
time.sleep(.5)
logging.info("woke up")