I have a program that launches other instances of itself. Each instance redirects the standard output stream to a different file. I'm having an issue with the following chain of events:
- A BAT file gets executed that contains the command
my_program.exe > output_launcher.txt
- my_program.exe launches more instances of itself using the commands
my_program.exe > output_1.txt
andmy_program.exe > output_2.txt
- The original instance (
my_program.exe > output_launcher.txt
) exits while the two launched instances (my_program.exe > output_1.txt
andmy_program.exe > output_2.txt
) continue to execute. - The same BAT file from #1 gets executed again.
- The new instance of
my_program.exe > output_launcher.txt
fails with the error “The process cannot access the file because it is being used by another process.”
I don't get the error if the BAT file from #1 does not redirect output, and I don't get the error if the two launched instances exit prior to executing the BAT file for a second time.
So, I'm assuming that CMD.EXE is holding exclusive rights to the output_launcher.txt file until all of the sub-processes are finished.
First of all, is that a good assumption?
What I would like to happen is that CMD.EXE gives up its rights to output_launcher.txt when the original instance exists in #3 since each sub-process is redirecting to its own file.
Is this possible when redirecting standard output? The best alternative I can think of would be to actually take in the log file location as a command line argument, and write to the file directly from my program, rather than redirecting standard output. This would involve a lot more work for me though, so I'd like to avoid that route if possible.
Thanks!
EDIT: The actual command line that the first instance uses to start the additional instances looks like start cmd /C "call my_program.exe > output_1.txt"
. Then I pass that command to the "system()" function (my_program.exe is written in MSDN C).
Maybe I could launch the additional instances in a different way that could help?