Working with Multiple Processes

The multithread libraries provide a number of routines for working with multiple processes. An application can use multiple processes for functions that require a private address space and private resources, to protect them from the activities of other threads. It is usually more efficient to implement multitasking by creating several threads in one process, rather than by creating multiple processes, for these reasons:

If you want to create an independent process that runs concurrently with the current one, use CreateProcess. CreateProcess returns a 32-bit process identifier that is valid until the process terminates. ExitProcess stops the process and notifies all DLLs the process is terminating.

Different processes can share mutexes, events, and semaphores (but not critical sections). Processes can also optionally inherit handles from the process that created them (see online help for CreateProcess).

You can obtain information about the current process by calling GetCurrentProcess (returns a pseudohandle to its own process), and GetCurrentProcessId (returns the process identifier). The value returned by these functions can be used in calls to communicate with other processes. GetExitCodeProcess returns the exit code of a process, or an indication that it is still running.

The OpenProcess function opens a handle to a process specified by its process identifier. OpenProcess allows you to specify the handle's access rights and inheritability.

A process terminates whenever one of the following occurs:

ExitProcess is the preferred way to terminate a process because it notifies all attached DLLs of the termination, and ensures that all threads of the process terminate. DLLs are not notified after a call to TerminateProcess.