从c++程序中运行一些程序的多个副本

Run several copies of some program from c++ program

本文关键字:程序 副本 c++ 运行      更新时间:2023-10-16

我需要做的是从主程序中使用不同的命令行参数运行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调用都发生得非常快。