Windows via C/C ++: sub-process

Source: Internet
Author: User

When you write a program, you may want to run a piece of code independently, such as calling a function or subroutine. At this time, the caller must wait for the function or subroutine to return. In most cases, we need to synchronize this single task. Another optional method is to encapsulate this independent job into a new thread for execution. Now we can obtain the multi-task feature, which is very useful, however, if you want to obtain the execution results of the new thread, the synchronization problem will occur.

Another method is to create a sub-process to complete the task. When a task is complex, multi-threading may cause accidental destruction of data in the process space. A good synchronization mechanism is required to ensure data integrity. You do not have to worry about this situation when using sub-processes. The address space of different processes is independent of each other. However, in some cases, sub-processes need to read and write data from the address space of the parent process. Windows provides several methods for inter-process communication: Dynamic Data Exchange-DDE), Ole, MPs queue, and email slot. The most common method is the memory ing file. We will discuss memory ing in Chapter 17th.

After creating a sub-process, if you need to wait for its execution results, you can use code similar to the following:
Process_information PI; <br/> DWORD dwexitcode; </P> <p> // spawn the child process. <br/> bool fsuccess = CreateProcess (..., & PI); <br/> If (fsuccess) {</P> <p> // close the thread handle as soon as it is no longer needed! <Br/> closehandle (Pi. hthread); </P> <p> // suspend our execution until the child has terminated. <br/> waitforsingleobject (Pi. hprocess, infinite); </P> <p> // The child process terminated; get its exit code. <br/> getexitcodeprocess (Pi. hprocess, & dwexitcode); </P> <p> // close the Process Handle as soon as it is no longer needed. <br/> closehandle (Pi. hprocess); <br/>}In the above Code, we use the waitforsingleobject function to wait for the process to complete execution:
DWORD waitforsingleobject (handle hobject, DWORD dwtimeout );Chapter 9 describes the detailed usage of waitforsingleobject. Now you only need to know that the function will be blocked until the state of the object defined by hobject changes to signaled ), when the thread/process object is terminated, it changes to the "fired" state. After waitforsingleobject is returned, you can call getexitcodeprocess to obtain its exit code. The last closehandle call will reduce the reference count of the Process object by 1. In the above Code, the operating system will destroy the kernel object.

You may notice that when the CreateProcess function in the example returns, we close the handle of the main thread of the new process, which will not cause the corresponding thread to terminate, only the reference count of the main thread kernel object is reduced by 1. The advantage of this is that the operating system can destroy this object when appropriate. Otherwise, the reference count of the main thread object of the child process will never be cleared unless the process is terminated, in this way, the operating system cannot recycle the memory occupied by it.

Related Article

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.