forked from irr/python-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork.py
executable file
·33 lines (28 loc) · 1 KB
/
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
#!/usr/bin/env python
"""A fork that demonstrates a copied environment"""
import os, psutil, time
from os import environ
def loop(p):
for i in range(10):
print os.getpid(), "...", p.get_cpu_affinity()
time.sleep(2)
def my_fork():
environ['FOO']="baz"
print "FOO environmental variable set to: %s" % environ['FOO']
environ['FOO']="bar"
print "FOO environmental variable changed to: %s" % environ['FOO']
child_pid = os.fork()
if child_pid == 0:
p = psutil.Process(os.getpid())
p.set_cpu_affinity([1])
print "Child Process: PID# %s [%s]" % (os.getpid(), p.get_cpu_affinity())
print "Child FOO environmental variable == %s" % environ['FOO']
loop(p)
else:
p = psutil.Process(os.getpid())
p.set_cpu_affinity([0])
print "Parent Process: PID# %s [%s]" % (os.getpid(), p.get_cpu_affinity())
print "Parent FOO environmental variable == %s" % environ['FOO']
loop(p)
if __name__ == "__main__":
my_fork()