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.
|
|
|
|
|
2001-01-30 14:35:32 -04:00
|
|
|
While BeOS doesn't officially support fork and native threading in
|
|
|
|
the same application, the present example should work just fine. DC
|
2000-02-25 15:25:05 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os, sys, time, thread
|
2002-07-23 16:23:22 -03:00
|
|
|
from test.test_support import verify, verbose, TestSkipped
|
2000-02-25 15:25:05 -04:00
|
|
|
|
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
|
|
|
|
2001-04-11 17:58:20 -03:00
|
|
|
if sys.platform in ['unixware7']:
|
|
|
|
cpid = os.fork1()
|
|
|
|
else:
|
|
|
|
cpid = os.fork()
|
2000-02-25 15:25:05 -04:00
|
|
|
|
|
|
|
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()
|