
Gregory Colvin wrote:
extern "C" void child_handler(int) { pid = wait(&status); std::map<pid_t, int>::iterator it = completed_children.find(pid); if (it != completed_children.end()) it->second = status; }
I'm pretty sure that the above is safe code.
It's unsafe because it's not reentrant.
I believe to be truely reentrant yet flexible you'd need something more intrusive, such as a semaphore and then require the application to implement some checking, for example a callback that can be triggered from the main loop if ever the application has one.
But why do you need to reap the zombie? I've implemented the java process control natives with no need for reaping.
I don't know how java deals with this but on posix platforms a sub-process holds some resources that have to be claimed back by the parent process. This includes (but is not restricted to) the result of the process, i.e. its return value. In case you are familiar with (posix) threads: there you have to join a thread after it terminated. It's conceptually the same thing here. Regards, Stefan