Multi-process implementation in Windows

Source: Internet
Author: User

Recently I encountered a problem of multi-process involving multi-process operations in windows. After research, we found a solution. The following describes your gains in this topic.

Background: The program involves Win32 and Linux platforms. It is required to start multiple processes at the same time and start multiple threads under the process. Because the design principle of Windows is quite different from that of Linux (For details, refer to porting windows to Linux), it is easier to create multiple processes on Linux. Windows is difficult.

The following is the code in 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 {// parent process // uichildprocnum for sub-process count ++;} end: if (uiforkerrorflag = 1) & (uichildprocnum> 0) {// terminate other sub-processes for (I = 0; I <uichildprocnum; I ++) {If (PIDS [I]! = 0) {kill (PIDS [I], sigterm) ;}} else if (uichildprocnum> 0) {// wait for the child process to terminate pid_termination = wait (null ); // terminate other sub-processes for (I = 0; I <uichildprocnum; I ++) {If (pid_termination =-1) | (PIDS [I]! = Pid_termination) {kill (PIDS [I], sigterm );}}}

If you write the code on the Win32 platform based on this idea, first, based on the creatprocess design structure, in windows, we know that creatprocess creates a new process, this means that the new process created like Linux cannot execute the code in this program. Otherwise, infinite recursion will occur to create a process. This is to extract the code that needs to be executed separately to generate an executable file. If there is any required parameter with the parent process, use the second parameter in creatprocess to pass in. The handle of each sub-process is used in the parent process to monitor the execution of each sub-process.

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 (str1_line, ""); strcat (strcm Dline, strdevtype); // start the child process. If (! CreateProcess (null, // No module name (use command line ). strpolicline, // 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_inform Ation structure .) {// failed to create sub-process uiforkerrorflag = 1; goto end;} // parent process // uichildprocnum for sub-process count ++; lphandles [I] = PI [I]. hprocess;} end: If (uiforkerrorflag = 1) & (uichildprocnum> 0) {// terminate other sub-processes 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, lphand Les, false, infinite); If (RV> = wait_object_0) & (RV <= wait_object_0 + uichildprocnum-1) {closehandle (PI [RV]. hthread); closehandle (PI [RV]. hprocess) ;}// terminate other sub-processes for (I = 0; I <uichildprocnum; I ++) {if (I! = RV) {terminateprocess (PI [I]. hprocess, 127); sleep (60); closehandle (PI [I]. hthread); closehandle (PI [I]. hprocess );}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.