Actually, I agree with Chas.Owens - the design is deadly corrupted.
It seems you need to fix the design, so:
If you have control over the 2nd program, create a file and transfer the file name to the 1st program.
But if the 2nd program is not what you wrote, and therefore you cannot change it, I would recommend one of the following:
1 - Use the first PID process as part of the file name to minimize the risk of duplicate file names.
2 - Make the 2nd program channel your access to the 1st program, do not bother the file at all. Personally, this is a much better solution than 1.
3 - Wrap the second program in a script (shell, perl, whatever) that creates the name and file and passes them to both programs.
source share