There are some bugs with libgcj's POSIX Runtime.exec implementation. Problem #1: Process.exitValue() only returns the processes exit code if waitFor() has already been called, because only waitFor() ever calls waitpid. Problem #2: Process.waitFor() and Process.exitValue() do not work if they are called from a thread other than the thread which called Runtime.exec(). This seems to be because the pid created in Runtime.exec() is only considered a child of the thread which created it. Currently the problem can be hidden in some cases because Process.waitFor() will actually return 0 if waitpid returns an error other than EINTR. Possible solution: SIGCHLD handler?
I can confirm Problem 1. Problem 2 sounds more like a Linux (Linux Threads or otherwise, glibc) problem rather than a libgcj problem as it will work fine on Darwin as threads are not processes in Darwin.
Yes, it is presumably due to linux's 1:1 thread->pid mapping. I guess that POSIX does not specify how waitpid() should interact with threads, in which case it isn't a bug in linuxthreads. In any case, the JRE works correctly on the same system, so its something that needs to be fixed in libgcj.
I recently discovered that if your program does not call waitFor() on a process, it will remain as a zombie. This is predictable, of course, since only waitFor() calls wait to reap the child. However, this is a difference from the JDK, and thus arguably a bug. I'm not too concerned about LinuxThreads problems; the world is moving to NPTL...
*** Bug 12956 has been marked as a duplicate of this bug. ***
The way to solve the problem on non-NPTL linux systems is to create a thread that is dedicated to forking and waiting for all execed processes. This is the approach that Sun's runtime uses.
That sounds like the right approach. We could possibly simplify a lot of a lot of natPosixProcess.cc by doing this. A related bug I noticed recently is that Process.destroy() leaves a zombie, because it also doesnt call waitpid().
*** Bug 14709 has been marked as a duplicate of this bug. ***
Some more info in this thread: http://gcc.gnu.org/ml/java-patches/2004-q3/msg00126.html
Subject: Bug 11801 CVSROOT: /cvs/gcc Module name: gcc Changes by: daney@gcc.gnu.org 2004-08-12 16:20:12 Modified files: libjava : Makefile.am Makefile.in configure configure.in posix-threads.cc libjava/gcj : Makefile.in javaprims.h libjava/include: Makefile.in config.h.in libjava/java/lang: PosixProcess.java Runtime.java natPosixProcess.cc libjava/testsuite: Makefile.in Log message: 2004-08-12 David Daney <ddaney@avtrex.com> PR libgcj/11801 * java/lang/PosixProcess.java: Rewrote. * java/lang/natPosixProcess.cc: Rewrote. * java/lang/Runtime.java (execInternal): Declare throws IOException. * gcj/javaprims.h (ConcreteProcess$ProcessManager): Declare. * posix-threads.cc (block_sigchld) New function. (_Jv_ThreadRegister) Use it. (_Jv_ThreadStart) Use it. * configure.in (PLATFORM_INNER_NAT_HDRS): New AC_SUBST() used in... * Makefile.am: ... to specify extra native headers. * configure: Regenerated. * include/config.h: Regenerated. * Makefile.in: Regenerated. * gcj/Makefile.in: Regenerated. * include/Makefile.in: Regenerated. * testsuite/Makefile.in: Regenerated. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/Makefile.am.diff?cvsroot=gcc&r1=1.398&r2=1.399 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/Makefile.in.diff?cvsroot=gcc&r1=1.424&r2=1.425 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/configure.diff?cvsroot=gcc&r1=1.225&r2=1.226 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/configure.in.diff?cvsroot=gcc&r1=1.203&r2=1.204 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/posix-threads.cc.diff?cvsroot=gcc&r1=1.34&r2=1.35 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/gcj/Makefile.in.diff?cvsroot=gcc&r1=1.50&r2=1.51 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/gcj/javaprims.h.diff?cvsroot=gcc&r1=1.51&r2=1.52 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/include/Makefile.in.diff?cvsroot=gcc&r1=1.51&r2=1.52 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/include/config.h.in.diff?cvsroot=gcc&r1=1.52&r2=1.53 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/java/lang/PosixProcess.java.diff?cvsroot=gcc&r1=1.5&r2=1.6 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/java/lang/Runtime.java.diff?cvsroot=gcc&r1=1.15&r2=1.16 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/java/lang/natPosixProcess.cc.diff?cvsroot=gcc&r1=1.18&r2=1.19 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/testsuite/Makefile.in.diff?cvsroot=gcc&r1=1.61&r2=1.62
Fixed by the patch.