工程描述
我要做个多进程的应用,大概意思是 父进程启动子进程1,子进程2,在第一次运行完子进程的时候,我不让子进程关闭,待下次需要调用子进程的时候 子进程接着运行。
问题
1. 如下代码在运行完子进程的时候,通过任务管理器,会发现子进程都消失了。
for(i=0; i<uiDevNum; i++)
{
ZeroMemory( &si[i], sizeof(si[i]) );
si[i].cb = sizeof(si[i]);
ZeroMemory( &pi[i], sizeof(pi[i]) );
memset(strCmdLine, 0, sizeof(strCmdLine));
strcpy(strCmdLine, "Auto_EnumDevice_Product_Test_Sub.exe");
strcat(strCmdLine, " ");
strcat(strCmdLine, pucDevName[i]);
memset(strDevType, 0, sizeof(strDevType));
sprintf(strDevType, "%d", puiDevType[i]);
strcat(strCmdLine, " ");
strcat(strCmdLine, strDevType);
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
strCmdLine, // Command line.
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.
)
{
//创建子进程失败
uiForkErrorFlag = 1;
goto END;
}
//父进程
//子进程计数
uiChildProcNum++;
lpHandles[i] = pi[i].hProcess;
}
2.如果在子进程里加上 while循环,我发现cpu使用率很高,每个子进程会达到百分之12,启动多个子进程的话,机器就动不了了,虽然我是8核机器。
请问 同学们这个问题怎么解决呢、万分感谢。