windows - Spawning multiple copies of a program from C code and redirecting the output to a file -
i working on modifying command-line program written in msdn c , pro*c (oracle pre-compiler write in-line sql , pl/sql statements) multiple copies can spawned , process concurrently. database-heavy program , concurrency issues taken care of on database side, thought easier run multiple copies alter program run multi-threaded.
anyway, rely on printf() , output piping write program's output log files debugging purposes. having trouble launching separate copies of exe appropriately write own log files. have played lot exec() , system() functions different copies of exe launch , write logs. closest have gotten work using c line such as:
system("start cmd /k call [program commmand , args] > log_file.txt");
this works great - spawns separate command windows , spawns separate copies of program appropriate data-sets. problem command windows stay open after program has finished executing. of our clients run program on scheduler, , having manually close of command windows not acceptable them.
other similar commands have tried looking like:
system("[program command , args] > log_file.txt");
or
exec("[program command , args] > log_file.txt");
both of these execute new copy of program, write log files fine, , close command window when process finished, commands wait newly spawned exe finish running return control launching executable. prevents multiple copies running @ same time, whole goal begin with, not solution either.
i played trying add "exit" command end of command line windows appending exiting command line, hoping command window close, this:
system("start cmd /k call \"[program commmand , args] > log_file.txt; exit\"");
to no avail. tried similar variations, never correct behavior.
i appreciate advice correct behavior. looking launch multiple copies of executable run concurrently , write separate log files, using " > log_file.txt" output-piping feature of windows command prompt. avoid having use threading libraries (it's been awhile , i'm under time constraints) or using other printf() , output piping, since these print statements used throughout application , large effort replace of function calls @ point in time.
anyone know way command windows close using system() calls, or have other easy method of solving problem? keep in mind there time constraints involved, i'm not looking best way this, quickest , easiest. have opportunity implement proper logging functionality soon, need past problem first.
you said first solution works great except doesn't close command window after program done executing.
system("start cmd /k call [program commmand , args] > log_file.txt");
the /k option states carries out command specified string remains
the /c option states carries out command specified string , terminates
you should able change original solution use /c option so
system("start cmd /c call [program commmand , args] > log_file.txt");
Comments
Post a Comment