in reply to waitpid for child #1 inside child #2: any way to make it work?
From Un*x man wait4:
The wait4() and waitpid() calls will fail and return immediately if:
[ECHILD]: The process specified by pid does not exist or is not a child of the calling process ...
See also perldoc perlport, which is a topic that’s all about operating-system dependent differences in behavior.
Waiting for a process-id is specifically a mechanism designed to “reap” the final status from terminated (zombie) processes. And, the entire notion of “zombies” is specifically engineered to facilitate that rendezvous without introducing race-conditions. If you truly need to wait for an unrelated process to finish, one way to do that sort of thing is with a semaphore ... but the timing is very tricky. Even if you launch two processes in quick succession, you cannot guarantee that the second will not begin executing first, and so on.
You have a design problem here. You are going to have to re-think portions of that design in order to obtain a program that not only “works at all,” but works reliably in all cases.