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

时间:2013-11-18 07:50:46

标签: c++ windows createprocess

我需要做的是运行(例如) 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] = '\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 运行“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的回答,我只是添加几行(初始化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
}

现在工作正常。 再次感谢。

1 个答案:

答案 0 :(得分:0)

在CreateProcess返回FALSE的情况下,您应该检查GetLastError()。在您发布的代码中,您没有初始化si数组。 STARUPINFO是CreateProcess的输入参数,因此必须先将其初始化,然后再将其传递给CreateProcess。

我猜example.exe的运行速度非常快,以至于在你看到它们之前就已经终止了。如果在每个CreateProcess之后打印一个像GetTickCount()这样的计时值,您将看到所有CreateProcess调用都发生得非常快。