从c++程序中运行一些程序的多个副本
Run several copies of some program from c++ program
我需要做的是从主程序中使用不同的命令行参数运行example.exe(也是我的程序)的6个副本。这些副本应在的同时工作。我使用的代码是这样的:
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] = ' ';
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运行"d:\test\example.exe"1 2 3 4。但如果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的回答,我刚刚在循环中添加了几行(到初始化STURTUPINO)
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调用都发生得非常快。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 如果静态变量只为程序的整个部分存储了一个副本,为什么我不能使用静态变量交换 2 个数字?
- 当分配动态内存以存储字符串副本时,程序会中断
- 从c++程序中运行一些程序的多个副本
- 处理程序捕获的对象是原始对象的副本
- MongoDB C++驱动程序处理副本集连接故障
- 在C ++ R3.0.2驱动程序中使用连接池连接到MongoDB副本集的正确方法是什么?