2000-02-25 15:25:05 -04: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.
|
|
|
|
|
2000-08-15 15:52:33 -03:00
|
|
|
On BeOS, you CANNOT mix threads and fork(), the behaviour is undefined.
|
|
|
|
That's OK, fork() is a grotesque hack anyway. ;-) [cjh]
|
|
|
|
|
2000-02-25 15:25:05 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os, sys, time, thread
|
2001-01-17 15:11:13 -04:00
|
|
|
from test_support import verify, verbose, TestSkipped
|
2000-02-25 15:25:05 -04:00
|
|
|
|
2000-08-15 15:52:33 -03:00
|
|
|
try:
|
|
|
|
if os.uname()[0] == "BeOS":
|
|
|
|
raise TestSkipped, "can't mix os.fork with threads on BeOS"
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2000-05-03 21:36:42 -03:00
|
|
|
try:
|
|
|
|
os.fork
|
|
|
|
except AttributeError:
|
2000-08-04 10:34:43 -03:00
|
|
|
raise TestSkipped, "os.fork not defined -- skipping test_fork1"
|
2000-05-03 21:36:42 -03:00
|
|
|
|
2000-02-25 15:25:05 -04:00
|
|
|
LONGSLEEP = 2
|
|
|
|
|
|
|
|
SHORTSLEEP = 0.5
|
|
|
|
|
2000-04-10 12:36:39 -03:00
|
|
|
NUM_THREADS = 4
|
|
|
|
|
2000-02-25 15:25:05 -04:00
|
|
|
alive = {}
|
|
|
|
|
2000-04-24 11:07:03 -03:00
|
|
|
stop = 0
|
|
|
|
|
2000-02-25 15:25:05 -04:00
|
|
|
def f(id):
|
2000-04-24 11:07:03 -03:00
|
|
|
while not stop:
|
2000-02-25 15:25:05 -04:00
|
|
|
alive[id] = os.getpid()
|
|
|
|
try:
|
|
|
|
time.sleep(SHORTSLEEP)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def main():
|
2000-04-10 12:36:39 -03:00
|
|
|
for i in range(NUM_THREADS):
|
2000-02-25 15:25:05 -04:00
|
|
|
thread.start_new(f, (i,))
|
|
|
|
|
|
|
|
time.sleep(LONGSLEEP)
|
|
|
|
|
|
|
|
a = alive.keys()
|
|
|
|
a.sort()
|
2001-01-17 15:11:13 -04:00
|
|
|
verify(a == range(NUM_THREADS))
|
2000-04-10 12:36:39 -03:00
|
|
|
|
|
|
|
prefork_lives = alive.copy()
|
2000-02-25 15:25:05 -04:00
|
|
|
|
|
|
|
cpid = os.fork()
|
|
|
|
|
|
|
|
if cpid == 0:
|
|
|
|
# Child
|
|
|
|
time.sleep(LONGSLEEP)
|
|
|
|
n = 0
|
|
|
|
for key in alive.keys():
|
2000-04-10 12:36:39 -03:00
|
|
|
if alive[key] != prefork_lives[key]:
|
2000-02-25 15:25:05 -04:00
|
|
|
n = n+1
|
|
|
|
os._exit(n)
|
|
|
|
else:
|
|
|
|
# Parent
|
|
|
|
spid, status = os.waitpid(cpid, 0)
|
2001-01-17 15:11:13 -04:00
|
|
|
verify(spid == cpid)
|
|
|
|
verify(status == 0,
|
|
|
|
"cause = %d, exit = %d" % (status&0xff, status>>8) )
|
2000-04-24 11:07:03 -03:00
|
|
|
global stop
|
|
|
|
# Tell threads to die
|
|
|
|
stop = 1
|
|
|
|
time.sleep(2*SHORTSLEEP) # Wait for threads to die
|
2000-02-25 15:25:05 -04:00
|
|
|
|
|
|
|
main()
|