[英]Run several copies of some program from c++ program
我需要做的是使用与主程序不同的命令行参数运行 (例如) 6个副本 (独立进程)的example.exe (也是我的程序)。 此副本应该在同一时间工作。 我正在使用这样的代码:
const int NumberOfProcesses= 6;
STARTUPINFO si[NumberOfProcesses];
PROCESS_INFORMATION pi[NumberOfProcesses];
srand((unsigned)time(NULL));
for(int i=0;i<NumberOfProcesses;i++)
{
char fname[MAX_PATH];
strncpy(fname,"\"",1);
fname[1] = '\0';
strcat(fname,"d:\\test\\example.exe");
strcat(fname,"\"");
int id = i;
strcat(fname," ");
strcat(fname,(std::to_string(id)).c_str());
int count = (rand()%1000) + 1;
strcat(fname," ");
strcat(fname,(std::to_string(count)).c_str());
int lb = 13;
strcat(fname," ");
strcat(fname,(std::to_string(lb)).c_str());
int ub = 666;
strcat(fname," ");
strcat(fname,(std::to_string(ub)).c_str());
printf(fname);
cout<<"\n";
//Here in fname I have correct command, that runs properly
bool t = false;
t=CreateProcess( NULL, // No module name (use command line)
(LPSTR)fname, // Command line CharToLPWSTR(fname2)
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si[i], // Pointer to STARTUPINFO structure
&pi[i] ); // Pointer to PROCESS_INFORMATION structure
}
因此,如果NumberOfProcesses == 0,它将从fname运行1 2 3 4“ d:\\ test \\ example.exe” 。 但是,如果NumberOfProcesses == 6(或其他),零迭代将正确完成,但其他迭代将返回false。 有时第4次迭代正常运行。
我认为这是因为零迭代运行“ d:\\ test \\ example.exe” 1 2 3 4时 example.exe很忙,无法再运行一次。 所以我将代码更改为此:
bool t = false;
getchar();
for(int i=0;i<5;i++)
{
t=CreateProcess( NULL, // No module name (use command line)
(LPSTR)fname, // Command line CharToLPWSTR(fname2)
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si[i], // Pointer to STARTUPINFO structure
&pi[i] ); // Pointer to PROCESS_INFORMATION structure
if(t) break;
}
因此,在启动example.exe之间有一些延迟-它有所帮助 。 所有6个副本均已开始并完成,但它们不是并行运行的(我从example.exe输出)。 但这不是我希望程序工作的方式。
如何避免这个问题? 谢谢。
UPD。 根据Werner Henze的回答,我只添加了几行(以初始化STURTUPINFO )到循环中
const int NumberOfProcesses= 6;
STARTUPINFO si[NumberOfProcesses];
PROCESS_INFORMATION pi[NumberOfProcesses];
for(int i=0;i<NumberOfProcesses;i++)
{
///Next two lines is important
ZeroMemory( &si[i], sizeof(si[i]) );
si[i].cb = sizeof(si);
/*Some actions*/
t=CreateProcess( NULL, // No module name (use command line)
(LPSTR)fname, // Command line CharToLPWSTR(fname2)
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si[i], // Pointer to STARTUPINFO structure
&pi[i] ); // Pointer to PROCESS_INFORMATION structure
}
现在一切正常。 再次感谢。
如果CreateProcess返回FALSE,则应检查GetLastError()。 在您发布的代码中,您没有初始化si数组。 STARUPINFO是CreateProcess的输入参数,因此必须先对其进行初始化,然后再将其传递给CreateProcess。
我想example.exe运行得如此之快,以至于您在查看它们之前将其终止。 如果在每个CreateProcess之后打印诸如GetTickCount()之类的计时值,您将看到所有CreateProcess调用都发生得非常快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.