forked from ii0/readnote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosfork.py
More file actions
35 lines (30 loc) · 1.06 KB
/
Copy pathosfork.py
File metadata and controls
35 lines (30 loc) · 1.06 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
print "I'm going to fork now - the child will write something to a pipe, and the parent will read it back"
processName = "parent"
print "Program executing \n\tpid: %d, processName: %s" % (os.getpid(), processName)
r, w = os.pipe() # these are file descriptors, not file objects
pid = os.fork()
print "pid : %s" % pid
if pid:
# we are the parent
os.close(w) # use os.close() to close a file descriptor
r = os.fdopen(r) # turn r into a file object
print "parent: reading"
txt = r.read()
os.waitpid(pid, 0) # make sure the child process gets cleaned up
else:
# we are the child
processName = "child"
print "Program executing \n\tpid: %d, processName: %s" % (os.getpid(), processName)
os.close(r)
w = os.fdopen(w, 'w')
print "child: writing"
w.write("here's some text from the child")
w.close()
print "child: closing"
sys.exit(0)
print "parent: got it; text =", txt
print "Program executing \n\tpid: %d, processName: %s" % (os.getpid(), processName)