最近碰到一個多進程的問題牽涉到了Windows下多進程的操作。經過研究也算是找到了一種解決方案。下面具體介紹一下這個題目中自己的收穫。
背景介紹:程式涉及到Win32與Linux兩個平台,要求同時啟動多個進程,進程下面啟動 多個線程。由於Windows的設計原理與Linux有很大區別(具體可參考Windows移植到Linux),因此Linux平台下建立多個進程比較容易實現。Windows下就比較困難。
下面是Linux下的代碼:
rv = setpgid(0, 0);if(rv == -1){return rv;}for(i=0; i<uiDevNum; i++){if((pids[i] = fork()) < 0){uiForkErrorFlag = 1;goto END;}else if(pids[i] == 0){rv = Primary_Test(pucDevName[i], puiDevType[i]);if(rv != SDR_OK){return rv;}_exit(127);}else{//父進程//子進程計數uiChildProcNum++;}}END:if((uiForkErrorFlag == 1) && (uiChildProcNum > 0)){//終止其餘子進程for(i=0; i<uiChildProcNum; i++){if(pids[i] != 0){kill(pids[i], SIGTERM);}}}else if(uiChildProcNum > 0){//等待子進程終止pid_Termination = wait(NULL); //終止其餘子進程for(i=0; i<uiChildProcNum; i++){if((pid_Termination == -1) || (pids[i] != pid_Termination)){kill(pids[i], SIGTERM);}}}
如果按照這個思路寫出Win32平台下的代碼,首先根據CreatProcess的設計結構,在Windows下我們知道CreatProcess建立一個新的進程,這意味著不能像Linux一樣建立的新進程執行本程式中的代碼。否則會出現無限遞迴建立進程的情況,這是我們將需要執行的那段代碼單獨抽取出來,產生可執行檔,如果跟父進程有什麼所需的參數,使用CreatProcess中的第二個參數傳遞進去。在父進程中通過每個子進程的handle監測每個子進程的執行情況。
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;}END:if((uiForkErrorFlag == 1) && (uiChildProcNum > 0)){//終止其餘子進程for(i=0; i<uiChildProcNum; i++){TerminateProcess(pi[i].hProcess, 127); SLEEP(60); CloseHandle(pi[i].hThread); CloseHandle(pi[i].hProcess);}}else if(uiChildProcNum > 0){rv = WaitForMultipleObjects(uiChildProcNum, lpHandles, FALSE, INFINITE);if((rv >= WAIT_OBJECT_0) && (rv <= WAIT_OBJECT_0 + uiChildProcNum - 1)){CloseHandle(pi[rv].hThread); CloseHandle(pi[rv].hProcess);} //終止其餘子進程for(i=0; i<uiChildProcNum; i++){if(i != rv){TerminateProcess(pi[i].hProcess, 127); SLEEP(60); CloseHandle(pi[i].hThread); CloseHandle(pi[i].hProcess);}} }