Windows下多進程的實現案例

來源:互聯網
上載者:User

       最近碰到一個多進程的問題牽涉到了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);}}  }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.