2000-02-25 19:25:05 +00:00
|
|
|
"""This test checks for correct fork() behavior.
|
|
|
|
|
|
|
|
We want fork1() semantics -- only the forking thread survives in the
|
|
|
|
child after a fork().
|
|
|
|
|
|
|
|
On some systems (e.g. Solaris without posix threads) we find that all
|
|
|
|
active threads survive in the child after a fork(); this is an error.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os, sys, time, thread
|
2000-08-04 13:34:43 +00:00
|
|
|
from test_support import TestSkipped
|
2000-02-25 19:25:05 +00:00
|
|
|
|
2000-05-04 00:36:42 +00:00
|
|
|
try:
|
|
|
|
os.fork
|
|
|
|
except AttributeError:
|
2000-08-04 13:34:43 +00:00
|
|
|
raise TestSkipped, "os.fork not defined -- skipping test_fork1"
|
2000-05-04 00:36:42 +00:00
|
|
|
|
2000-02-25 19:25:05 +00:00
|
|
|
LONGSLEEP = 2
|
|
|
|
|
|
|
|
SHORTSLEEP = 0.5
|
|
|
|
|
2000-04-10 15:36:39 +00:00
|
|
|
NUM_THREADS = 4
|
|
|
|
|
2000-02-25 19:25:05 +00:00
|
|
|
alive = {}
|
|
|
|
|
2000-04-24 14:07:03 +00:00
|
|
|
stop = 0
|
|
|
|
|
2000-02-25 19:25:05 +00:00
|
|
|
def f(id):
|
2000-04-24 14:07:03 +00:00
|
|
|
while not stop:
|
2000-02-25 19:25:05 +00:00
|
|
|
alive[id] = os.getpid()
|
|
|
|
try:
|
|
|
|
time.sleep(SHORTSLEEP)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def main():
|
2000-04-10 15:36:39 +00:00
|
|
|
for i in range(NUM_THREADS):
|
2000-02-25 19:25:05 +00:00
|
|
|
thread.start_new(f, (i,))
|
|
|
|
|
|
|
|
time.sleep(LONGSLEEP)
|
|
|
|
|
|
|
|
a = alive.keys()
|
|
|
|
a.sort()
|
2000-04-10 15:36:39 +00:00
|
|
|
assert a == range(NUM_THREADS)
|
|
|
|
|
|
|
|
prefork_lives = alive.copy()
|
2000-02-25 19:25:05 +00:00
|
|
|
|
|
|
|
cpid = os.fork()
|
|
|
|
|
|
|
|
if cpid == 0:
|
|
|
|
# Child
|
|
|
|
time.sleep(LONGSLEEP)
|
|
|
|
n = 0
|
|
|
|
for key in alive.keys():
|
2000-04-10 15:36:39 +00:00
|
|
|
if alive[key] != prefork_lives[key]:
|
2000-02-25 19:25:05 +00:00
|
|
|
n = n+1
|
|
|
|
os._exit(n)
|
|
|
|
else:
|
|
|
|
# Parent
|
|
|
|
spid, status = os.waitpid(cpid, 0)
|
|
|
|
assert spid == cpid
|
|
|
|
assert status == 0, "cause = %d, exit = %d" % (status&0xff, status>>8)
|
2000-04-24 14:07:03 +00:00
|
|
|
global stop
|
|
|
|
# Tell threads to die
|
|
|
|
stop = 1
|
|
|
|
time.sleep(2*SHORTSLEEP) # Wait for threads to die
|
2000-02-25 19:25:05 +00:00
|
|
|
|
|
|
|
main()
|