-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork_simple.py
35 lines (29 loc) · 893 Bytes
/
fork_simple.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
__author__ = 'panc'
import os
# In this example, we fork a child process which
# call a function defined in the same script.
# That is, in the parent(), a child process is forked
# and it calls the child() function.
def child():
print("\nA new child", os.getpid)
os._exit(0)
def parent():
# a parent process forks every time the
# user types in a 'c', when prompted.
while True:
# fork and enter into a childe process
newpid = os.fork()
# 0 means we are in the child process
# A positive number means we are in the
# parent process.
if newpid == 0:
child()
else:
pids = (os.getpid(), newpid)
print("parent: %d, child: %d\n" % pids)
reply = raw_input("q for quit / c for new fork")
if reply == 'c':
continue
else:
break
parent()