Quick update to the README file. For intros and books we now point to
[python/dscho.git] / Lib / test / test_fork1.py
blob361664e00e93cf2f0f8d1b29b0968eb3e8252348
1 """This test checks for correct fork() behavior.
3 We want fork1() semantics -- only the forking thread survives in the
4 child after a fork().
6 On some systems (e.g. Solaris without posix threads) we find that all
7 active threads survive in the child after a fork(); this is an error.
9 """
11 import os, sys, time, thread
13 LONGSLEEP = 2
15 SHORTSLEEP = 0.5
17 alive = {}
19 def f(id):
20 while 1:
21 alive[id] = os.getpid()
22 try:
23 time.sleep(SHORTSLEEP)
24 except IOError:
25 pass
27 def main():
28 for i in range(4):
29 thread.start_new(f, (i,))
31 time.sleep(LONGSLEEP)
33 a = alive.keys()
34 a.sort()
35 assert a == range(4)
37 cpid = os.fork()
39 if cpid == 0:
40 # Child
41 time.sleep(LONGSLEEP)
42 n = 0
43 pid = os.getpid()
44 for key in alive.keys():
45 if alive[key] == pid:
46 n = n+1
47 os._exit(n)
48 else:
49 # Parent
50 spid, status = os.waitpid(cpid, 0)
51 assert spid == cpid
52 assert status == 0, "cause = %d, exit = %d" % (status&0xff, status>>8)
54 main()